1use Test::More;
2
3# Skip if doing a regular install
4# Avoids mystery DST bugs [rt 128240], [GH40]
5plan skip_all => "DST tests not required for installation"
6  unless ( $ENV{AUTOMATED_TESTING} );
7
8my $is_win32 = ($^O =~ /Win32/);
9my $is_qnx = ($^O eq 'qnx');
10my $is_vos = ($^O eq 'vos');
11my $is_linux = ($^O =~ /linux/);
12my $is_bsd = ($^O =~ /bsd/);
13my $is_mac = ($^O =~ /darwin/);
14
15use Time::Piece;
16use Time::Seconds;
17
18#test using an epoch that can be DST
19#because sometimes funny stuff can occur [cpan #93095]
20#https://rt.cpan.org/Ticket/Display.html?id=93095#txn-1482590
21
22my $t = gmtime(1373371631); # 2013-07-09T12:07:11
23
24is($t->sec,               11);
25is($t->second,            11);
26is($t->min,               07);
27is($t->minute,            07);
28is($t->hour,              12);
29is($t->mday,               9);
30is($t->day_of_month,       9);
31is($t->mon,                7);
32is($t->_mon,               6);
33is($t->year,            2013);
34is($t->_year,            113);
35is($t->yy,              '13');
36
37cmp_ok($t->wday,        '==',         3);
38cmp_ok($t->_wday,       '==',         2);
39cmp_ok($t->day_of_week, '==',         2);
40cmp_ok($t->yday,        '==',        189);
41cmp_ok($t->day_of_year, '==',        189);
42
43# In GMT there should be no daylight savings ever.
44cmp_ok($t->isdst, '==', 0);
45cmp_ok($t->epoch, '==',   1373371631);
46cmp_ok($t->hms,   'eq',   '12:07:11');
47cmp_ok($t->time,  'eq',   '12:07:11');
48cmp_ok($t->ymd,   'eq', '2013-07-09');
49cmp_ok($t->date,  'eq', '2013-07-09');
50cmp_ok($t->mdy,   'eq', '07-09-2013');
51cmp_ok($t->dmy,   'eq', '09-07-2013');
52cmp_ok($t->cdate, 'eq', 'Tue Jul  9 12:07:11 2013');
53cmp_ok("$t",      'eq', 'Tue Jul  9 12:07:11 2013');
54cmp_ok($t->datetime, 'eq','2013-07-09T12:07:11');
55cmp_ok($t->daylight_savings, '==', 0);
56
57
58cmp_ok($t->week, '==', 28);
59
60# strftime tests
61
62# %a, %A, %b, %B, %c are locale-dependent
63
64# %C is unportable: sometimes its like asctime(3) or date(1),
65# sometimes it's the century (and whether for 2000 the century is
66# 20 or 19, is fun, too..as far as I can read SUSv2 it should be 20.)
67cmp_ok($t->strftime('%d'), '==', 9);
68
69SKIP: {
70  skip "can't strftime %D, %R, %T or %e on Win32", 1 if $is_win32;
71  cmp_ok($t->strftime('%D'), 'eq', '07/09/13'); # Yech!
72}
73SKIP:{
74  skip "can't strftime %D, %R, %T or %e on Win32", 1 if $is_win32;
75  skip "can't strftime %e on QNX", 1 if $is_qnx;
76  cmp_ok($t->strftime('%e'), 'eq', ' 9');       # should test with < 10
77}
78
79# %h is locale-dependent
80cmp_ok($t->strftime('%H'), 'eq', '12'); # should test with < 10
81
82cmp_ok($t->strftime('%I'), 'eq', '12'); # should test with < 10
83cmp_ok($t->strftime('%j'), '==',  190 ); # why ->yday+1 ?
84cmp_ok($t->strftime('%M'), 'eq', '07'); # should test with < 10
85
86# %p, %P, and %r are not widely implemented,
87# and are possibly unportable (am or AM or a.m., and so on)
88
89SKIP: {
90  skip "can't strftime %R on Win32 or QNX", 1 if $is_win32 or $is_qnx;
91  cmp_ok($t->strftime('%R'), 'eq', '12:07');    # should test with > 12
92}
93
94ok($t->strftime('%S') eq '11'); # should test with < 10
95
96SKIP: {
97  skip "can't strftime %T on Win32", 1 if $is_win32;
98  cmp_ok($t->strftime('%T'), 'eq', '12:07:11'); # < 12 and > 12
99}
100
101# There are bugs in the implementation of %u in many platforms.
102# (e.g. Linux seems to think, despite the man page, that %u
103# 1-based on Sunday...)
104
105cmp_ok($t->strftime('%U'), 'eq', '27'); # Sun cmp Mon
106
107SKIP: {
108    skip "can't strftime %V on Win32 or QNX or VOS", 1 if $is_win32 or $is_qnx or $is_vos;
109    # is this test really broken on Mac OS? -- rjbs, 2006-02-08
110    cmp_ok($t->strftime('%V'), 'eq', '28'); # Sun cmp Mon
111}
112
113cmp_ok($t->strftime('%w'), '==', 2);
114cmp_ok($t->strftime('%W'), 'eq', '27'); # Sun cmp Mon
115
116# %x is locale and implementation dependent.
117
118cmp_ok($t->strftime('%y'), '==', 13); # should test with 1999
119cmp_ok($t->strftime('%Y'), 'eq', '2013');
120
121ok(not $t->is_leap_year); # should test more with different dates
122
123cmp_ok($t->month_last_day, '==', 31); # test more
124
125
126SKIP: {
127	skip "Extra tests for Linux, BSD only.", 8 unless $is_linux or $is_mac or $is_bsd;
128
129    local $ENV{TZ} = "EST5EDT4,M3.2.0/2,M11.1.0/2";
130    Time::Piece::_tzset();
131    my $lt = localtime(1373371631); #2013-07-09T12:07:11
132    cmp_ok(scalar($lt->tzoffset), 'eq', '-14400');
133    cmp_ok($lt->strftime("%Y-%m-%d %H:%M:%S %Z"), 'eq', '2013-07-09 08:07:11 EDT');
134    like  ($lt->strftime("%z"), qr/-0400|EDT/); #windows: %Z and %z are the same
135    is    ($lt->strftime("%s"), 1373371631, 'Epoch output is the same with EDT');
136
137    $lt = localtime(1357733231); #2013-01-09T12:07:11
138    cmp_ok(scalar($lt->tzoffset), 'eq', '-18000');
139    cmp_ok($lt->strftime("%Y-%m-%d %H:%M:%S %Z"), 'eq', '2013-01-09 07:07:11 EST');
140    like  ($lt->strftime("%z"), qr/-0500|EST/);
141    is    ($lt->strftime("%s"), 1357733231, 'Epoch output is the same with EST');
142}
143
144done_testing(56);
145