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