1#!./perl -w
2
3BEGIN {
4    chdir 't' if -d 't';
5    require './test.pl';
6    set_up_inc('../lib');
7}
8
9plan tests => 72;
10
11# These tests make sure, among other things, that we don't end up
12# burning tons of CPU for dates far in the future.
13# watchdog() makes sure that the test script eventually exits if
14# the tests are triggering the failing behavior
15watchdog(25);
16
17($beguser,$begsys) = times;
18
19$beg = time;
20
21while (($now = time) == $beg) { sleep 1 }
22
23ok($now > $beg && $now - $beg < 10,             'very basic time test');
24my $x = "aaaa";
25for ($i = 0; $i < 1_000_000; $i++) {
26    for my $j (1..1000) { ++$x; }; # burn some user cycles
27    ($nowuser, $nowsys) = times;
28    $i = 2_000_000 if $nowuser > $beguser && ( $nowsys >= $begsys ||
29                                            (!$nowsys && !$begsys));
30    last if time - $beg > 20;
31}
32
33ok($i >= 2_000_000, 'very basic times test');
34
35($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($beg);
36($xsec,$foo) = localtime($now);
37$localyday = $yday;
38
39isnt($sec, $xsec,      'localtime() list context');
40ok $mday,              '  month day';
41ok $year,              '  year';
42
43ok(localtime() =~ /^(Sun|Mon|Tue|Wed|Thu|Fri|Sat)[ ]
44                    (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[ ]
45                    ([ \d]\d)\ (\d\d):(\d\d):(\d\d)\ (\d{4})$
46                  /x,
47   'localtime(), scalar context'
48  );
49
50SKIP: {
51    # This conditional of "No tzset()" is stolen from ext/POSIX/t/time.t
52    skip "No tzset()", 1
53        if $^O eq "VMS" || $^O eq "cygwin" ||
54           $^O eq "djgpp" || $^O eq "MSWin32" || $^O eq "dos" ||
55           $^O eq "interix";
56
57# check that localtime respects changes to $ENV{TZ}
58$ENV{TZ} = "GMT-5";
59($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($beg);
60$ENV{TZ} = "GMT+5";
61($sec,$min,$hour2,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($beg);
62ok($hour != $hour2,                             'changes to $ENV{TZ} respected');
63}
64
65
66($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($beg);
67($xsec,$foo) = localtime($now);
68
69isnt($sec, $xsec,      'gmtime() list conext');
70ok $mday,              '  month day';
71ok $year,              '  year';
72
73my $day_diff = $localyday - $yday;
74ok( grep({ $day_diff == $_ } (0, 1, -1, 364, 365, -364, -365)),
75                     'gmtime() and localtime() agree what day of year');
76
77
78# This could be stricter.
79ok(gmtime() =~ /^(Sun|Mon|Tue|Wed|Thu|Fri|Sat)[ ]
80                 (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[ ]
81                 ([ \d]\d)\ (\d\d):(\d\d):(\d\d)\ (\d{4})$
82               /x,
83   'gmtime(), scalar context'
84  );
85
86
87
88# Test gmtime over a range of times.
89{
90    # The range should be limited only by the 53-bit mantissa of an IEEE double (or
91    # whatever kind of double you've got).  Here we just prove that we're comfortably
92    # beyond the range possible with 32-bit time_t.
93    my %tests = (
94        # time_t         gmtime list                          scalar
95        -2**35  => [52, 13, 20, 7, 2, -1019, 5, 65, 0, "Fri Mar  7 20:13:52 881"],
96        -2**32  => [44, 31, 17, 24, 10, -67, 0, 327, 0, "Sun Nov 24 17:31:44 1833"],
97        -2**31  => [52, 45, 20, 13, 11, 1, 5, 346, 0, "Fri Dec 13 20:45:52 1901"],
98        -1      => [59, 59, 23, 31, 11, 69, 3, 364, 0, "Wed Dec 31 23:59:59 1969"],
99        0       => [0, 0, 0, 1, 0, 70, 4, 0, 0, "Thu Jan  1 00:00:00 1970"],
100        1       => [1, 0, 0, 1, 0, 70, 4, 0, 0, "Thu Jan  1 00:00:01 1970"],
101        2**30   => [4, 37, 13, 10, 0, 104, 6, 9, 0, "Sat Jan 10 13:37:04 2004"],
102        2**31   => [8, 14, 3, 19, 0, 138, 2, 18, 0, "Tue Jan 19 03:14:08 2038"],
103        2**32   => [16, 28, 6, 7, 1, 206, 0, 37, 0, "Sun Feb  7 06:28:16 2106"],
104        2**39   => [8, 18, 12, 25, 0, 17491, 2, 24, 0, "Tue Jan 25 12:18:08 19391"],
105    );
106
107    for my $time (keys %tests) {
108        my @expected  = @{$tests{$time}};
109        my $scalar    = pop @expected;
110
111        ok eq_array([gmtime($time)], \@expected),  "gmtime($time) list context";
112        is scalar gmtime($time), $scalar,       "  scalar";
113    }
114}
115
116
117# Test localtime
118{
119    # We pick times which fall in the middle of a month, so the month and year should be
120    # the same regardless of the time zone.
121    my %tests = (
122        # time_t           month, year,  scalar
123        -8589934592     => [9,    -203,                 qr/Oct \d+ .* 1697$/],
124        -1296000        => [11,   69,                   qr/Dec \d+ .* 1969$/],
125        1296000         => [0,    70,                   qr/Jan \d+ .* 1970$/],
126        5000000000      => [5,    228,                  qr/Jun \d+ .* 2128$/],
127        1163500000      => [10,   106,                  qr/Nov \d+ .* 2006$/],
128    );
129
130    for my $time (keys %tests) {
131        my @expected  = @{$tests{$time}};
132        my $scalar    = pop @expected;
133
134        my @time = (localtime($time))[4,5];
135        ok( eq_array(\@time, \@expected),  "localtime($time) list context" )
136          or diag("@time");
137        like scalar localtime($time), $scalar,       "  scalar";
138    }
139}
140
141# Test floating point args
142{
143    warning_is(sub {is( (localtime(1296000.23))[5] + 1900, 1970 )},
144	       undef, 'Ignore fractional time');
145    warning_is(sub {is( (gmtime(1.23))[5] + 1900, 1970 )},
146	       undef, 'Ignore fractional time');
147}
148
149
150# Some sanity tests for the far, far future and far, far past
151{
152    my %time2year = (
153        -2**52  => -142711421,
154        -2**48  => -8917617,
155        -2**46  => -2227927,
156         2**46  => 2231866,
157         2**48  => 8921556,
158         2**52  => 142715360,
159    );
160
161    for my $time (sort keys %time2year) {
162        my $want = $time2year{$time};
163
164        my $have = (gmtime($time))[5] + 1900;
165        is $have, $want, "year check, gmtime($time)";
166
167        $have = (localtime($time))[5] + 1900;
168        is $have, $want, "year check, localtime($time)";
169    }
170}
171
172
173# Test that Perl warns properly when it can't handle a time.
174{
175    my $warning;
176    local $SIG{__WARN__} = sub { $warning .= join "\n", @_; };
177
178    my $big_time   = 2**60;
179    my $small_time = -2**60;
180
181    $warning = '';
182    my $date = gmtime($big_time);
183    like $warning, qr/^gmtime(.*) too large/;
184
185    $warning = '';
186    $date = localtime($big_time);
187    like $warning, qr/^localtime(.*) too large/;
188
189    $warning = '';
190    $date = gmtime($small_time);
191    like $warning, qr/^gmtime(.*) too small/;
192
193    $warning = '';
194    $date = localtime($small_time);
195    like $warning, qr/^localtime(.*) too small/;
196}
197
198SKIP: { #rt #73040
199    # these are from the definitions of TIME_LOWER_BOUND AND TIME_UPPER_BOUND
200    my $smallest = -67768100567755200.0;
201    my $biggest = 67767976233316800.0;
202
203    # offset to a value that will fail
204    my $small_time = $smallest - 200;
205    my $big_time = $biggest + 200;
206
207    # check they're representable - typically means NV is
208    # long double
209    if ($small_time + 200 != $smallest
210	|| $small_time == $smallest
211        || $big_time - 200 != $biggest
212	|| $big_time == $biggest) {
213	skip "Can't represent test values", 8;
214    }
215    my $small_time_f = sprintf("%.0f", $small_time);
216    my $big_time_f = sprintf("%.0f", $big_time);
217
218    # check the numbers in the warning are correct
219    my $warning;
220    local $SIG{__WARN__} = sub { $warning .= join "\n", @_; };
221    $warning = '';
222    my $date = gmtime($big_time);
223    like $warning, qr/^gmtime\($big_time_f\) too large/;
224    like $warning, qr/^gmtime\($big_time_f\) failed/m;
225
226    $warning = '';
227    $date = localtime($big_time);
228    like $warning, qr/^localtime\($big_time_f\) too large/;
229    like $warning, qr/^localtime\($big_time_f\) failed/m;
230
231    $warning = '';
232    $date = gmtime($small_time);
233    like $warning, qr/^gmtime\($small_time_f\) too small/;
234    like $warning, qr/^gmtime\($small_time_f\) failed/m;
235
236    $warning = '';
237    $date = localtime($small_time);
238    like $warning, qr/^localtime\($small_time_f\) too small/;
239    like $warning, qr/^localtime\($small_time_f\) failed/m;
240}
241
242my $is_vax = (pack("d", 1) =~ /^[\x80\x10]\x40/);
243my $has_nan = !$is_vax;
244
245SKIP: {
246    skip("No NaN", 2) unless $has_nan;
247    local $^W;
248    is scalar gmtime("NaN"), undef, '[perl #123495] gmtime(NaN)';
249    is scalar localtime("NaN"), undef, 'localtime(NaN)';
250}
251