3 use Test::MockTime qw(set_fixed_time restore_time);
6 use warnings; use strict;
7 use RT::Test tests => 172;
13 my $date = RT::Date->new(RT->SystemUser);
14 isa_ok($date, 'RT::Date', "constructor returned RT::Date oject");
15 $date = $date->new(RT->SystemUser);
16 isa_ok($date, 'RT::Date', "constructor returned RT::Date oject");
20 # set timezone in all places to UTC
21 RT->SystemUser->UserObj->__Set(Field => 'Timezone', Value => 'UTC')
22 if RT->SystemUser->UserObj->Timezone;
23 RT->Config->Set( Timezone => 'UTC' );
28 my $user = RT::User->new(RT->SystemUser);
29 my($uid, $msg) = $user->Create(
30 Name => "date_api". rand(200),
34 ok($uid, "user was created") or diag("error: $msg");
35 $current_user = RT::CurrentUser->new($user);
39 my $date = RT::Date->new( $current_user );
40 is($date->Timezone('user'), 'UTC', "dropped all timzones to UTC");
41 is($date->Timezone('server'), 'UTC', "dropped all timzones to UTC");
42 is($date->Timezone('unknown'), 'UTC', "with wrong context returns UTC");
44 $current_user->UserObj->__Set( Field => 'Timezone', Value => 'Europe/Moscow');
45 is($current_user->UserObj->Timezone,
47 "successfuly changed user's timezone");
48 is($date->Timezone('user'),
50 "in user context returns user's timezone");
51 is($date->Timezone('server'), 'UTC', "wasn't changed");
53 RT->Config->Set( Timezone => 'Africa/Ouagadougou' );
54 is($date->Timezone('server'),
56 "timezone of the RT server was changed");
57 is($date->Timezone('user'),
59 "in user context still returns user's timezone");
61 $current_user->UserObj->__Set( Field => 'Timezone', Value => '');
62 is_empty($current_user->UserObj->Timezone,
63 "successfuly changed user's timezone");
64 is($date->Timezone('user'),
66 "in user context returns timezone of the server if user's one is not defined");
68 RT->Config->Set( Timezone => 'GMT' );
69 is($date->Timezone('server'),
71 "timezone is GMT which one is alias for UTC");
73 RT->Config->Set( Timezone => '' );
74 is($date->Timezone('user'),
76 "user's and server's timzones are not defined, so UTC");
77 is($date->Timezone('server'),
79 "timezone of the server is not defined so UTC");
81 RT->Config->Set( Timezone => 'UTC' );
85 my $date = RT::Date->new(RT->SystemUser);
86 is($date->Unix, 0, "new date returns 0 in Unix format");
87 is($date->Get, '1970-01-01 00:00:00', "default is ISO format");
88 is($date->Get(Format =>'SomeBadFormat'),
89 '1970-01-01 00:00:00',
90 "don't know format, return ISO format");
91 is($date->Get(Format =>'W3CDTF'),
92 '1970-01-01T00:00:00Z',
93 "W3CDTF format with defaults");
94 is($date->Get(Format =>'RFC2822'),
95 'Thu, 1 Jan 1970 00:00:00 +0000',
96 "RFC2822 format with defaults");
97 is($date->Get(Format =>'LocalizedDateTime'),
98 'Thu, Jan 1, 1970 12:00:00 AM',
99 "LocalizedDateTime format with defaults");
101 is($date->ISO(Time => 0),
103 "ISO format without time part");
104 is($date->W3CDTF(Time => 0),
106 "W3CDTF format without time part");
107 is($date->RFC2822(Time => 0),
109 "RFC2822 format without time part");
110 is($date->LocalizedDateTime(Time => 0),
112 "LocalizedDateTime format without time part");
114 is($date->ISO(Date => 0),
116 "ISO format without date part");
117 is($date->W3CDTF(Date => 0),
118 '1970-01-01T00:00:00Z',
119 "W3CDTF format is incorrect without date part");
120 is($date->RFC2822(Date => 0),
122 "RFC2822 format without date part");
123 is($date->LocalizedDateTime(Date => 0),
125 "LocalizedDateTime format without date part");
127 is($date->ISO(Date => 0, Seconds => 0),
129 "ISO format without date part and seconds");
130 is($date->W3CDTF(Date => 0, Seconds => 0),
132 "W3CDTF format without seconds, but we ship date part even if Date is false");
133 is($date->RFC2822(Date => 0, Seconds => 0),
135 "RFC2822 format without date part and seconds");
137 is($date->RFC2822(DayOfWeek => 0),
138 '1 Jan 1970 00:00:00 +0000',
139 "RFC2822 format without 'day of week' part");
140 is($date->RFC2822(DayOfWeek => 0, Date => 0),
142 "RFC2822 format without 'day of week' and date parts(corner case test)");
144 is($date->LocalizedDateTime(AbbrDay => 0),
145 'Thursday, Jan 1, 1970 12:00:00 AM',
146 "LocalizedDateTime format without abbreviation of day");
147 is($date->LocalizedDateTime(AbbrMonth => 0),
148 'Thu, January 1, 1970 12:00:00 AM',
149 "LocalizedDateTime format without abbreviation of month");
150 is($date->LocalizedDateTime(DateFormat => 'date_format_short'),
151 '1/1/70 12:00:00 AM',
152 "LocalizedDateTime format with non default DateFormat");
153 is($date->LocalizedDateTime(TimeFormat => 'time_format_short'),
154 'Thu, Jan 1, 1970 12:00 AM',
155 "LocalizedDateTime format with non default TimeFormat");
159 "the default format for the 'Date' method is ISO");
160 is($date->Date(Format => 'W3CDTF'),
162 "'Date' method, W3CDTF format");
163 is($date->Date(Format => 'RFC2822'),
165 "'Date' method, RFC2822 format");
166 is($date->Date(Time => 1),
168 "'Date' method doesn't pass through 'Time' argument");
169 is($date->Date(Date => 0),
171 "'Date' method overrides 'Date' argument");
175 "the default format for the 'Time' method is ISO");
176 is($date->Time(Format => 'W3CDTF'),
177 '1970-01-01T00:00:00Z',
178 "'Time' method, W3CDTF format, date part is required by w3c doc");
179 is($date->Time(Format => 'RFC2822'),
181 "'Time' method, RFC2822 format");
182 is($date->Time(Date => 1),
184 "'Time' method doesn't pass through 'Date' argument");
185 is($date->Time(Time => 0),
187 "'Time' method overrides 'Time' argument");
190 '1970-01-01 00:00:00',
191 "the default format for the 'DateTime' method is ISO");
192 is($date->DateTime(Format =>'W3CDTF'),
193 '1970-01-01T00:00:00Z',
194 "'DateTime' method, W3CDTF format");
195 is($date->DateTime(Format =>'RFC2822'),
196 'Thu, 1 Jan 1970 00:00:00 +0000',
197 "'DateTime' method, RFC2822 format");
198 is($date->DateTime(Date => 0, Time => 0),
199 '1970-01-01 00:00:00',
200 "the 'DateTime' method overrides both 'Date' and 'Time' arguments");
204 { # positive timezone
205 $current_user->UserObj->__Set( Field => 'Timezone', Value => 'Europe/Moscow');
206 my $date = RT::Date->new( $current_user );
207 $date->Set( Format => 'ISO', Timezone => 'utc', Value => '2005-01-01 15:10:00' );
208 is($date->ISO( Timezone => 'user' ), '2005-01-01 18:10:00', "ISO");
209 is($date->W3CDTF( Timezone => 'user' ), '2005-01-01T18:10:00+03:00', "W3C DTF");
210 is($date->RFC2822( Timezone => 'user' ), 'Sat, 1 Jan 2005 18:10:00 +0300', "RFC2822");
213 $date = RT::Date->new( $current_user );
214 $date->Set( Format => 'ISO', Timezone => 'utc', Value => '2005-07-01 15:10:00' );
215 is($date->ISO( Timezone => 'user' ), '2005-07-01 19:10:00', "ISO");
216 is($date->W3CDTF( Timezone => 'user' ), '2005-07-01T19:10:00+04:00', "W3C DTF");
217 is($date->RFC2822( Timezone => 'user' ), 'Fri, 1 Jul 2005 19:10:00 +0400', "RFC2822");
220 { # negative timezone
221 $current_user->UserObj->__Set( Field => 'Timezone', Value => 'America/New_York');
222 my $date = RT::Date->new( $current_user );
223 $date->Set( Format => 'ISO', Timezone => 'utc', Value => '2005-01-01 15:10:00' );
224 is($date->ISO( Timezone => 'user' ), '2005-01-01 10:10:00', "ISO");
225 is($date->W3CDTF( Timezone => 'user' ), '2005-01-01T10:10:00-05:00', "W3C DTF");
226 is($date->RFC2822( Timezone => 'user' ), 'Sat, 1 Jan 2005 10:10:00 -0500', "RFC2822");
229 $date = RT::Date->new( $current_user );
230 $date->Set( Format => 'ISO', Timezone => 'utc', Value => '2005-07-01 15:10:00' );
231 is($date->ISO( Timezone => 'user' ), '2005-07-01 11:10:00', "ISO");
232 is($date->W3CDTF( Timezone => 'user' ), '2005-07-01T11:10:00-04:00', "W3C DTF");
233 is($date->RFC2822( Timezone => 'user' ), 'Fri, 1 Jul 2005 11:10:00 -0400', "RFC2822");
238 my $date = RT::Date->new(RT->SystemUser);
239 $date->Set( Format => 'bad' );
240 is($date->Unix, 0, "bad format");
241 } qr{Unknown Date format: bad};
244 { # setting value via Unix method
245 my $date = RT::Date->new(RT->SystemUser);
247 is($date->ISO, '1970-01-01 00:00:01', "correct value");
249 foreach (undef, 0, ''){
251 is($date->ISO, '1970-01-01 00:00:01', "correct value");
253 $date->Set(Format => 'unix', Value => $_);
254 is($date->ISO, '1970-01-01 00:00:00', "Set a date to midnight 1/1/1970 GMT due to wrong call");
255 is($date->Unix, 0, "unix is 0 => unset");
259 my $year = (localtime(time))[5] + 1900;
262 my $date = RT::Date->new(RT->SystemUser);
264 $date->Set(Format => 'ISO', Value => 'weird date');
265 } qr/Couldn't parse date 'weird date' as a ISO format/;
266 is($date->Unix, 0, "date was wrong => unix == 0");
268 # XXX: ISO format has more feature than we suport
269 # http://www.cl.cam.ac.uk/~mgk25/iso-time.html
271 $date->Set(Format => 'ISO', Value => '2005-11-28 15:10:00');
272 is($date->ISO, '2005-11-28 15:10:00', "YYYY-DD-MM hh:mm:ss");
274 $date->Set(Format => 'ISO', Value => '2005-11-28 15:10:00+00');
275 is($date->ISO, '2005-11-28 15:10:00', "YYYY-DD-MM hh:mm:ss+00");
277 $date->Set(Format => 'ISO', Value => '11-28 15:10:00');
278 is($date->ISO, $year .'-11-28 15:10:00', "DD-MM hh:mm:ss");
280 $date->Set(Format => 'ISO', Value => '11-28 15:10:00+00');
281 is($date->ISO, $year .'-11-28 15:10:00', "DD-MM hh:mm:ss+00");
283 $date->Set(Format => 'ISO', Value => '20051128151000');
284 is($date->ISO, '2005-11-28 15:10:00', "YYYYDDMMhhmmss");
286 $date->Set(Format => 'ISO', Value => '1128151000');
287 is($date->ISO, $year .'-11-28 15:10:00', "DDMMhhmmss");
289 $date->Set(Format => 'ISO', Value => '2005112815:10:00');
290 is($date->ISO, '2005-11-28 15:10:00', "YYYYDDMMhh:mm:ss");
292 $date->Set(Format => 'ISO', Value => '112815:10:00');
293 is($date->ISO, $year .'-11-28 15:10:00', "DDMMhh:mm:ss");
295 $date->Set(Format => 'ISO', Value => '2005-13-28 15:10:00');
296 is($date->Unix, 0, "wrong month value");
298 $date->Set(Format => 'ISO', Value => '2005-00-28 15:10:00');
299 is($date->Unix, 0, "wrong month value");
301 $date->Set(Format => 'ISO', Value => '1960-01-28 15:10:00');
302 is($date->Unix, 0, "too old, we don't support");
305 { # set+datemanip format(Time::ParseDate)
306 my $date = RT::Date->new(RT->SystemUser);
307 $date->Set(Format => 'unknown', Value => 'weird date');
308 is($date->Unix, 0, "date was wrong");
310 RT->Config->Set( Timezone => 'Europe/Moscow' );
311 $date->Set(Format => 'datemanip', Value => '2005-11-28 15:10:00');
312 is($date->ISO, '2005-11-28 12:10:00', "YYYY-DD-MM hh:mm:ss");
314 RT->Config->Set( Timezone => 'UTC' );
315 $date->Set(Format => 'datemanip', Value => '2005-11-28 15:10:00');
316 is($date->ISO, '2005-11-28 15:10:00', "YYYY-DD-MM hh:mm:ss");
318 $current_user->UserObj->__Set( Field => 'Timezone', Value => 'Europe/Moscow');
319 $date = RT::Date->new( $current_user );
320 $date->Set(Format => 'datemanip', Value => '2005-11-28 15:10:00');
321 is($date->ISO, '2005-11-28 12:10:00', "YYYY-DD-MM hh:mm:ss");
324 { # set+unknown format(Time::ParseDate)
325 my $date = RT::Date->new(RT->SystemUser);
326 $date->Set(Format => 'unknown', Value => 'weird date');
327 is($date->Unix, 0, "date was wrong");
329 RT->Config->Set( Timezone => 'Europe/Moscow' );
330 $date->Set(Format => 'unknown', Value => '2005-11-28 15:10:00');
331 is($date->ISO, '2005-11-28 12:10:00', "YYYY-DD-MM hh:mm:ss");
333 $date->Set(Format => 'unknown', Value => '2005-11-28 15:10:00', Timezone => 'utc' );
334 is($date->ISO, '2005-11-28 15:10:00', "YYYY-DD-MM hh:mm:ss");
336 # test relative dates
338 set_fixed_time("2005-11-28T15:10:00Z");
339 $date->Set(Format => 'unknown', Value => 'now');
340 is($date->ISO, '2005-11-28 15:10:00', "YYYY-DD-MM hh:mm:ss");
342 $date->Set(Format => 'unknown', Value => '1 day ago');
343 is($date->ISO, '2005-11-27 15:10:00', "YYYY-DD-MM hh:mm:ss");
347 RT->Config->Set( Timezone => 'UTC' );
348 $date->Set(Format => 'unknown', Value => '2005-11-28 15:10:00');
349 is($date->ISO, '2005-11-28 15:10:00', "YYYY-DD-MM hh:mm:ss");
351 $current_user->UserObj->__Set( Field => 'Timezone', Value => 'Europe/Moscow');
352 $date = RT::Date->new( $current_user );
353 $date->Set(Format => 'unknown', Value => '2005-11-28 15:10:00');
354 is($date->ISO, '2005-11-28 12:10:00', "YYYY-DD-MM hh:mm:ss");
355 $date->Set(Format => 'unknown', Value => '2005-11-28 15:10:00', Timezone => 'server' );
356 is($date->ISO, '2005-11-28 15:10:00', "YYYY-DD-MM hh:mm:ss");
357 $date->Set(Format => 'unknown', Value => '2005-11-28 15:10:00', Timezone => 'utc' );
358 is($date->ISO, '2005-11-28 15:10:00', "YYYY-DD-MM hh:mm:ss");
362 my $date = RT::Date->new(RT->SystemUser);
364 RT->Config->Set( Timezone => 'Europe/Moscow' );
365 $date->Set(Format => 'ISO', Value => '2005-11-28 15:10:00');
366 $date->SetToMidnight;
367 is($date->ISO, '2005-11-28 00:00:00', "default is utc");
368 $date->Set(Format => 'ISO', Value => '2005-11-28 15:10:00');
369 $date->SetToMidnight(Timezone => 'utc');
370 is($date->ISO, '2005-11-28 00:00:00', "utc context");
371 $date->Set(Format => 'ISO', Value => '2005-11-28 15:10:00');
372 $date->SetToMidnight(Timezone => 'user');
373 is($date->ISO, '2005-11-27 21:00:00', "user context, user has no preference, fallback to server");
374 $date->Set(Format => 'ISO', Value => '2005-11-28 15:10:00');
375 $date->SetToMidnight(Timezone => 'server');
376 is($date->ISO, '2005-11-27 21:00:00', "server context");
378 $current_user->UserObj->__Set( Field => 'Timezone', Value => 'Europe/Moscow');
379 $date = RT::Date->new( $current_user );
380 $date->Set(Format => 'ISO', Value => '2005-11-28 15:10:00');
381 $date->SetToMidnight;
382 is($date->ISO, '2005-11-28 00:00:00', "default is utc");
383 $date->Set(Format => 'ISO', Value => '2005-11-28 15:10:00');
384 $date->SetToMidnight(Timezone => 'utc');
385 is($date->ISO, '2005-11-28 00:00:00', "utc context");
386 $date->Set(Format => 'ISO', Value => '2005-11-28 15:10:00');
387 $date->SetToMidnight(Timezone => 'user');
388 is($date->ISO, '2005-11-27 21:00:00', "user context");
389 $date->SetToMidnight(Timezone => 'server');
390 is($date->ISO, '2005-11-27 21:00:00', "server context");
392 RT->Config->Set( Timezone => 'UTC' );
396 my $date = RT::Date->new(RT->SystemUser);
399 ok($date->Unix >= $time, 'close enough');
400 ok($date->Unix < $time+5, 'difference is less than five seconds');
404 my $date = RT::Date->new(RT->SystemUser);
408 is($date->ISO, '1970-01-01 00:00:00', "nothing changed");
409 $date->AddSeconds(0);
410 is($date->ISO, '1970-01-01 00:00:00', "nothing changed");
413 $date->AddSeconds(5);
414 is($date->ISO, '1970-01-01 00:00:05', "added five seconds");
415 $date->AddSeconds(-2);
416 is($date->ISO, '1970-01-01 00:00:03', "substracted two seconds");
419 $date->AddSeconds(3661);
420 is($date->ISO, '1970-01-01 01:01:01', "added one hour, minute and a second");
422 # XXX: TODO, doesn't work with Test::Warn
424 # local $TODO = "BUG or subject to change Date handling to support unix time <= 0";
426 # $date->AddSeconds(-2);
427 # ok($date->Unix > 0);
432 is($date->ISO, '1970-01-02 00:00:00', "added one day");
434 is($date->ISO, '1970-01-04 00:00:00', "added two days");
436 is($date->ISO, '1970-01-03 00:00:00', "substructed one day");
440 is($date->ISO, '1970-02-01 00:00:00', "added one month");
444 $current_user->UserObj->__Set( Field => 'Timezone', Value => '');
445 my $date = RT::Date->new( $current_user );
446 is($date->AsString, "Not set", "AsString returns 'Not set'");
448 RT->Config->Set( DateTimeFormat => '');
450 is($date->AsString, 'Thu Jan 01 00:00:01 1970', "correct string");
451 is($date->AsString(Date => 0), '00:00:01', "correct string");
452 is($date->AsString(Time => 0), 'Thu Jan 01 1970', "correct string");
453 is($date->AsString(Date => 0, Time => 0), 'Thu Jan 01 00:00:01 1970', "invalid input");
455 RT->Config->Set( DateTimeFormat => 'RFC2822' );
457 is($date->AsString, 'Thu, 1 Jan 1970 00:00:01 +0000', "correct string");
459 RT->Config->Set( DateTimeFormat => { Format => 'RFC2822', Seconds => 0 } );
461 is($date->AsString, 'Thu, 1 Jan 1970 00:00 +0000', "correct string");
462 is($date->AsString(Seconds => 1), 'Thu, 1 Jan 1970 00:00:01 +0000', "correct string");
466 my $date = RT::Date->new(RT->SystemUser);
468 is($date->DurationAsString(1), '1 sec', '1 sec');
469 is($date->DurationAsString(59), '59 sec', '59 sec');
470 is($date->DurationAsString(60), '1 min', '1 min');
471 is($date->DurationAsString(60*119), '119 min', '119 min');
472 is($date->DurationAsString(60*60*2-1), '120 min', '120 min');
473 is($date->DurationAsString(60*60*2), '2 hours', '2 hours');
474 is($date->DurationAsString(60*60*48-1), '48 hours', '48 hours');
475 is($date->DurationAsString(60*60*48), '2 days', '2 days');
476 is($date->DurationAsString(60*60*24*14-1), '14 days', '14 days');
477 is($date->DurationAsString(60*60*24*14), '2 weeks', '2 weeks');
478 is($date->DurationAsString(60*60*24*7*8-1), '8 weeks', '8 weeks');
479 is($date->DurationAsString(60*60*24*61), '2 months', '2 months');
480 is($date->DurationAsString(60*60*24*365-1), '12 months', '12 months');
481 is($date->DurationAsString(60*60*24*366), '1 years', '1 years');
483 is($date->DurationAsString(-1), '1 sec ago', '1 sec ago');
487 my $date = RT::Date->new(RT->SystemUser);
488 is($date->DiffAsString(1), '', 'no diff, wrong input');
489 is($date->DiffAsString(-1), '', 'no diff, wrong input');
490 is($date->DiffAsString('qwe'), '', 'no diff, wrong input');
493 is($date->DiffAsString(-1), '', 'no diff, wrong input');
495 is($date->DiffAsString(3), '1 sec ago', 'diff: 1 sec ago');
496 is($date->DiffAsString(1), '1 sec', 'diff: 1 sec');
498 my $ndate = RT::Date->new(RT->SystemUser);
499 is($date->DiffAsString($ndate), '', 'no diff, wrong input');
501 is($date->DiffAsString($ndate), '1 sec ago', 'diff: 1 sec ago');
505 my $date = RT::Date->new(RT->SystemUser);
507 my $diff = $date->Diff;
508 ok($diff <= 0, 'close enought');
509 ok($diff > -5, 'close enought');
513 my $date = RT::Date->new(RT->SystemUser);
515 my $diff = $date->AgeAsString;
516 like($diff, qr/^(0 sec|[1-5] sec ago)$/, 'close enought');
520 my $date = RT::Date->new(RT->SystemUser);
521 is($date->GetWeekday(7), '', '7 and greater are invalid');
522 is($date->GetWeekday(6), 'Sat', '6 is Saturday');
523 is($date->GetWeekday(0), 'Sun', '0 is Sunday');
524 is($date->GetWeekday(-1), 'Sat', '-1 is Saturday');
525 is($date->GetWeekday(-7), 'Sun', '-7 is Sunday');
526 is($date->GetWeekday(-8), '', '-8 and lesser are invalid');
530 my $date = RT::Date->new(RT->SystemUser);
531 is($date->GetMonth(12), '', '12 and greater are invalid');
532 is($date->GetMonth(11), 'Dec', '11 is December');
533 is($date->GetMonth(0), 'Jan', '0 is January');
534 is($date->GetMonth(-1), 'Dec', '11 is December');
535 is($date->GetMonth(-12), 'Jan', '0 is January');
536 is($date->GetMonth(-13), '', '-13 and lesser are invalid');
540 # set unknown format: edge cases
541 my $date = RT::Date->new(RT->SystemUser);
542 $date->Set( Value => 0, Format => 'unknown' );
543 is( $date->Unix(), 0, "unix is 0 with Value => 0, Format => 'unknown'" );
545 $date->Set( Value => '', Format => 'unknown' );
546 is( $date->Unix(), 0, "unix is 0 with Value => '', Format => 'unknown'" );
548 $date->Set( Value => ' ', Format => 'unknown' );
549 is( $date->Unix(), 0, "unix is 0 with Value => ' ', Format => 'unknown'" );
553 #TODO: RFC2822, W3CDTF with Timezones