This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
We are no longer bound by the limits of the tm struct.
[perl5.git] / t / op / time.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6     require './test.pl';
7 }
8
9 plan tests => 34;
10
11 ($beguser,$begsys) = times;
12
13 $beg = time;
14
15 while (($now = time) == $beg) { sleep 1 }
16
17 ok($now > $beg && $now - $beg < 10,             'very basic time test');
18
19 for ($i = 0; $i < 1_000_000; $i++) {
20     for my $j (1..100) {}; # burn some user cycles
21     ($nowuser, $nowsys) = times;
22     $i = 2_000_000 if $nowuser > $beguser && ( $nowsys >= $begsys ||
23                                             (!$nowsys && !$begsys));
24     last if time - $beg > 20;
25 }
26
27 ok($i >= 2_000_000, 'very basic times test');
28
29 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($beg);
30 ($xsec,$foo) = localtime($now);
31 $localyday = $yday;
32
33 isnt($sec, $xsec),      'localtime() list context';
34 ok $mday,               '  month day';
35 ok $year,               '  year';
36
37 ok(localtime() =~ /^(Sun|Mon|Tue|Wed|Thu|Fri|Sat)[ ]
38                     (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[ ]
39                     ([ \d]\d)\ (\d\d):(\d\d):(\d\d)\ (\d{4})$
40                   /x,
41    'localtime(), scalar context'
42   );
43
44 SKIP: {
45     # This conditional of "No tzset()" is stolen from ext/POSIX/t/time.t
46     skip "No tzset()", 1
47         if $^O eq "MacOS" || $^O eq "VMS" || $^O eq "cygwin" ||
48            $^O eq "djgpp" || $^O eq "MSWin32" || $^O eq "dos" ||
49            $^O eq "interix";
50
51 # check that localtime respects changes to $ENV{TZ}
52 $ENV{TZ} = "GMT-5";
53 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($beg);
54 $ENV{TZ} = "GMT+5";
55 ($sec,$min,$hour2,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($beg);
56 ok($hour != $hour2,                             'changes to $ENV{TZ} respected');
57 }
58
59
60 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($beg);
61 ($xsec,$foo) = localtime($now);
62
63 isnt($sec, $xsec),      'gmtime() list conext';
64 ok $mday,               '  month day';
65 ok $year,               '  year';
66
67 my $day_diff = $localyday - $yday;
68 ok( grep({ $day_diff == $_ } (0, 1, -1, 364, 365, -364, -365)),
69                      'gmtime() and localtime() agree what day of year');
70
71
72 # This could be stricter.
73 ok(gmtime() =~ /^(Sun|Mon|Tue|Wed|Thu|Fri|Sat)[ ]
74                  (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[ ]
75                  ([ \d]\d)\ (\d\d):(\d\d):(\d\d)\ (\d{4})$
76                /x,
77    'gmtime(), scalar context'
78   );
79
80
81
82 # Test gmtime over a range of times.
83 {
84     # gm/localtime should go all the way from -2**63 to 2**63-1
85     my %tests = (
86         # time_t         gmtime list                          scalar
87         -2**35        => [52, 13, 20, 7, 2, -1019, 5, 65, 0, "Fri Mar  7 20:13:52 881"],
88         -2**32        => [44, 31, 17, 24, 10, -67, 0, 327, 0, "Sun Nov 24 17:31:44 1833"],
89         -2**31        => [52, 45, 20, 13, 11, 1, 5, 346, 0, "Fri Dec 13 20:45:52 1901"],
90         0             => [0, 0, 0, 1, 0, 70, 4, 0, 0, "Thu Jan  1 00:00:00 1970"],
91         2**30         => [4, 37, 13, 10, 0, 104, 6, 9, 0, "Sat Jan 10 13:37:04 2004"],
92         2**31         => [8, 14, 3, 19, 0, 138, 2, 18, 0, "Tue Jan 19 03:14:08 2038"],
93         2**32         => [16, 28, 6, 7, 1, 206, 0, 37, 0, "Sun Feb  7 06:28:16 2106"],
94         2**39         => [8, 18, 12, 25, 0, 17491, 2, 24, 0, "Tue Jan 25 12:18:08 19391"],
95     );
96
97     for my $time (keys %tests) {
98         my @expected  = @{$tests{$time}};
99         my $scalar    = pop @expected;
100
101         ok eq_array([gmtime($time)], \@expected),  "gmtime($time) list context";
102         is scalar gmtime($time), $scalar,       "  scalar";
103     }
104 }
105
106
107 # Test localtime
108 {
109     # We pick times which fall in the middle of a month, so the month and year should be
110     # the same regardless of the time zone.
111     my %tests = (
112         # time_t           month, year,  scalar
113         -8589934592     => [9,    -203,  qr/Oct \d+ .* 1697$/],
114         5000000000      => [5,    228,   qr/Jun \d+ .* 2128$/],
115         1163500000      => [10,   106,   qr/Nov \d+ .* 2006$/],
116     );
117
118     for my $time (keys %tests) {
119         my @expected  = @{$tests{$time}};
120         my $scalar    = pop @expected;
121
122         my @time = (localtime($time))[4,5];
123         ok( eq_array(\@time, \@expected),  "localtime($time) list context" )
124           or diag("@time");
125         like scalar localtime($time), $scalar,       "  scalar";
126     }
127 }