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