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