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