Convert to using File::Spec, so that we can build Unicode files
[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
ee80afe6 12use Config;
823a6996 13use Test;
1a3850a5 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 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],
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
61bb5906 34# use vmsish 'time' makes for oddness around the Unix epoch
35if ($^O eq 'VMS') { $time[0][2]++ }
36
823a6996 37my $tests = (@time * 12) + 6;
1c41b6a4 38$tests += 2 if $ENV{PERL_CORE};
4ab0373f 39$tests += 5 if $ENV{MAINTAINER};
1c41b6a4 40
823a6996 41plan tests => $tests;
1a3850a5 42
1a3850a5 43for (@time) {
44 my($year, $mon, $mday, $hour, $min, $sec) = @$_;
45 $year -= 1900;
823a6996 46 $mon--;
47
48 if ($^O eq 'vos' && $year == 70) {
49 skip(1, "skipping 1970 test on VOS.\n") for 1..6;
1a3850a5 50 } else {
823a6996 51 my $time = timelocal($sec,$min,$hour,$mday,$mon,$year);
52
53 my($s,$m,$h,$D,$M,$Y) = localtime($time);
54
55 ok($s, $sec, 'second');
56 ok($m, $min, 'minute');
57 ok($h, $hour, 'hour');
58 ok($D, $mday, 'day');
59 ok($M, $mon, 'month');
60 ok($Y, $year, 'year');
1a3850a5 61 }
1a3850a5 62
823a6996 63 if ($^O eq 'vos' && $year == 70) {
64 skip(1, "skipping 1970 test on VOS.\n") for 1..6;
1a3850a5 65 } else {
4ab0373f 66 my $time = timegm($sec,$min,$hour,$mday,$mon,$year);
823a6996 67
4ab0373f 68 my($s,$m,$h,$D,$M,$Y) = gmtime($time);
823a6996 69
70 ok($s, $sec, 'second');
71 ok($m, $min, 'minute');
72 ok($h, $hour, 'hour');
73 ok($D, $mday, 'day');
74 ok($M, $mon, 'month');
75 ok($Y, $year, 'year');
1a3850a5 76 }
1a3850a5 77}
78
823a6996 79ok(timelocal(0,0,1,1,0,90) - timelocal(0,0,0,1,0,90), 3600,
80 'one hour difference between two calls to timelocal');
1a3850a5 81
823a6996 82ok(timelocal(1,2,3,1,0,100) - timelocal(1,2,3,31,11,99), 24 * 3600,
83 'one day difference between two calls to timelocal');
1a3850a5 84
78063c05 85# Diff beween Jan 1, 1980 and Mar 1, 1980 = (31 + 29 = 60 days)
823a6996 86ok(timegm(0,0,0, 1, 2, 80) - timegm(0,0,0, 1, 0, 80), 60 * 24 * 3600,
87 '60 day difference between two calls to timegm');
1a3850a5 88
13ef5feb 89# bugid #19393
90# At a DST transition, the clock skips forward, eg from 01:59:59 to
91# 03:00:00. In this case, 02:00:00 is an invalid time, and should be
92# treated like 03:00:00 rather than 01:00:00 - negative zone offsets used
93# to do the latter
13ef5feb 94{
95 my $hour = (localtime(timelocal(0, 0, 2, 7, 3, 102)))[2];
96 # testers in US/Pacific should get 3,
97 # other testers should get 2
823a6996 98 ok($hour == 2 || $hour == 3, 1, 'hour should be 2 or 3');
99}
100
101# round trip was broken for edge cases
ee80afe6 102if ($^O eq "aix" && $Config{osvers} =~ m/^4\.3\./) {
4ab0373f 103 skip ("No fix expected for edge case test for $_ on AIX 4.3") for qw( timegm timelocal );
ee80afe6 104} else {
4ab0373f 105 ok(sprintf('%x', timegm(gmtime(0x7fffffff))), sprintf('%x', 0x7fffffff),
106 '0x7fffffff round trip through gmtime then timegm');
823a6996 107
4ab0373f 108 ok(sprintf('%x', timelocal(localtime(0x7fffffff))), sprintf('%x', 0x7fffffff),
109 '0x7fffffff round trip through localtime then timelocal');
ee80afe6 110}
823a6996 111
112if ($ENV{MAINTAINER}) {
113 eval { require POSIX; POSIX::tzset() };
114 if ($@) {
115 skip("Cannot call POSIX::tzset() on this platform\n") for 1..3;
116 }
117 else {
118 local $ENV{TZ} = 'Europe/Vienna';
119 POSIX::tzset();
120
121 # 2001-10-28 02:30:00 - could be either summer or standard time,
122 # prefer earlier of the two, in this case summer
123 my $time = timelocal(0, 30, 2, 28, 9, 101);
124 ok($time, 1004229000,
125 'timelocal prefers earlier epoch in the presence of a DST change');
126
127 local $ENV{TZ} = 'America/Chicago';
128 POSIX::tzset();
129
4ab0373f 130 # Same local time in America/Chicago. There is a transition
131 # here as well.
823a6996 132 $time = timelocal(0, 30, 1, 28, 9, 101);
133 ok($time, 1004250600,
134 'timelocal prefers earlier epoch in the presence of a DST change');
135
4ab0373f 136 $time = timelocal(0, 30, 2, 1, 3, 101);
137 ok($time, 986113800,
138 'timelocal for non-existent time gives you the time one hour later');
139
823a6996 140 local $ENV{TZ} = 'Australia/Sydney';
141 POSIX::tzset();
142
143 # 2001-03-25 02:30:00 in Australia/Sydney. This is the transition
144 # _to_ summer time. The southern hemisphere transitions are
145 # opposite those of the northern.
146 $time = timelocal(0, 30, 2, 25, 2, 101);
147 ok($time, 985447800,
148 'timelocal prefers earlier epoch in the presence of a DST change');
4ab0373f 149
150 $time = timelocal(0, 30, 2, 28, 9, 101);
151 ok($time, 1004200200,
152 'timelocal for non-existent time gives you the time one hour later');
823a6996 153 }
13ef5feb 154}
155
1c41b6a4 156if ($ENV{PERL_CORE}) {
1c41b6a4 157 package test;
158 require 'timelocal.pl';
4ab0373f 159
160 # need to get ok() from main package
823a6996 161 ::ok(timegm(0,0,0,1,0,80), main::timegm(0,0,0,1,0,80),
162 'timegm in timelocal.pl');
1a3850a5 163
823a6996 164 ::ok(timelocal(1,2,3,4,5,88), main::timelocal(1,2,3,4,5,88),
165 'timelocal in timelocal.pl');
1c41b6a4 166}