xref: /openbsd-src/gnu/usr.bin/perl/t/op/time.t (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1#!./perl -w
2
3BEGIN {
4    chdir 't' if -d 't';
5    @INC = '../lib';
6    require './test.pl';
7}
8
9plan tests => 62;
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(15);
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');
24
25for ($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;
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    eval {
144        $SIG{__WARN__} = sub { die @_; };
145        is( (localtime(1296000.23))[5] + 1900, 1970 );
146    };
147    is($@, '', 'Ignore fractional time');
148    eval {
149        $SIG{__WARN__} = sub { die @_; };
150        is( (gmtime(1.23))[5] + 1900, 1970 );
151    };
152    is($@, '', 'Ignore fractional time');
153}
154
155
156# Some sanity tests for the far, far future and far, far past
157{
158    my %time2year = (
159        -2**52  => -142711421,
160        -2**48  => -8917617,
161        -2**46  => -2227927,
162         2**46  => 2231866,
163         2**48  => 8921556,
164         2**52  => 142715360,
165    );
166
167    for my $time (sort keys %time2year) {
168        my $want = $time2year{$time};
169
170        my $have = (gmtime($time))[5] + 1900;
171        is $have, $want, "year check, gmtime($time)";
172
173        $have = (localtime($time))[5] + 1900;
174        is $have, $want, "year check, localtime($time)";
175    }
176}
177
178
179# Test that Perl warns properly when it can't handle a time.
180{
181    my $warning;
182    local $SIG{__WARN__} = sub { $warning .= join "\n", @_; };
183
184    my $big_time   = 2**60;
185    my $small_time = -2**60;
186
187    $warning = '';
188    my $date = gmtime($big_time);
189    like $warning, qr/^gmtime(.*) too large/;
190
191    $warning = '';
192    $date = localtime($big_time);
193    like $warning, qr/^localtime(.*) too large/;
194
195    $warning = '';
196    $date = gmtime($small_time);
197    like $warning, qr/^gmtime(.*) too small/;
198
199    $warning = '';
200    $date = localtime($small_time);
201    like $warning, qr/^localtime(.*) too small/;
202}
203