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