[perl #63914] Time::Piece reports timezone incorrectly
[p5sagit/p5-mst-13.2.git] / ext / Time-Piece / t / 02core.t
1 use Test::More tests => 95;
2
3 my $is_win32 = ($^O =~ /Win32/);
4 my $is_qnx = ($^O eq 'qnx');
5 BEGIN { use_ok('Time::Piece'); }
6 ok(1);
7
8 my $t = gmtime(951827696); # 2000-02-29T12:34:56
9
10 is($t->sec,               56);
11 is($t->second,            56);
12 is($t->min,               34);
13 is($t->minute,            34);
14 is($t->hour,              12);
15 is($t->mday,              29);
16 is($t->day_of_month,      29);
17 is($t->mon,                2);
18 is($t->_mon,               1);
19 is($t->monname,        'Feb');
20 is($t->month,          'Feb');
21 is($t->fullmonth, 'February');
22 is($t->year,            2000);
23 is($t->_year,            100);
24 is($t->yy,              '00');
25
26 cmp_ok($t->wday,        '==',         3);
27 cmp_ok($t->_wday,       '==',         2);
28 cmp_ok($t->day_of_week, '==',         2);
29 cmp_ok($t->wdayname,    'eq',     'Tue');
30 cmp_ok($t->day,         'eq',     'Tue');
31 cmp_ok($t->fullday,     'eq', 'Tuesday');
32 cmp_ok($t->yday,        '==',        59);
33 cmp_ok($t->day_of_year, '==',        59);
34
35 # In GMT there should be no daylight savings ever.
36 cmp_ok($t->isdst, '==', 0);
37 cmp_ok($t->epoch, '==', 951827696);
38 cmp_ok($t->hms,   'eq',   '12:34:56');
39 cmp_ok($t->time,  'eq',   '12:34:56');
40 cmp_ok($t->ymd,   'eq', '2000-02-29');
41 cmp_ok($t->date,  'eq', '2000-02-29');
42 cmp_ok($t->mdy,   'eq', '02-29-2000');
43 cmp_ok($t->dmy,   'eq', '29-02-2000');
44 cmp_ok($t->cdate, 'eq', 'Tue Feb 29 12:34:56 2000');
45 cmp_ok("$t",      'eq', 'Tue Feb 29 12:34:56 2000');
46 cmp_ok($t->datetime, 'eq','2000-02-29T12:34:56');
47 cmp_ok($t->daylight_savings, '==', 0);
48
49 # ->tzoffset?
50 {
51     local $ENV{TZ} = "EST";
52     my $lt = localtime;
53     cmp_ok(scalar($lt->tzoffset), 'eq', '-18000');
54     cmp_ok($lt->strftime("%z"), 'eq', '-0500');
55 }
56
57 cmp_ok(($t->julian_day / 2451604.0243 ) - 1, '<', 0.001);
58 cmp_ok(($t->mjd        /   51603.52426) - 1, '<', 0.001);
59 cmp_ok($t->week, '==', 9);
60
61 # strftime tests
62
63 # %a, %A, %b, %B, %c are locale-dependent
64
65 # %C is unportable: sometimes its like asctime(3) or date(1),
66 # sometimes it's the century (and whether for 2000 the century is
67 # 20 or 19, is fun, too..as far as I can read SUSv2 it should be 20.)
68 cmp_ok($t->strftime('%d'), '==', 29);
69
70 SKIP: {
71   skip "can't strftime %D, %R, %T or %e on Win32", 1 if $is_win32;
72   cmp_ok($t->strftime('%D'), 'eq', '02/29/00'); # Yech!
73 }
74 SKIP:{
75   skip "can't strftime %D, %R, %T or %e on Win32", 1 if $is_win32;
76   skip "can't strftime %e on QNX", 1 if $is_qnx;
77   cmp_ok($t->strftime('%e'), 'eq', '29');       # should test with < 10
78 }
79
80 # %h is locale-dependent
81 cmp_ok($t->strftime('%H'), 'eq', '12'); # should test with < 10
82
83 cmp_ok($t->strftime('%I'), 'eq', '12'); # should test with < 10
84 cmp_ok($t->strftime('%j'), '==',  60 ); # why ->yday+1 ?
85 cmp_ok($t->strftime('%M'), 'eq', '34'); # should test with < 10
86
87 # %p, %P, and %r are not widely implemented,
88 # and are possibly unportable (am or AM or a.m., and so on)
89
90 SKIP: {
91   skip "can't strftime %R on Win32 or QNX", 1 if $is_win32 or $is_qnx;
92   cmp_ok($t->strftime('%R'), 'eq', '12:34');    # should test with > 12
93 }
94
95 ok($t->strftime('%S') eq '56'); # should test with < 10
96
97 SKIP: {
98   skip "can't strftime %T on Win32", 1 if $is_win32;
99   cmp_ok($t->strftime('%T'), 'eq', '12:34:56'); # < 12 and > 12
100 }
101
102 # There are bugs in the implementation of %u in many platforms.
103 # (e.g. Linux seems to think, despite the man page, that %u
104 # 1-based on Sunday...)
105
106 cmp_ok($t->strftime('%U'), 'eq', '09'); # Sun cmp Mon
107
108 SKIP: {
109     skip "can't strftime %V on Win32 or QNX", 1 if $is_win32 or $is_qnx;
110     # is this test really broken on Mac OS? -- rjbs, 2006-02-08
111     cmp_ok($t->strftime('%V'), 'eq', '09'); # Sun cmp Mon
112 }
113
114 cmp_ok($t->strftime('%w'), '==', 2);
115 cmp_ok($t->strftime('%W'), 'eq', '09'); # Sun cmp Mon
116
117 # %x is locale and implementation dependent.
118
119 cmp_ok($t->strftime('%y'), '==', 0); # should test with 1999
120 cmp_ok($t->strftime('%Y'), 'eq', '2000');
121
122 # %Z is locale and implementation dependent
123 # (there is NO standard for timezone names)
124 cmp_ok($t->date(""), 'eq', '20000229');
125 cmp_ok($t->ymd("") , 'eq', '20000229');
126 cmp_ok($t->mdy("/"), 'eq', '02/29/2000');
127 cmp_ok($t->dmy("."), 'eq', '29.02.2000');
128 cmp_ok($t->date_separator, 'eq', '-');
129
130 $t->date_separator("/");
131 cmp_ok($t->date_separator, 'eq', '/');
132 cmp_ok($t->ymd,            'eq', '2000/02/29');
133
134 $t->date_separator("-");
135 cmp_ok($t->time_separator, 'eq', ':');
136 cmp_ok($t->hms("."),       'eq', '12.34.56');
137
138 $t->time_separator(".");
139 cmp_ok($t->time_separator, 'eq', '.');
140 cmp_ok($t->hms,            'eq', '12.34.56');
141
142 $t->time_separator(":");
143
144 my @fidays = qw( sunnuntai maanantai tiistai keskiviikko torstai
145                  perjantai lauantai );
146 my @frdays = qw( Dimanche Lundi Merdi Mercredi Jeudi Vendredi Samedi );
147
148 cmp_ok($t->day(@fidays), 'eq', "tiistai");
149 my @days = $t->day_list();
150
151 $t->day_list(@frdays);
152
153 cmp_ok($t->day, 'eq', "Merdi");
154
155 $t->day_list(@days);
156
157 cmp_ok($t->day, 'eq', "Tue");
158
159 my @months = $t->mon_list();
160
161 my @dumonths = qw(januari februari maart april mei juni
162                   juli augustus september oktober november december);
163
164 cmp_ok($t->month(@dumonths), 'eq', "februari");
165
166 $t->mon_list(@dumonths);
167
168 cmp_ok($t->month, 'eq', "februari");
169
170 $t->mon_list(@months);
171
172 cmp_ok($t->month, 'eq', "Feb");
173
174 cmp_ok(
175   $t->datetime(date => '/', T => ' ', time => '-'),
176   'eq',
177   "2000/02/29 12-34-56"
178 );
179
180 ok($t->is_leap_year); # should test more with different dates
181
182 cmp_ok($t->month_last_day, '==', 29); # test more
183
184 ok(!Time::Piece::_is_leap_year(1900));
185
186 ok(!Time::Piece::_is_leap_year(1901));
187
188 ok(Time::Piece::_is_leap_year(1904));
189
190 cmp_ok(Time::Piece->strptime("1945", "%Y")->year, '==', 1945, "Year is 1945?");
191
192 cmp_ok(Time::Piece->strptime("13:00", "%H:%M")->hour, '==', 13, "Hour is 13?");
193
194 # Test week number
195 # [from Ilya Martynov]
196 cmp_ok(Time::Piece->strptime("2002/06/10 0", '%Y/%m/%d %H')->week,  '==', 24);
197 cmp_ok(Time::Piece->strptime("2002/06/10 1", '%Y/%m/%d %H')->week,  '==', 24);
198 cmp_ok(Time::Piece->strptime("2002/06/10 2", '%Y/%m/%d %H')->week,  '==', 24);
199 cmp_ok(Time::Piece->strptime("2002/06/10 12", '%Y/%m/%d %H')->week, '==', 24);
200 cmp_ok(Time::Piece->strptime("2002/06/10 13", '%Y/%m/%d %H')->week, '==', 24);
201 cmp_ok(Time::Piece->strptime("2002/06/10 14", '%Y/%m/%d %H')->week, '==', 24);
202 cmp_ok(Time::Piece->strptime("2002/06/10 23", '%Y/%m/%d %H')->week, '==', 24);
203
204 # Test that strptime populates all relevant fields
205 cmp_ok(Time::Piece->strptime("2002/07/10", '%Y/%m/%d')->wday,  '==', 4);
206 cmp_ok(Time::Piece->strptime("2002/12/31", '%Y/%m/%d')->yday,  '==', 364);
207 cmp_ok(Time::Piece->strptime("2002/07/10", '%Y/%m/%d')->isdst, '==', 0);
208 cmp_ok(Time::Piece->strptime("2002/07/10", '%Y/%m/%d')->day_of_week, '==', 3);
209
210 cmp_ok(
211   Time::Piece->strptime("2000/02/29 12:34:56", '%Y/%m/%d %H:%M:%S')->epoch,
212   '==',
213   951827696
214 );