1 use Test::More tests => 95;
3 my $is_win32 = ($^O =~ /Win32/);
4 my $is_qnx = ($^O eq 'qnx');
5 BEGIN { use_ok('Time::Piece'); }
8 my $t = gmtime(951827696); # 2000-02-29T12:34:56
16 is($t->day_of_month, 29);
19 is($t->monname, 'Feb');
21 is($t->fullmonth, 'February');
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);
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);
50 my $is_pseudo_fork = 0;
51 if (defined &Win32::GetCurrentProcessId
52 ? $$ != Win32::GetCurrentProcessId() : $^O eq "MSWin32" && $$ < 0) {
56 skip "can't register TZ changes in a pseudo-fork", 2 if $is_pseudo_fork;
57 local $ENV{TZ} = "EST5";
58 Time::Piece::_tzset(); # register the environment change
60 cmp_ok(scalar($lt->tzoffset), 'eq', '-18000');
61 cmp_ok($lt->strftime("%Z"), 'eq', 'EST');
64 cmp_ok(($t->julian_day / 2451604.0243 ) - 1, '<', 0.001);
65 cmp_ok(($t->mjd / 51603.52426) - 1, '<', 0.001);
66 cmp_ok($t->week, '==', 9);
70 # %a, %A, %b, %B, %c are locale-dependent
72 # %C is unportable: sometimes its like asctime(3) or date(1),
73 # sometimes it's the century (and whether for 2000 the century is
74 # 20 or 19, is fun, too..as far as I can read SUSv2 it should be 20.)
75 cmp_ok($t->strftime('%d'), '==', 29);
78 skip "can't strftime %D, %R, %T or %e on Win32", 1 if $is_win32;
79 cmp_ok($t->strftime('%D'), 'eq', '02/29/00'); # Yech!
82 skip "can't strftime %D, %R, %T or %e on Win32", 1 if $is_win32;
83 skip "can't strftime %e on QNX", 1 if $is_qnx;
84 cmp_ok($t->strftime('%e'), 'eq', '29'); # should test with < 10
87 # %h is locale-dependent
88 cmp_ok($t->strftime('%H'), 'eq', '12'); # should test with < 10
90 cmp_ok($t->strftime('%I'), 'eq', '12'); # should test with < 10
91 cmp_ok($t->strftime('%j'), '==', 60 ); # why ->yday+1 ?
92 cmp_ok($t->strftime('%M'), 'eq', '34'); # should test with < 10
94 # %p, %P, and %r are not widely implemented,
95 # and are possibly unportable (am or AM or a.m., and so on)
98 skip "can't strftime %R on Win32 or QNX", 1 if $is_win32 or $is_qnx;
99 cmp_ok($t->strftime('%R'), 'eq', '12:34'); # should test with > 12
102 ok($t->strftime('%S') eq '56'); # should test with < 10
105 skip "can't strftime %T on Win32", 1 if $is_win32;
106 cmp_ok($t->strftime('%T'), 'eq', '12:34:56'); # < 12 and > 12
109 # There are bugs in the implementation of %u in many platforms.
110 # (e.g. Linux seems to think, despite the man page, that %u
111 # 1-based on Sunday...)
113 cmp_ok($t->strftime('%U'), 'eq', '09'); # Sun cmp Mon
116 skip "can't strftime %V on Win32 or QNX", 1 if $is_win32 or $is_qnx;
117 # is this test really broken on Mac OS? -- rjbs, 2006-02-08
118 cmp_ok($t->strftime('%V'), 'eq', '09'); # Sun cmp Mon
121 cmp_ok($t->strftime('%w'), '==', 2);
122 cmp_ok($t->strftime('%W'), 'eq', '09'); # Sun cmp Mon
124 # %x is locale and implementation dependent.
126 cmp_ok($t->strftime('%y'), '==', 0); # should test with 1999
127 cmp_ok($t->strftime('%Y'), 'eq', '2000');
129 # %Z is locale and implementation dependent
130 # (there is NO standard for timezone names)
131 cmp_ok($t->date(""), 'eq', '20000229');
132 cmp_ok($t->ymd("") , 'eq', '20000229');
133 cmp_ok($t->mdy("/"), 'eq', '02/29/2000');
134 cmp_ok($t->dmy("."), 'eq', '29.02.2000');
135 cmp_ok($t->date_separator, 'eq', '-');
137 $t->date_separator("/");
138 cmp_ok($t->date_separator, 'eq', '/');
139 cmp_ok($t->ymd, 'eq', '2000/02/29');
141 $t->date_separator("-");
142 cmp_ok($t->time_separator, 'eq', ':');
143 cmp_ok($t->hms("."), 'eq', '12.34.56');
145 $t->time_separator(".");
146 cmp_ok($t->time_separator, 'eq', '.');
147 cmp_ok($t->hms, 'eq', '12.34.56');
149 $t->time_separator(":");
151 my @fidays = qw( sunnuntai maanantai tiistai keskiviikko torstai
152 perjantai lauantai );
153 my @frdays = qw( Dimanche Lundi Merdi Mercredi Jeudi Vendredi Samedi );
155 cmp_ok($t->day(@fidays), 'eq', "tiistai");
156 my @days = $t->day_list();
158 $t->day_list(@frdays);
160 cmp_ok($t->day, 'eq', "Merdi");
164 cmp_ok($t->day, 'eq', "Tue");
166 my @months = $t->mon_list();
168 my @dumonths = qw(januari februari maart april mei juni
169 juli augustus september oktober november december);
171 cmp_ok($t->month(@dumonths), 'eq', "februari");
173 $t->mon_list(@dumonths);
175 cmp_ok($t->month, 'eq', "februari");
177 $t->mon_list(@months);
179 cmp_ok($t->month, 'eq', "Feb");
182 $t->datetime(date => '/', T => ' ', time => '-'),
184 "2000/02/29 12-34-56"
187 ok($t->is_leap_year); # should test more with different dates
189 cmp_ok($t->month_last_day, '==', 29); # test more
191 ok(!Time::Piece::_is_leap_year(1900));
193 ok(!Time::Piece::_is_leap_year(1901));
195 ok(Time::Piece::_is_leap_year(1904));
197 cmp_ok(Time::Piece->strptime("1945", "%Y")->year, '==', 1945, "Year is 1945?");
199 cmp_ok(Time::Piece->strptime("13:00", "%H:%M")->hour, '==', 13, "Hour is 13?");
202 # [from Ilya Martynov]
203 cmp_ok(Time::Piece->strptime("2002/06/10 0", '%Y/%m/%d %H')->week, '==', 24);
204 cmp_ok(Time::Piece->strptime("2002/06/10 1", '%Y/%m/%d %H')->week, '==', 24);
205 cmp_ok(Time::Piece->strptime("2002/06/10 2", '%Y/%m/%d %H')->week, '==', 24);
206 cmp_ok(Time::Piece->strptime("2002/06/10 12", '%Y/%m/%d %H')->week, '==', 24);
207 cmp_ok(Time::Piece->strptime("2002/06/10 13", '%Y/%m/%d %H')->week, '==', 24);
208 cmp_ok(Time::Piece->strptime("2002/06/10 14", '%Y/%m/%d %H')->week, '==', 24);
209 cmp_ok(Time::Piece->strptime("2002/06/10 23", '%Y/%m/%d %H')->week, '==', 24);
211 # Test that strptime populates all relevant fields
212 cmp_ok(Time::Piece->strptime("2002/07/10", '%Y/%m/%d')->wday, '==', 4);
213 cmp_ok(Time::Piece->strptime("2002/12/31", '%Y/%m/%d')->yday, '==', 364);
214 cmp_ok(Time::Piece->strptime("2002/07/10", '%Y/%m/%d')->isdst, '==', 0);
215 cmp_ok(Time::Piece->strptime("2002/07/10", '%Y/%m/%d')->day_of_week, '==', 3);
218 Time::Piece->strptime("2000/02/29 12:34:56", '%Y/%m/%d %H:%M:%S')->epoch,