| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
use strict; |
| 4 |
|
| 5 |
use lib './lib'; |
| 6 |
|
| 7 |
use DateTime::TimeZone; |
| 8 |
use File::Spec; |
| 9 |
use Getopt::Long; |
| 10 |
use DateTime; |
| 11 |
|
| 12 |
my %opts; |
| 13 |
GetOptions( 'name:s' => \@{ $opts{name} }, |
| 14 |
'zoneinfo:s' => \$opts{zoneinfo}, |
| 15 |
'zdump:s' => \$opts{zdump}, |
| 16 |
'verbose' => \$opts{verbose}, |
| 17 |
'help' => \$opts{help}, |
| 18 |
); |
| 19 |
|
| 20 |
if ( $opts{help} ) |
| 21 |
{ |
| 22 |
print <<'EOF'; |
| 23 |
|
| 24 |
This script uses the zdump utility to generate comprehensive tests for |
| 25 |
time zones. |
| 26 |
|
| 27 |
Tests are generated as files in the tztests/ directory starting with "tz_". |
| 28 |
|
| 29 |
By default, it generates tests for all time zones. |
| 30 |
|
| 31 |
For each time zone name, it checks to see that the zoneinfo directory |
| 32 |
has a corresponding file. This is done because zdump will happily |
| 33 |
generate garbage output if given a non-existent time zone name. |
| 34 |
|
| 35 |
Note, if your version of the zoneinfo data is different from that used |
| 36 |
to generate the Perl time zone modules then you will almost certainly |
| 37 |
end up generating some tests that fail. |
| 38 |
|
| 39 |
It takes the following arguments: |
| 40 |
|
| 41 |
--name Only create tests for this zone. |
| 42 |
May be given multiple times. |
| 43 |
|
| 44 |
--zoneinfo The location of your zoneinfo directory. |
| 45 |
Defaults to /usr/share/zoneinfo. |
| 46 |
|
| 47 |
--zdump Path to zdump binary. Default is just 'zdump'. |
| 48 |
|
| 49 |
--verbose Blab about what it's doing as it does it. |
| 50 |
|
| 51 |
--help What you are reading |
| 52 |
|
| 53 |
EOF |
| 54 |
|
| 55 |
exit; |
| 56 |
} |
| 57 |
|
| 58 |
$opts{zoneinfo} ||= '/usr/share/zoneinfo'; |
| 59 |
|
| 60 |
die "No zoneinfo directory at $opts{zoneinfo}!\n" unless -d $opts{zoneinfo}; |
| 61 |
|
| 62 |
$opts{zdump} ||= 'zdump'; |
| 63 |
|
| 64 |
my $x = 1; |
| 65 |
my %months = map { $_ => $x++ } |
| 66 |
qw( Jan Feb Mar Apr May Jun |
| 67 |
Jul Aug Sep Oct Nov Dec); |
| 68 |
|
| 69 |
my @pieces = qw( year month day hour minute second ); |
| 70 |
|
| 71 |
my @names = @{ $opts{name} } ? @{ $opts{name} } : DateTime::TimeZone::all_names(); |
| 72 |
foreach my $tz_name (@names) |
| 73 |
{ |
| 74 |
unless ( -e File::Spec->catfile( $opts{zoneinfo}, split /\//, $tz_name ) ) |
| 75 |
{ |
| 76 |
print "\nNo zoneinfo file for $tz_name - skipping\n" if $opts{verbose}; |
| 77 |
next; |
| 78 |
} |
| 79 |
|
| 80 |
print "\nGetting change data for $tz_name\n" if $opts{verbose}; |
| 81 |
|
| 82 |
my @tests; |
| 83 |
my $command = "$opts{zdump} -v $tz_name"; |
| 84 |
my @lines = `$command`; |
| 85 |
|
| 86 |
die qq|Nothing returning from calling "$command". Did you specify a valid zdump binary?\n| |
| 87 |
unless @lines; |
| 88 |
|
| 89 |
foreach my $line (@lines) |
| 90 |
{ |
| 91 |
# This seems to happen on 64-bit systems. |
| 92 |
next if $line =~ /= NULL$/; |
| 93 |
|
| 94 |
my ( $utc_mon_name, $utc_day, $utc_hour, $utc_min, $utc_sec, $utc_year, |
| 95 |
$loc_mon_name, $loc_day, $loc_hour, $loc_min, $loc_sec, $loc_year, |
| 96 |
$short_name, $is_dst ) = |
| 97 |
$line =~ |
| 98 |
m/ ^ |
| 99 |
\w+(?:\/[\w\/-]+)? # zone name |
| 100 |
\s+ |
| 101 |
\w\w\w # UTC day name |
| 102 |
\s+ |
| 103 |
(\w\w\w) # UTC month name |
| 104 |
\s+ |
| 105 |
(\d+) # UTC day of month |
| 106 |
\s+ |
| 107 |
(\d\d):(\d\d):(\d\d) # UTC time |
| 108 |
\s+ |
| 109 |
(\d\d\d\d) # UTC year |
| 110 |
\s+ |
| 111 |
(?:UTC|GMT) # some systems say one, some the other |
| 112 |
\s+ |
| 113 |
= |
| 114 |
\s+ |
| 115 |
\w\w\w # local day name |
| 116 |
\s+ |
| 117 |
(\w\w\w) # local month name |
| 118 |
\s+ |
| 119 |
(\d+) # local day of month |
| 120 |
\s+ |
| 121 |
(\d\d):(\d\d):(\d\d) # local time |
| 122 |
\s+ |
| 123 |
(\d\d\d\d) # local year |
| 124 |
\s+ |
| 125 |
(\w+) # local short name |
| 126 |
\s+ |
| 127 |
isdst=(1|0) |
| 128 |
/x; |
| 129 |
|
| 130 |
unless ($1) |
| 131 |
{ |
| 132 |
warn "Can't parse zump output:\n$line\n"; |
| 133 |
next; |
| 134 |
} |
| 135 |
|
| 136 |
my $utc_month = $months{$utc_mon_name}; |
| 137 |
my $loc_month = $months{$loc_mon_name}; |
| 138 |
|
| 139 |
my $dt = DateTime->new( year => 1 * $utc_year, |
| 140 |
month => (1 * $utc_month), |
| 141 |
day => 1 * $utc_day, |
| 142 |
hour => 1 * $utc_hour, |
| 143 |
minute => 1 * $utc_min, |
| 144 |
second => 1 * $utc_sec, |
| 145 |
time_zone => 'UTC', ); |
| 146 |
$dt->set_time_zone($tz_name); |
| 147 |
# use '1 * ' to make sure everything is treated as numbers, |
| 148 |
push @tests, { time_zone => $tz_name, |
| 149 |
short_name => $short_name, |
| 150 |
epoch => $dt->epoch, |
| 151 |
offset => (($dt->offset * -1) / 60), |
| 152 |
edge => 1, |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
# Just some fairly-current tests - non-edgecase |
| 157 |
my $currDt = DateTime->new( year => 2009, |
| 158 |
month => 1, |
| 159 |
day => 5, |
| 160 |
hour => 10, |
| 161 |
minute => 30, |
| 162 |
second => 0, |
| 163 |
time_zone => 'UTC', ); |
| 164 |
$currDt->set_time_zone($tz_name); |
| 165 |
push @tests, { time_zone => $tz_name, |
| 166 |
short_name => $currDt->time_zone_short_name, |
| 167 |
epoch => $currDt->epoch, |
| 168 |
offset => (($currDt->offset * -1) / 60), |
| 169 |
edge => 0, |
| 170 |
}; |
| 171 |
$currDt = DateTime->new( year => 2009, |
| 172 |
month => 7, |
| 173 |
day => 5, |
| 174 |
hour => 10, |
| 175 |
minute => 30, |
| 176 |
second => 0, |
| 177 |
time_zone => 'UTC', ); |
| 178 |
$currDt->set_time_zone($tz_name); |
| 179 |
push @tests, { time_zone => $tz_name, |
| 180 |
short_name => $currDt->time_zone_short_name, |
| 181 |
epoch => $currDt->epoch, |
| 182 |
offset => (($currDt->offset * -1) / 60), |
| 183 |
edge => 0, |
| 184 |
}; |
| 185 |
|
| 186 |
|
| 187 |
unless (@tests) |
| 188 |
{ |
| 189 |
print "No change data in time_t range for $tz_name - can't create tests\n" if $opts{verbose}; |
| 190 |
next; |
| 191 |
} |
| 192 |
|
| 193 |
local *T; |
| 194 |
|
| 195 |
(my $test_file_name = $tz_name) =~ s,/,-,g; |
| 196 |
my $file = File::Spec->catfile( 'tztests', "tz_$test_file_name.json" ); |
| 197 |
open T, ">$file" |
| 198 |
or die "Cannot write to $file: $!"; |
| 199 |
|
| 200 |
print "Creating tests for $tz_name in $file\n" if $opts{verbose}; |
| 201 |
|
| 202 |
my $test_count = scalar @tests * 9; |
| 203 |
|
| 204 |
my $started = 0; |
| 205 |
|
| 206 |
foreach my $t (@tests) |
| 207 |
{ |
| 208 |
if (!$started){ |
| 209 |
print T <<"EOF"; |
| 210 |
({ |
| 211 |
name: "date.timezone.$test_file_name", |
| 212 |
runTest: function(t){ |
| 213 |
var tz = "$tz_name"; |
| 214 |
EOF |
| 215 |
$started = 1; |
| 216 |
} |
| 217 |
print T <<"EOF"; |
| 218 |
doh.checkDate({tzOffset: $t->{offset}, tzAbbr: "$t->{short_name}"}, $t->{epoch}000, tz, $t->{edge}); |
| 219 |
EOF |
| 220 |
} |
| 221 |
if($started){ |
| 222 |
print T <<"EOF"; |
| 223 |
} |
| 224 |
}) |
| 225 |
EOF |
| 226 |
} |
| 227 |
} |