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
17 ($beguser,$begsys) = times;
21 while (($now = time) == $beg) { sleep 1 }
23 ok($now > $beg && $now - $beg < 10, 'very basic time test');
25 for ($i = 0; $i < 1_000_000; $i++) {
26 for my $j (1..100) {}; # 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;
33 ok($i >= 2_000_000, 'very basic times test');
35 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($beg);
36 ($xsec,$foo) = localtime($now);
39 isnt($sec, $xsec, 'localtime() list context');
40 ok $mday, ' month day';
43 ok(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})$
47 'localtime(), scalar context'
51 # This conditional of "No tzset()" is stolen from ext/POSIX/t/time.t
53 if $^O eq "VMS" || $^O eq "cygwin" ||
54 $^O eq "djgpp" || $^O eq "MSWin32" || $^O eq "dos" ||
57 # check that localtime respects changes to $ENV{TZ}
59 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($beg);
61 ($sec,$min,$hour2,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($beg);
62 ok($hour != $hour2, 'changes to $ENV{TZ} respected');
66 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($beg);
67 ($xsec,$foo) = localtime($now);
69 isnt($sec, $xsec, 'gmtime() list conext');
70 ok $mday, ' month day';
73 my $day_diff = $localyday - $yday;
74 ok( grep({ $day_diff == $_ } (0, 1, -1, 364, 365, -364, -365)),
75 'gmtime() and localtime() agree what day of year');
78 # This could be stricter.
79 ok(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})$
83 'gmtime(), scalar context'
88 # Test gmtime over a range of times.
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.
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"],
107 for my $time (keys %tests) {
108 my @expected = @{$tests{$time}};
109 my $scalar = pop @expected;
111 ok eq_array([gmtime($time)], \@expected), "gmtime($time) list context";
112 is scalar gmtime($time), $scalar, " scalar";
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.
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$/],
130 for my $time (keys %tests) {
131 my @expected = @{$tests{$time}};
132 my $scalar = pop @expected;
134 my @time = (localtime($time))[4,5];
135 ok( eq_array(\@time, \@expected), "localtime($time) list context" )
137 like scalar localtime($time), $scalar, " scalar";
141 # Test floating point args
144 $SIG{__WARN__} = sub { die @_; };
145 is( (localtime(1296000.23))[5] + 1900, 1970 );
147 is($@, '', 'Ignore fractional time');
149 $SIG{__WARN__} = sub { die @_; };
150 is( (gmtime(1.23))[5] + 1900, 1970 );
152 is($@, '', 'Ignore fractional time');
156 # Some sanity tests for the far, far future and far, far past
159 -2**52 => -142711421,
167 for my $time (sort keys %time2year) {
168 my $want = $time2year{$time};
170 my $have = (gmtime($time))[5] + 1900;
171 is $have, $want, "year check, gmtime($time)";
173 $have = (localtime($time))[5] + 1900;
174 is $have, $want, "year check, localtime($time)";
179 # Test that Perl warns properly when it can't handle a time.
182 local $SIG{__WARN__} = sub { $warning .= join "\n", @_; };
184 my $big_time = 2**60;
185 my $small_time = -2**60;
188 my $date = gmtime($big_time);
189 like $warning, qr/^gmtime(.*) too large/;
192 $date = localtime($big_time);
193 like $warning, qr/^localtime(.*) too large/;
196 $date = gmtime($small_time);
197 like $warning, qr/^gmtime(.*) too small/;
200 $date = localtime($small_time);
201 like $warning, qr/^localtime(.*) too small/;