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