Move the require './test.pl' to the end of t/comp/hints.t
[p5sagit/p5-mst-13.2.git] / t / op / time.t
CommitLineData
8d063cd8 1#!./perl
2
a8c5b3cc 3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6 require './test.pl';
26575770 7}
8
43eb9815 9plan tests => 44;
8d063cd8 10
11($beguser,$begsys) = times;
12
13$beg = time;
14
463ee0b2 15while (($now = time) == $beg) { sleep 1 }
8d063cd8 16
26575770 17ok($now > $beg && $now - $beg < 10, 'very basic time test');
8d063cd8 18
5f80d426 19for ($i = 0; $i < 1_000_000; $i++) {
584ba4d5 20 for my $j (1..100) {}; # burn some user cycles
8d063cd8 21 ($nowuser, $nowsys) = times;
5f80d426 22 $i = 2_000_000 if $nowuser > $beguser && ( $nowsys >= $begsys ||
a0d0e21e 23 (!$nowsys && !$begsys));
8d063cd8 24 last if time - $beg > 20;
25}
26
5f80d426 27ok($i >= 2_000_000, 'very basic times test');
8d063cd8 28
29($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($beg);
30($xsec,$foo) = localtime($now);
31$localyday = $yday;
32
a272e669 33isnt($sec, $xsec), 'localtime() list context';
34ok $mday, ' month day';
35ok $year, ' year';
26575770 36
37ok(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 );
8d063cd8 43
a8c5b3cc 44SKIP: {
45 # This conditional of "No tzset()" is stolen from ext/POSIX/t/time.t
46 skip "No tzset()", 1
7b903762 47 if $^O eq "VMS" || $^O eq "cygwin" ||
a8c5b3cc 48 $^O eq "djgpp" || $^O eq "MSWin32" || $^O eq "dos" ||
49 $^O eq "interix";
50
8572b25d 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);
56ok($hour != $hour2, 'changes to $ENV{TZ} respected');
a8c5b3cc 57}
8572b25d 58
a0d0e21e 59
8d063cd8 60($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($beg);
61($xsec,$foo) = localtime($now);
62
a272e669 63isnt($sec, $xsec), 'gmtime() list conext';
64ok $mday, ' month day';
65ok $year, ' year';
26575770 66
67my $day_diff = $localyday - $yday;
68ok( grep({ $day_diff == $_ } (0, 1, -1, 364, 365, -364, -365)),
69 'gmtime() and localtime() agree what day of year');
8d063cd8 70
f5a29b03 71
72# This could be stricter.
26575770 73ok(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 );
a272e669 79
80
81
82# Test gmtime over a range of times.
83{
d95a2ea5 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.
a272e669 87 my %tests = (
88 # time_t gmtime list scalar
461d5a49 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"],
a272e669 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
461d5a49 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$/],
a272e669 122 );
123
124 for my $time (keys %tests) {
125 my @expected = @{$tests{$time}};
126 my $scalar = pop @expected;
127
4c91ace1 128 my @time = (localtime($time))[4,5];
129 ok( eq_array(\@time, \@expected), "localtime($time) list context" )
130 or diag("@time");
a272e669 131 like scalar localtime($time), $scalar, " scalar";
132 }
a8c5b3cc 133}
43eb9815 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}