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