This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix command-line quoting under Windows for the new optree tests
[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
12use Test;
1a3850a5
GA
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
GA
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],
78063c05
PG
25 [2020, 2, 29, 12, 59, 59],
26 [2030, 7, 4, 17, 07, 06],
27# The following test fails on a surprising number of systems
28# so it is commented out. The end of the Epoch for a 32-bit signed
29# implementation of time_t should be Jan 19, 2038 03:14:07 UTC.
30# [2038, 1, 17, 23, 59, 59], # last full day in any tz
1a3850a5
GA
31 );
32
61bb5906
CB
33# use vmsish 'time' makes for oddness around the Unix epoch
34if ($^O eq 'VMS') { $time[0][2]++ }
35
823a6996 36my $tests = (@time * 12) + 6;
1c41b6a4 37$tests += 2 if $ENV{PERL_CORE};
823a6996 38$tests += 3 if $ENV{MAINTAINER};
1c41b6a4 39
823a6996 40plan tests => $tests;
1a3850a5 41
1a3850a5
GA
42for (@time) {
43 my($year, $mon, $mday, $hour, $min, $sec) = @$_;
44 $year -= 1900;
823a6996
RGS
45 $mon--;
46
47 if ($^O eq 'vos' && $year == 70) {
48 skip(1, "skipping 1970 test on VOS.\n") for 1..6;
1a3850a5 49 } else {
823a6996
RGS
50 my $time = timelocal($sec,$min,$hour,$mday,$mon,$year);
51
52 my($s,$m,$h,$D,$M,$Y) = localtime($time);
53
54 ok($s, $sec, 'second');
55 ok($m, $min, 'minute');
56 ok($h, $hour, 'hour');
57 ok($D, $mday, 'day');
58 ok($M, $mon, 'month');
59 ok($Y, $year, 'year');
1a3850a5 60 }
1a3850a5 61
823a6996
RGS
62 if ($^O eq 'vos' && $year == 70) {
63 skip(1, "skipping 1970 test on VOS.\n") for 1..6;
1a3850a5 64 } else {
823a6996
RGS
65 my $time = timelocal($sec,$min,$hour,$mday,$mon,$year);
66
67 my($s,$m,$h,$D,$M,$Y) = localtime($time);
68
69 ok($s, $sec, 'second');
70 ok($m, $min, 'minute');
71 ok($h, $hour, 'hour');
72 ok($D, $mday, 'day');
73 ok($M, $mon, 'month');
74 ok($Y, $year, 'year');
1a3850a5 75 }
1a3850a5
GA
76}
77
823a6996
RGS
78ok(timelocal(0,0,1,1,0,90) - timelocal(0,0,0,1,0,90), 3600,
79 'one hour difference between two calls to timelocal');
1a3850a5 80
823a6996
RGS
81ok(timelocal(1,2,3,1,0,100) - timelocal(1,2,3,31,11,99), 24 * 3600,
82 'one day difference between two calls to timelocal');
1a3850a5 83
78063c05 84# Diff beween Jan 1, 1980 and Mar 1, 1980 = (31 + 29 = 60 days)
823a6996
RGS
85ok(timegm(0,0,0, 1, 2, 80) - timegm(0,0,0, 1, 0, 80), 60 * 24 * 3600,
86 '60 day difference between two calls to timegm');
1a3850a5 87
13ef5feb
DM
88# bugid #19393
89# At a DST transition, the clock skips forward, eg from 01:59:59 to
90# 03:00:00. In this case, 02:00:00 is an invalid time, and should be
91# treated like 03:00:00 rather than 01:00:00 - negative zone offsets used
92# to do the latter
13ef5feb
DM
93{
94 my $hour = (localtime(timelocal(0, 0, 2, 7, 3, 102)))[2];
95 # testers in US/Pacific should get 3,
96 # other testers should get 2
823a6996
RGS
97 ok($hour == 2 || $hour == 3, 1, 'hour should be 2 or 3');
98}
99
100# round trip was broken for edge cases
101ok(sprintf('%x', timegm(gmtime(0x7fffffff))), sprintf('%x', 0x7fffffff),
102 '0x7fffffff round trip through gmtime then timegm');
103
104ok(sprintf('%x', timelocal(localtime(0x7fffffff))), sprintf('%x', 0x7fffffff),
105 '0x7fffffff round trip through localtime then timelocal');
106
107if ($ENV{MAINTAINER}) {
108 eval { require POSIX; POSIX::tzset() };
109 if ($@) {
110 skip("Cannot call POSIX::tzset() on this platform\n") for 1..3;
111 }
112 else {
113 local $ENV{TZ} = 'Europe/Vienna';
114 POSIX::tzset();
115
116 # 2001-10-28 02:30:00 - could be either summer or standard time,
117 # prefer earlier of the two, in this case summer
118 my $time = timelocal(0, 30, 2, 28, 9, 101);
119 ok($time, 1004229000,
120 'timelocal prefers earlier epoch in the presence of a DST change');
121
122 local $ENV{TZ} = 'America/Chicago';
123 POSIX::tzset();
124
125 # Same local time in America/Chicago. There is transition here as
126 # well.
127 $time = timelocal(0, 30, 1, 28, 9, 101);
128 ok($time, 1004250600,
129 'timelocal prefers earlier epoch in the presence of a DST change');
130
131 local $ENV{TZ} = 'Australia/Sydney';
132 POSIX::tzset();
133
134 # 2001-03-25 02:30:00 in Australia/Sydney. This is the transition
135 # _to_ summer time. The southern hemisphere transitions are
136 # opposite those of the northern.
137 $time = timelocal(0, 30, 2, 25, 2, 101);
138 ok($time, 985447800,
139 'timelocal prefers earlier epoch in the presence of a DST change');
140 }
13ef5feb
DM
141}
142
1c41b6a4 143if ($ENV{PERL_CORE}) {
1c41b6a4
RGS
144 package test;
145 require 'timelocal.pl';
823a6996
RGS
146 ::ok(timegm(0,0,0,1,0,80), main::timegm(0,0,0,1,0,80),
147 'timegm in timelocal.pl');
1a3850a5 148
823a6996
RGS
149 ::ok(timelocal(1,2,3,4,5,88), main::timelocal(1,2,3,4,5,88),
150 'timelocal in timelocal.pl');
1c41b6a4 151}