This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Correct test output for t/op/eval.t (missing newline)
[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 => 44;
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     # The range should be limited only by the 53-bit mantissa of an IEEE double (or 
85     # whatever kind of double you've got).  Here we just prove that we're comfortably 
86     # beyond the range possible with 32-bit time_t.
87     my %tests = (
88         # time_t         gmtime list                          scalar
89         -2**35  => [52, 13, 20, 7, 2, -1019, 5, 65, 0, "Fri Mar  7 20:13:52 881"],
90         -2**32  => [44, 31, 17, 24, 10, -67, 0, 327, 0, "Sun Nov 24 17:31:44 1833"],
91         -2**31  => [52, 45, 20, 13, 11, 1, 5, 346, 0, "Fri Dec 13 20:45:52 1901"],
92         -1      => [59, 59, 23, 31, 11, 69, 3, 364, 0, "Wed Dec 31 23:59:59 1969"],
93         0       => [0, 0, 0, 1, 0, 70, 4, 0, 0, "Thu Jan  1 00:00:00 1970"],
94         1       => [1, 0, 0, 1, 0, 70, 4, 0, 0, "Thu Jan  1 00:00:01 1970"],
95         2**30   => [4, 37, 13, 10, 0, 104, 6, 9, 0, "Sat Jan 10 13:37:04 2004"],
96         2**31   => [8, 14, 3, 19, 0, 138, 2, 18, 0, "Tue Jan 19 03:14:08 2038"],
97         2**32   => [16, 28, 6, 7, 1, 206, 0, 37, 0, "Sun Feb  7 06:28:16 2106"],
98         2**39   => [8, 18, 12, 25, 0, 17491, 2, 24, 0, "Tue Jan 25 12:18:08 19391"],
99     );
100
101     for my $time (keys %tests) {
102         my @expected  = @{$tests{$time}};
103         my $scalar    = pop @expected;
104
105         ok eq_array([gmtime($time)], \@expected),  "gmtime($time) list context";
106         is scalar gmtime($time), $scalar,       "  scalar";
107     }
108 }
109
110
111 # Test localtime
112 {
113     # We pick times which fall in the middle of a month, so the month and year should be
114     # the same regardless of the time zone.
115     my %tests = (
116         # time_t           month, year,  scalar
117         -8589934592     => [9,    -203,                 qr/Oct \d+ .* 1697$/],
118         -1296000        => [11,   69,                   qr/Dec \d+ .* 1969$/],
119         1296000         => [0,    70,                   qr/Jan \d+ .* 1970$/],
120         5000000000      => [5,    228,                  qr/Jun \d+ .* 2128$/],
121         1163500000      => [10,   106,                  qr/Nov \d+ .* 2006$/],
122     );
123
124     for my $time (keys %tests) {
125         my @expected  = @{$tests{$time}};
126         my $scalar    = pop @expected;
127
128         my @time = (localtime($time))[4,5];
129         ok( eq_array(\@time, \@expected),  "localtime($time) list context" )
130           or diag("@time");
131         like scalar localtime($time), $scalar,       "  scalar";
132     }
133 }
134
135 # Test floating point args
136 {
137     eval {
138         $SIG{__WARN__} = sub { die @_; };
139         localtime(1.23);
140     };
141     is($@, '', 'Ignore fractional time');
142     eval {
143         $SIG{__WARN__} = sub { die @_; };
144         gmtime(1.23);
145     };
146     is($@, '', 'Ignore fractional time');
147 }