This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: [perl #36654] Inconsistent treatment of NaN
[perl5.git] / lib / Time / Local.t
CommitLineData
1a3850a5
GA
1#!./perl
2
3BEGIN {
1c41b6a4
RGS
4 if ($ENV{PERL_CORE}){
5 chdir('t') if -d 't';
6 @INC = ('.', '../lib');
7 }
1a3850a5
GA
8}
9
823a6996
RGS
10use strict;
11
ee80afe6 12use Config;
823a6996 13use Test;
1a3850a5
GA
14use Time::Local;
15
16# Set up time values to test
823a6996 17my @time =
1a3850a5 18 (
823a6996 19 #year,mon,day,hour,min,sec
9b599b2a 20 [1970, 1, 2, 00, 00, 00],
1a3850a5
GA
21 [1980, 2, 28, 12, 00, 00],
22 [1980, 2, 29, 12, 00, 00],
23 [1999, 12, 31, 23, 59, 59],
24 [2000, 1, 1, 00, 00, 00],
25 [2010, 10, 12, 14, 13, 12],
5847cf89 26 # leap day
78063c05
PG
27 [2020, 2, 29, 12, 59, 59],
28 [2030, 7, 4, 17, 07, 06],
29# The following test fails on a surprising number of systems
30# so it is commented out. The end of the Epoch for a 32-bit signed
31# implementation of time_t should be Jan 19, 2038 03:14:07 UTC.
32# [2038, 1, 17, 23, 59, 59], # last full day in any tz
1a3850a5
GA
33 );
34
5847cf89
RGS
35my @bad_time =
36 (
37 # month too large
38 [1995, 13, 01, 01, 01, 01],
39 # day too large
40 [1995, 02, 30, 01, 01, 01],
41 # hour too large
42 [1995, 02, 10, 25, 01, 01],
43 # minute too large
44 [1995, 02, 10, 01, 60, 01],
45 # second too large
46 [1995, 02, 10, 01, 01, 60],
47 );
48
49my @neg_time =
50 (
51 # test negative epochs for systems that handle it
52 [ 1969, 12, 31, 16, 59, 59 ],
53 [ 1950, 04, 12, 9, 30, 31 ],
54 );
55
56my $neg_epoch_ok = defined ((localtime(-3600))[0]) ? 1 : 0;
57
61bb5906 58# use vmsish 'time' makes for oddness around the Unix epoch
5c415a7a
CB
59if ($^O eq 'VMS') {
60 $time[0][2]++;
61 $neg_epoch_ok = 0; # time_t is unsigned
62}
61bb5906 63
734c7f37
SH
64# Borland compiler sets $neg_epoch_ok differently depending on the
65# current Time Zone in use on the machine, but it should always be
66# set to 0.
67if ($^O eq 'MSWin32' and $Config{ccname} eq 'bcc32') {
68 $neg_epoch_ok = 0;
69}
70
5847cf89
RGS
71my $tests = (@time * 12);
72$tests += @neg_time * 12;
73$tests += @bad_time;
74$tests += 8;
1c41b6a4 75$tests += 2 if $ENV{PERL_CORE};
4ab0373f 76$tests += 5 if $ENV{MAINTAINER};
1c41b6a4 77
823a6996 78plan tests => $tests;
1a3850a5 79
5847cf89 80for (@time, @neg_time) {
1a3850a5
GA
81 my($year, $mon, $mday, $hour, $min, $sec) = @$_;
82 $year -= 1900;
823a6996
RGS
83 $mon--;
84
85 if ($^O eq 'vos' && $year == 70) {
86 skip(1, "skipping 1970 test on VOS.\n") for 1..6;
5847cf89
RGS
87 } elsif ($year < 70 && ! $neg_epoch_ok) {
88 skip(1, "skipping negative epoch.\n") for 1..6;
1a3850a5 89 } else {
5847cf89
RGS
90 my $year_in = $year < 70 ? $year + 1900 : $year;
91 my $time = timelocal($sec,$min,$hour,$mday,$mon,$year_in);
823a6996
RGS
92
93 my($s,$m,$h,$D,$M,$Y) = localtime($time);
94
5847cf89
RGS
95 ok($s, $sec, 'timelocal second');
96 ok($m, $min, 'timelocal minute');
97 ok($h, $hour, 'timelocal hour');
98 ok($D, $mday, 'timelocal day');
99 ok($M, $mon, 'timelocal month');
100 ok($Y, $year, 'timelocal year');
1a3850a5 101 }
1a3850a5 102
823a6996
RGS
103 if ($^O eq 'vos' && $year == 70) {
104 skip(1, "skipping 1970 test on VOS.\n") for 1..6;
5847cf89
RGS
105 } elsif ($year < 70 && ! $neg_epoch_ok) {
106 skip(1, "skipping negative epoch.\n") for 1..6;
1a3850a5 107 } else {
5847cf89
RGS
108 my $year_in = $year < 70 ? $year + 1900 : $year;
109 my $time = timegm($sec,$min,$hour,$mday,$mon,$year_in);
823a6996 110
4ab0373f 111 my($s,$m,$h,$D,$M,$Y) = gmtime($time);
823a6996 112
5847cf89
RGS
113 ok($s, $sec, 'timegm second');
114 ok($m, $min, 'timegm minute');
115 ok($h, $hour, 'timegm hour');
116 ok($D, $mday, 'timegm day');
117 ok($M, $mon, 'timegm month');
118 ok($Y, $year, 'timegm year');
1a3850a5 119 }
1a3850a5
GA
120}
121
5847cf89
RGS
122for (@bad_time) {
123 my($year, $mon, $mday, $hour, $min, $sec) = @$_;
124 $year -= 1900;
125 $mon--;
126
127 eval { timegm($sec,$min,$hour,$mday,$mon,$year) };
128
129 ok($@, qr/.*out of range.*/, 'invalid time caused an error');
130}
131
823a6996
RGS
132ok(timelocal(0,0,1,1,0,90) - timelocal(0,0,0,1,0,90), 3600,
133 'one hour difference between two calls to timelocal');
1a3850a5 134
823a6996
RGS
135ok(timelocal(1,2,3,1,0,100) - timelocal(1,2,3,31,11,99), 24 * 3600,
136 'one day difference between two calls to timelocal');
1a3850a5 137
78063c05 138# Diff beween Jan 1, 1980 and Mar 1, 1980 = (31 + 29 = 60 days)
823a6996
RGS
139ok(timegm(0,0,0, 1, 2, 80) - timegm(0,0,0, 1, 0, 80), 60 * 24 * 3600,
140 '60 day difference between two calls to timegm');
1a3850a5 141
13ef5feb
DM
142# bugid #19393
143# At a DST transition, the clock skips forward, eg from 01:59:59 to
144# 03:00:00. In this case, 02:00:00 is an invalid time, and should be
145# treated like 03:00:00 rather than 01:00:00 - negative zone offsets used
146# to do the latter
13ef5feb
DM
147{
148 my $hour = (localtime(timelocal(0, 0, 2, 7, 3, 102)))[2];
149 # testers in US/Pacific should get 3,
150 # other testers should get 2
823a6996
RGS
151 ok($hour == 2 || $hour == 3, 1, 'hour should be 2 or 3');
152}
153
5847cf89
RGS
154if ($neg_epoch_ok) {
155 eval { timegm(0,0,0,29,1,1900) };
156 ok($@, qr/Day '29' out of range 1\.\.28/);
157
158 eval { timegm(0,0,0,29,1,1904) };
159 ok($@, '');
160} else {
161 skip(1, "skipping negative epoch.\n") for 1..2;
162}
163
823a6996 164# round trip was broken for edge cases
ee80afe6 165if ($^O eq "aix" && $Config{osvers} =~ m/^4\.3\./) {
f6e7a6f2 166 skip( 1, "No fix expected for edge case test for $_ on AIX 4.3") for qw( timegm timelocal );
ee80afe6 167} else {
4ab0373f
MB
168 ok(sprintf('%x', timegm(gmtime(0x7fffffff))), sprintf('%x', 0x7fffffff),
169 '0x7fffffff round trip through gmtime then timegm');
823a6996 170
4ab0373f
MB
171 ok(sprintf('%x', timelocal(localtime(0x7fffffff))), sprintf('%x', 0x7fffffff),
172 '0x7fffffff round trip through localtime then timelocal');
ee80afe6 173}
823a6996
RGS
174
175if ($ENV{MAINTAINER}) {
176 eval { require POSIX; POSIX::tzset() };
177 if ($@) {
f6e7a6f2 178 skip( 1, "Cannot call POSIX::tzset() on this platform\n" ) for 1..3;
823a6996
RGS
179 }
180 else {
181 local $ENV{TZ} = 'Europe/Vienna';
182 POSIX::tzset();
183
184 # 2001-10-28 02:30:00 - could be either summer or standard time,
185 # prefer earlier of the two, in this case summer
186 my $time = timelocal(0, 30, 2, 28, 9, 101);
187 ok($time, 1004229000,
188 'timelocal prefers earlier epoch in the presence of a DST change');
189
190 local $ENV{TZ} = 'America/Chicago';
191 POSIX::tzset();
192
4ab0373f
MB
193 # Same local time in America/Chicago. There is a transition
194 # here as well.
823a6996
RGS
195 $time = timelocal(0, 30, 1, 28, 9, 101);
196 ok($time, 1004250600,
197 'timelocal prefers earlier epoch in the presence of a DST change');
198
4ab0373f
MB
199 $time = timelocal(0, 30, 2, 1, 3, 101);
200 ok($time, 986113800,
201 'timelocal for non-existent time gives you the time one hour later');
202
823a6996
RGS
203 local $ENV{TZ} = 'Australia/Sydney';
204 POSIX::tzset();
205
206 # 2001-03-25 02:30:00 in Australia/Sydney. This is the transition
207 # _to_ summer time. The southern hemisphere transitions are
208 # opposite those of the northern.
209 $time = timelocal(0, 30, 2, 25, 2, 101);
210 ok($time, 985447800,
211 'timelocal prefers earlier epoch in the presence of a DST change');
4ab0373f
MB
212
213 $time = timelocal(0, 30, 2, 28, 9, 101);
214 ok($time, 1004200200,
215 'timelocal for non-existent time gives you the time one hour later');
823a6996 216 }
13ef5feb
DM
217}
218
1c41b6a4 219if ($ENV{PERL_CORE}) {
1c41b6a4
RGS
220 package test;
221 require 'timelocal.pl';
4ab0373f
MB
222
223 # need to get ok() from main package
823a6996
RGS
224 ::ok(timegm(0,0,0,1,0,80), main::timegm(0,0,0,1,0,80),
225 'timegm in timelocal.pl');
1a3850a5 226
823a6996
RGS
227 ::ok(timelocal(1,2,3,4,5,88), main::timelocal(1,2,3,4,5,88),
228 'timelocal in timelocal.pl');
1c41b6a4 229}