<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl -w

use strict;

use lib './lib';

use DateTime::TimeZone;
use File::Spec;
use Getopt::Long;
use DateTime;

my %opts;
GetOptions( 'name:s'     =&gt; \@{ $opts{name} },
            'zoneinfo:s' =&gt; \$opts{zoneinfo},
            'zdump:s'    =&gt; \$opts{zdump},
            'verbose'    =&gt; \$opts{verbose},
            'help'       =&gt; \$opts{help},
          );

if ( $opts{help} )
{
    print &lt;&lt;'EOF';

This script uses the zdump utility to generate comprehensive tests for
time zones.

Tests are generated as files in the tztests/ directory starting with "tz_".

By default, it generates tests for all time zones.

For each time zone name, it checks to see that the zoneinfo directory
has a corresponding file.  This is done because zdump will happily
generate garbage output if given a non-existent time zone name.

Note, if your version of the zoneinfo data is different from that used
to generate the Perl time zone modules then you will almost certainly
end up generating some tests that fail.

It takes the following arguments:

  --name      Only create tests for this zone.
              May be given multiple times.

  --zoneinfo  The location of your zoneinfo directory.
              Defaults to /usr/share/zoneinfo.

  --zdump     Path to zdump binary.  Default is just 'zdump'.

  --verbose   Blab about what it's doing as it does it.

  --help      What you are reading

EOF

    exit;
}

$opts{zoneinfo} ||= '/usr/share/zoneinfo';

die "No zoneinfo directory at $opts{zoneinfo}!\n" unless -d $opts{zoneinfo};

$opts{zdump} ||= 'zdump';

my $x = 1;
my %months = map { $_ =&gt; $x++ }
             qw( Jan Feb Mar Apr May Jun
	         Jul Aug Sep Oct Nov Dec);

my @pieces = qw( year month day hour minute second );

my @names = @{ $opts{name} } ? @{ $opts{name} } : DateTime::TimeZone::all_names();
foreach my $tz_name (@names)
{
    unless ( -e File::Spec-&gt;catfile( $opts{zoneinfo}, split /\//, $tz_name ) )
    {
        print "\nNo zoneinfo file for $tz_name - skipping\n" if $opts{verbose};
        next;
    }

    print "\nGetting change data for $tz_name\n" if $opts{verbose};

    my @tests;
    my $command = "$opts{zdump} -v $tz_name";
    my @lines = `$command`;

    die qq|Nothing returning from calling "$command".  Did you specify a valid zdump binary?\n|
        unless @lines;

    foreach my $line (@lines)
    {
        # This seems to happen on 64-bit systems.
        next if $line =~ /= NULL$/;

        my ( $utc_mon_name, $utc_day, $utc_hour, $utc_min, $utc_sec, $utc_year,
             $loc_mon_name, $loc_day, $loc_hour, $loc_min, $loc_sec, $loc_year,
             $short_name, $is_dst ) =
                 $line =~
                     m/ ^
                        \w+(?:\/[\w\/-]+)? # zone name
                        \s+
                        \w\w\w        # UTC day name
                        \s+
                        (\w\w\w)      # UTC month name
                        \s+
                        (\d+)         # UTC day of month
                        \s+
                        (\d\d):(\d\d):(\d\d)  # UTC time
                        \s+
                        (\d\d\d\d)    # UTC year
                        \s+
                        (?:UTC|GMT)   # some systems say one, some the other
                        \s+
                        =
                        \s+
                        \w\w\w        # local day name
                        \s+
                        (\w\w\w)      # local month name
                        \s+
                        (\d+)         # local day of month
                        \s+
                        (\d\d):(\d\d):(\d\d)  # local time
                        \s+
                        (\d\d\d\d)    # local year
                        \s+
                        (\w+)         # local short name
                        \s+
                        isdst=(1|0)
                      /x;

        unless ($1)
        {
            warn "Can't parse zump output:\n$line\n";
            next;
        }

        my $utc_month = $months{$utc_mon_name};
        my $loc_month = $months{$loc_mon_name};

		my $dt = DateTime-&gt;new( year =&gt; 1 * $utc_year,
								month =&gt; (1 * $utc_month),
								day =&gt; 1 * $utc_day,
								hour =&gt; 1 * $utc_hour,
								minute =&gt; 1 * $utc_min,
								second =&gt; 1 * $utc_sec,
								time_zone =&gt; 'UTC', );
		$dt-&gt;set_time_zone($tz_name);
        # use '1 * ' to make sure everything is treated as numbers,
        push @tests, { time_zone =&gt; $tz_name,
                       short_name =&gt; $short_name,
					   epoch	  =&gt; $dt-&gt;epoch,
                       offset     =&gt; (($dt-&gt;offset * -1) / 60),
                       edge       =&gt; 1,
                     }
    }

	# Just some fairly-current tests - non-edgecase
	my $currDt = DateTime-&gt;new( year =&gt; 2009,
                                month =&gt; 1,
                                day =&gt; 5,
                                hour =&gt; 10,
                                minute =&gt; 30,
                                second =&gt; 0,
                                time_zone =&gt; 'UTC', );
	$currDt-&gt;set_time_zone($tz_name);
	push @tests, { time_zone =&gt; $tz_name,
                       short_name =&gt; $currDt-&gt;time_zone_short_name,
					   epoch	  =&gt; $currDt-&gt;epoch,
                       offset     =&gt; (($currDt-&gt;offset * -1) / 60),
                       edge       =&gt; 0,
                     };
	$currDt = DateTime-&gt;new( year =&gt; 2009,
                                month =&gt; 7,
                                day =&gt; 5,
                                hour =&gt; 10,
                                minute =&gt; 30,
                                second =&gt; 0,
                                time_zone =&gt; 'UTC', );
	$currDt-&gt;set_time_zone($tz_name);
	push @tests, { time_zone =&gt; $tz_name,
                       short_name =&gt; $currDt-&gt;time_zone_short_name,
					   epoch	  =&gt; $currDt-&gt;epoch,
                       offset     =&gt; (($currDt-&gt;offset * -1) / 60),
                       edge       =&gt; 0,
                     };
					

    unless (@tests)
    {
        print "No change data in time_t range for $tz_name - can't create tests\n" if $opts{verbose};
        next;
    }

    local *T;

    (my $test_file_name = $tz_name) =~ s,/,-,g;
    my $file = File::Spec-&gt;catfile( 'tztests', "tz_$test_file_name.json" );
    open T, "&gt;$file"
        or die "Cannot write to $file: $!";

    print "Creating tests for $tz_name in $file\n" if $opts{verbose};

    my $test_count = scalar @tests * 9;

	my $started = 0;

    foreach my $t (@tests)
    {
		if (!$started){
			print T &lt;&lt;"EOF";
({
	name: "date.timezone.$test_file_name",
	runTest: function(t){
		var tz = "$tz_name";
EOF
			$started = 1;
		}
		print T &lt;&lt;"EOF";
		doh.checkDate({tzOffset: $t-&gt;{offset}, tzAbbr: "$t-&gt;{short_name}"}, $t-&gt;{epoch}000, tz, $t-&gt;{edge});
EOF
	}
	if($started){
    	print T &lt;&lt;"EOF";
	}
})
EOF
	}
}
</pre></body></html>