RT 4.2.11, ticket#13852
[freeside.git] / rt / t / api / date.t
1
2 use Test::MockTime qw(set_fixed_time restore_time);
3 use DateTime;
4
5 use warnings;
6 use strict;
7 use RT::Test tests => undef;
8 use RT::User;
9 use Test::Warn;
10
11 use_ok('RT::Date');
12 {
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");
17 }
18
19 {
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' );
24 }
25
26 my $current_user;
27 {
28     my $user = RT::User->new(RT->SystemUser);
29     my($uid, $msg) = $user->Create(
30         Name       => "date_api". rand(200),
31         Lang       => 'en',
32         Privileged => 1,
33     );
34     ok($uid, "user was created") or diag("error: $msg");
35     $current_user = RT::CurrentUser->new($user);
36 }
37
38 {
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");
43
44     $current_user->UserObj->__Set( Field => 'Timezone', Value => 'Europe/Moscow');
45     is($current_user->UserObj->Timezone,
46        'Europe/Moscow',
47        "successfuly changed user's timezone");
48     is($date->Timezone('user'),
49        'Europe/Moscow',
50        "in user context returns user's timezone");
51     is($date->Timezone('server'), 'UTC', "wasn't changed");
52
53     RT->Config->Set( Timezone => 'Africa/Ouagadougou' );
54     is($date->Timezone('server'),
55        'Africa/Ouagadougou',
56        "timezone of the RT server was changed");
57     is($date->Timezone('user'),
58        'Europe/Moscow',
59        "in user context still returns user's timezone");
60
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'),
65        'Africa/Ouagadougou',
66        "in user context returns timezone of the server if user's one is not defined");
67
68     RT->Config->Set( Timezone => 'GMT' );
69     is($date->Timezone('server'),
70        'UTC',
71        "timezone is GMT which one is alias for UTC");
72
73     RT->Config->Set( Timezone => '' );
74     is($date->Timezone('user'),
75        'UTC',
76        "user's and server's timzones are not defined, so UTC");
77     is($date->Timezone('server'),
78        'UTC',
79        "timezone of the server is not defined so UTC");
80
81     RT->Config->Set( Timezone => 'UTC' );
82 }
83
84 {
85     my $date = RT::Date->new(RT->SystemUser);
86     is($date->IsSet,0, "new date isn't set");
87     is($date->Unix, 0, "new date returns 0 in Unix format");
88     is($date->Get, '1970-01-01 00:00:00', "default is ISO format");
89     warning_like {
90         is($date->Get(Format =>'SomeBadFormat'),
91            '1970-01-01 00:00:00',
92            "don't know format, return ISO format");
93     } qr/Invalid date formatter/;
94     is($date->Get(Format =>'W3CDTF'),
95        '1970-01-01T00:00:00Z',
96        "W3CDTF format with defaults");
97     is($date->Get(Format =>'RFC2822'),
98        'Thu, 01 Jan 1970 00:00:00 +0000',
99        "RFC2822 format with defaults");
100     is($date->Get(Format =>'LocalizedDateTime'),
101        'Thu, Jan 1, 1970 12:00:00 AM',
102        "LocalizedDateTime format with defaults");
103
104     is($date->ISO(Time => 0),
105        '1970-01-01',
106        "ISO format without time part");
107     is($date->W3CDTF(Time => 0),
108        '1970-01-01',
109        "W3CDTF format without time part");
110     is($date->RFC2822(Time => 0),
111        'Thu, 01 Jan 1970',
112        "RFC2822 format without time part");
113     is($date->LocalizedDateTime(Time => 0),
114        'Thu, Jan 1, 1970',
115        "LocalizedDateTime format without time part");
116
117     is($date->ISO(Date => 0),
118        '00:00:00',
119        "ISO format without date part");
120     is($date->W3CDTF(Date => 0),
121        '1970-01-01T00:00:00Z',
122        "W3CDTF format is incorrect without date part");
123     is($date->RFC2822(Date => 0),
124        '00:00:00 +0000',
125        "RFC2822 format without date part");
126     is($date->LocalizedDateTime(Date => 0),
127        '12:00:00 AM',
128        "LocalizedDateTime format without date part");
129
130     is($date->ISO(Date => 0, Seconds => 0),
131        '00:00',
132        "ISO format without date part and seconds");
133     is($date->W3CDTF(Date => 0, Seconds => 0),
134        '1970-01-01T00:00Z',
135        "W3CDTF format without seconds, but we ship date part even if Date is false");
136     is($date->RFC2822(Date => 0, Seconds => 0),
137        '00:00 +0000',
138        "RFC2822 format without date part and seconds");
139
140     is($date->RFC2822(DayOfWeek => 0),
141        '01 Jan 1970 00:00:00 +0000',
142        "RFC2822 format without 'day of week' part");
143     is($date->RFC2822(DayOfWeek => 0, Date => 0),
144        '00:00:00 +0000',
145        "RFC2822 format without 'day of week' and date parts(corner case test)");
146
147     is($date->LocalizedDateTime(AbbrDay => 0),
148        'Thursday, Jan 1, 1970 12:00:00 AM',
149        "LocalizedDateTime format without abbreviation of day");
150     is($date->LocalizedDateTime(AbbrMonth => 0),
151        'Thu, January 1, 1970 12:00:00 AM',
152        "LocalizedDateTime format without abbreviation of month");
153     is($date->LocalizedDateTime(DateFormat => 'date_format_short'),
154        '1/1/70 12:00:00 AM',
155        "LocalizedDateTime format with non default DateFormat");
156     is($date->LocalizedDateTime(TimeFormat => 'time_format_short'),
157        'Thu, Jan 1, 1970 12:00 AM',
158        "LocalizedDateTime format with non default TimeFormat");
159
160     is($date->Date,
161        '1970-01-01',
162        "the default format for the 'Date' method is ISO");
163     is($date->Date(Format => 'W3CDTF'),
164        '1970-01-01',
165        "'Date' method, W3CDTF format");
166     is($date->Date(Format => 'RFC2822'),
167        'Thu, 01 Jan 1970',
168        "'Date' method, RFC2822 format");
169     is($date->Date(Time => 1),
170        '1970-01-01',
171        "'Date' method doesn't pass through 'Time' argument");
172     is($date->Date(Date => 0),
173        '1970-01-01',
174        "'Date' method overrides 'Date' argument");
175
176     is($date->Time,
177        '00:00:00',
178        "the default format for the 'Time' method is ISO");
179     is($date->Time(Format => 'W3CDTF'),
180        '1970-01-01T00:00:00Z',
181        "'Time' method, W3CDTF format, date part is required by w3c doc");
182     is($date->Time(Format => 'RFC2822'),
183        '00:00:00 +0000',
184        "'Time' method, RFC2822 format");
185     is($date->Time(Date => 1),
186        '00:00:00',
187        "'Time' method doesn't pass through 'Date' argument");
188     is($date->Time(Time => 0),
189        '00:00:00',
190        "'Time' method overrides 'Time' argument");
191
192     is($date->DateTime,
193        '1970-01-01 00:00:00',
194        "the default format for the 'DateTime' method is ISO");
195     is($date->DateTime(Format =>'W3CDTF'),
196        '1970-01-01T00:00:00Z',
197        "'DateTime' method, W3CDTF format");
198     is($date->DateTime(Format =>'RFC2822'),
199        'Thu, 01 Jan 1970 00:00:00 +0000',
200        "'DateTime' method, RFC2822 format");
201     is($date->DateTime(Date => 0, Time => 0),
202        '1970-01-01 00:00:00',
203        "the 'DateTime' method overrides both 'Date' and 'Time' arguments");
204 }
205
206
207 { # positive timezone
208     $current_user->UserObj->__Set( Field => 'Timezone', Value => 'Europe/Moscow');
209     my $date = RT::Date->new( $current_user );
210     $date->Set( Format => 'ISO', Timezone => 'utc', Value => '2005-01-01 15:10:00' );
211     is($date->IsSet,1,"Date has been set");
212     is($date->ISO( Timezone => 'user' ), '2005-01-01 18:10:00', "ISO");
213     is($date->W3CDTF( Timezone => 'user' ), '2005-01-01T18:10:00+03:00', "W3C DTF");
214     is($date->RFC2822( Timezone => 'user' ), 'Sat, 01 Jan 2005 18:10:00 +0300', "RFC2822");
215
216     # DST
217     $date = RT::Date->new( $current_user );
218     $date->Set( Format => 'ISO', Timezone => 'utc', Value => '2005-07-01 15:10:00' );
219     is($date->ISO( Timezone => 'user' ), '2005-07-01 19:10:00', "ISO");
220     is($date->W3CDTF( Timezone => 'user' ), '2005-07-01T19:10:00+04:00', "W3C DTF");
221     is($date->RFC2822( Timezone => 'user' ), 'Fri, 01 Jul 2005 19:10:00 +0400', "RFC2822");
222 }
223
224 { # negative timezone
225     $current_user->UserObj->__Set( Field => 'Timezone', Value => 'America/New_York');
226     my $date = RT::Date->new( $current_user );
227     $date->Set( Format => 'ISO', Timezone => 'utc', Value => '2005-01-01 15:10:00' );
228     is($date->ISO( Timezone => 'user' ), '2005-01-01 10:10:00', "ISO");
229     is($date->W3CDTF( Timezone => 'user' ), '2005-01-01T10:10:00-05:00', "W3C DTF");
230     is($date->RFC2822( Timezone => 'user' ), 'Sat, 01 Jan 2005 10:10:00 -0500', "RFC2822");
231
232     # DST
233     $date = RT::Date->new( $current_user );
234     $date->Set( Format => 'ISO', Timezone => 'utc', Value => '2005-07-01 15:10:00' );
235     is($date->ISO( Timezone => 'user' ), '2005-07-01 11:10:00', "ISO");
236     is($date->W3CDTF( Timezone => 'user' ), '2005-07-01T11:10:00-04:00', "W3C DTF");
237     is($date->RFC2822( Timezone => 'user' ), 'Fri, 01 Jul 2005 11:10:00 -0400', "RFC2822");
238 }
239
240 warning_like
241 { # bad format
242     my $date = RT::Date->new(RT->SystemUser);
243     $date->Set( Format => 'bad' );
244     ok(!$date->IsSet, "bad format");
245 } qr{Unknown Date format: bad};
246
247
248 { # setting value via Unix method
249     my $date = RT::Date->new(RT->SystemUser);
250     $date->Unix(1);
251     is($date->ISO, '1970-01-01 00:00:01', "correct value");
252
253     foreach (undef, 0, '', -5){
254         $date->Unix(1);
255         is($date->ISO, '1970-01-01 00:00:01', "correct value");
256         is($date->IsSet,1,"Date has been set to a value");
257
258         $date->Set(Format => 'unix', Value => $_);
259         is($date->ISO, '1970-01-01 00:00:00', "Set a date to midnight 1/1/1970 GMT due to wrong call");
260         is($date->Unix, 0, "unix is 0 => unset");
261         is($date->IsSet,0,"Date has been unset");
262     }
263
264     foreach (undef, 0, '', -5){
265         $date->Unix(1);
266         is($date->ISO, '1970-01-01 00:00:01', "correct value");
267         is($date->IsSet,1,"Date has been set to a value");
268
269         $date->Unix($_);
270         is($date->ISO, '1970-01-01 00:00:00', "Set a date to midnight 1/1/1970 GMT due to wrong call");
271         is($date->Unix, 0, "unix is 0 => unset");
272         is($date->IsSet,0,"Date has been unset");
273     }
274 }
275
276 my $year = (localtime(time))[5] + 1900;
277
278 { # set+ISO format
279     my $date = RT::Date->new(RT->SystemUser);
280     warning_like {
281         $date->Set(Format => 'ISO', Value => 'weird date');
282     } qr/Couldn't parse date 'weird date' as a ISO format/;
283     ok(!$date->IsSet, "date was wrong => unix == 0");
284
285     # XXX: ISO format has more feature than we suport
286     # http://www.cl.cam.ac.uk/~mgk25/iso-time.html
287
288     $date->Set(Format => 'ISO', Value => '2005-11-28 15:10:00');
289     is($date->ISO, '2005-11-28 15:10:00', "YYYY-DD-MM hh:mm:ss");
290
291     $date->Set(Format => 'ISO', Value => '2005-11-28 15:10:00+00');
292     is($date->ISO, '2005-11-28 15:10:00', "YYYY-DD-MM hh:mm:ss+00");
293
294     $date->Set(Format => 'ISO', Value => '11-28 15:10:00');
295     is($date->ISO, $year .'-11-28 15:10:00', "DD-MM hh:mm:ss");
296
297     $date->Set(Format => 'ISO', Value => '11-28 15:10:00+00');
298     is($date->ISO, $year .'-11-28 15:10:00', "DD-MM hh:mm:ss+00");
299
300     $date->Set(Format => 'ISO', Value => '20051128151000');
301     is($date->ISO, '2005-11-28 15:10:00', "YYYYDDMMhhmmss");
302
303     $date->Set(Format => 'ISO', Value => '1128151000');
304     is($date->ISO, $year .'-11-28 15:10:00', "DDMMhhmmss");
305
306     $date->Set(Format => 'ISO', Value => '2005112815:10:00');
307     is($date->ISO, '2005-11-28 15:10:00', "YYYYDDMMhh:mm:ss");
308
309     $date->Set(Format => 'ISO', Value => '112815:10:00');
310     is($date->ISO, $year .'-11-28 15:10:00', "DDMMhh:mm:ss");
311
312     warning_like {
313         $date->Set(Format => 'ISO', Value => '2005-13-28 15:10:00');
314     } qr/Invalid date/;
315     ok(!$date->IsSet, "wrong month value");
316
317     warning_like {
318         $date->Set(Format => 'ISO', Value => '2005-00-28 15:10:00');
319     } qr/Invalid date/;
320     ok(!$date->IsSet, "wrong month value");
321
322     $date->Set(Format => 'ISO', Value => '1960-01-28 15:10:00');
323     ok(!$date->IsSet, "too old, we don't support");
324 }
325
326 { # set+datemanip format(Time::ParseDate)
327     my $date = RT::Date->new(RT->SystemUser);
328
329     RT->Config->Set( Timezone => 'Europe/Moscow' );
330     $date->Set(Format => 'datemanip', Value => '2005-11-28 15:10:00');
331     is($date->ISO, '2005-11-28 12:10:00', "YYYY-DD-MM hh:mm:ss");
332
333     RT->Config->Set( Timezone => 'UTC' );
334     $date->Set(Format => 'datemanip', Value => '2005-11-28 15:10:00');
335     is($date->ISO, '2005-11-28 15:10:00', "YYYY-DD-MM hh:mm:ss");
336
337     $current_user->UserObj->__Set( Field => 'Timezone', Value => 'Europe/Moscow');
338     $date = RT::Date->new( $current_user );
339     $date->Set(Format => 'datemanip', Value => '2005-11-28 15:10:00');
340     is($date->ISO, '2005-11-28 12:10:00', "YYYY-DD-MM hh:mm:ss");
341 }
342
343 { # set+unknown format(Time::ParseDate)
344     my $date = RT::Date->new(RT->SystemUser);
345     warnings_like {
346         $date->Set(Format => 'unknown', Value => 'weird date');
347     } qr{Couldn't parse date 'weird date' by Time::ParseDate};
348     ok(!$date->IsSet, "date was wrong");
349
350     RT->Config->Set( Timezone => 'Europe/Moscow' );
351     $date->Set(Format => 'unknown', Value => '2005-11-28 15:10:00');
352     is($date->ISO, '2005-11-28 12:10:00', "YYYY-DD-MM hh:mm:ss");
353
354     $date->Set(Format => 'unknown', Value => '2005-11-28 15:10:00', Timezone => 'utc' );
355     is($date->ISO, '2005-11-28 15:10:00', "YYYY-DD-MM hh:mm:ss");
356
357     # test relative dates
358     {
359         set_fixed_time("2005-11-28T15:10:00Z");
360         $date->Set(Format => 'unknown', Value => 'now');
361         is($date->ISO, '2005-11-28 15:10:00', "YYYY-DD-MM hh:mm:ss");
362
363         $date->Set(Format => 'unknown', Value => '1 day ago');
364         is($date->ISO, '2005-11-27 15:10:00', "YYYY-DD-MM hh:mm:ss");
365         restore_time();
366     }
367
368     RT->Config->Set( Timezone => 'UTC' );
369     $date->Set(Format => 'unknown', Value => '2005-11-28 15:10:00');
370     is($date->ISO, '2005-11-28 15:10:00', "YYYY-DD-MM hh:mm:ss");
371
372     $current_user->UserObj->__Set( Field => 'Timezone', Value => 'Europe/Moscow');
373     $date = RT::Date->new( $current_user );
374     $date->Set(Format => 'unknown', Value => '2005-11-28 15:10:00');
375     is($date->ISO, '2005-11-28 12:10:00', "YYYY-DD-MM hh:mm:ss");
376     $date->Set(Format => 'unknown', Value => '2005-11-28 15:10:00', Timezone => 'server' );
377     is($date->ISO, '2005-11-28 15:10:00', "YYYY-DD-MM hh:mm:ss");
378     $date->Set(Format => 'unknown', Value => '2005-11-28 15:10:00', Timezone => 'utc' );
379     is($date->ISO, '2005-11-28 15:10:00', "YYYY-DD-MM hh:mm:ss");
380 }
381
382 { # 'tomorrow 10am' with TZ
383     $current_user->UserObj->__Set( Field => 'Timezone', Value => 'Europe/Moscow');
384
385     set_fixed_time("2012-06-14T15:10:00Z"); # 14th in UTC and Moscow
386     my $date = RT::Date->new( $current_user );
387     $date->Set(Format => 'unknown', Value => 'tomorrow 10am');
388     is($date->ISO, '2012-06-15 06:00:00', "YYYY-DD-MM hh:mm:ss");
389
390     set_fixed_time("2012-06-13T23:10:00Z"); # 13th in UTC and 14th in Moscow
391     $date = RT::Date->new( $current_user );
392     $date->Set(Format => 'unknown', Value => 'tomorrow 10am');
393     is($date->ISO, '2012-06-15 06:00:00', "YYYY-DD-MM hh:mm:ss");
394
395     $current_user->UserObj->__Set( Field => 'Timezone', Value => 'US/Hawaii');
396
397     set_fixed_time("2012-06-14T20:10:00Z"); # 14th in UTC and Hawaii
398     $date = RT::Date->new( $current_user );
399     $date->Set(Format => 'unknown', Value => 'tomorrow 10am');
400     is($date->ISO, '2012-06-15 20:00:00', "YYYY-DD-MM hh:mm:ss");
401
402     set_fixed_time("2012-06-15T05:10:00Z"); # 15th in UTC and 14th in Hawaii
403     $date = RT::Date->new( $current_user );
404     $date->Set(Format => 'unknown', Value => 'tomorrow 10am');
405     is($date->ISO, '2012-06-15 20:00:00', "YYYY-DD-MM hh:mm:ss");
406
407     restore_time();
408 }
409
410 { # SetToMidnight
411     my $date = RT::Date->new(RT->SystemUser);
412
413     RT->Config->Set( Timezone => 'Europe/Moscow' );
414     $date->Set(Format => 'ISO', Value => '2005-11-28 15:10:00');
415     $date->SetToMidnight;
416     is($date->ISO, '2005-11-28 00:00:00', "default is utc");
417     $date->Set(Format => 'ISO', Value => '2005-11-28 15:10:00');
418     $date->SetToMidnight(Timezone => 'utc');
419     is($date->ISO, '2005-11-28 00:00:00', "utc context");
420     $date->Set(Format => 'ISO', Value => '2005-11-28 15:10:00');
421     $date->SetToMidnight(Timezone => 'user');
422     is($date->ISO, '2005-11-27 21:00:00', "user context, user has no preference, fallback to server");
423     $date->Set(Format => 'ISO', Value => '2005-11-28 15:10:00');
424     $date->SetToMidnight(Timezone => 'server');
425     is($date->ISO, '2005-11-27 21:00:00', "server context");
426
427     $current_user->UserObj->__Set( Field => 'Timezone', Value => 'Europe/Moscow');
428     $date = RT::Date->new( $current_user );
429     $date->Set(Format => 'ISO', Value => '2005-11-28 15:10:00');
430     $date->SetToMidnight;
431     is($date->ISO, '2005-11-28 00:00:00', "default is utc");
432     $date->Set(Format => 'ISO', Value => '2005-11-28 15:10:00');
433     $date->SetToMidnight(Timezone => 'utc');
434     is($date->ISO, '2005-11-28 00:00:00', "utc context");
435     $date->Set(Format => 'ISO', Value => '2005-11-28 15:10:00');
436     $date->SetToMidnight(Timezone => 'user');
437     is($date->ISO, '2005-11-27 21:00:00', "user context");
438     $date->SetToMidnight(Timezone => 'server');
439     is($date->ISO, '2005-11-27 21:00:00', "server context");
440
441     RT->Config->Set( Timezone => 'UTC' );
442 }
443
444 { # SetToNow
445     my $date = RT::Date->new(RT->SystemUser);
446     my $time = time;
447     $date->SetToNow;
448     ok($date->Unix >= $time, 'close enough');
449     ok($date->Unix < $time+5, 'difference is less than five seconds');
450 }
451
452 {
453     my $date = RT::Date->new(RT->SystemUser);
454     
455     $date->Unix(0);
456     $date->AddSeconds;
457     is($date->ISO, '1970-01-01 00:00:00', "nothing changed");
458     $date->AddSeconds(0);
459     is($date->ISO, '1970-01-01 00:00:00', "nothing changed");
460     
461     $date->Unix(0);
462     $date->AddSeconds(5);
463     is($date->ISO, '1970-01-01 00:00:05', "added five seconds");
464     $date->AddSeconds(-2);
465     is($date->ISO, '1970-01-01 00:00:03', "substracted two seconds");
466     
467     $date->Unix(0);
468     $date->AddSeconds(3661);
469     is($date->ISO, '1970-01-01 01:01:01', "added one hour, minute and a second");
470
471 # XXX: TODO, doesn't work with Test::Warn
472 #    TODO: {
473 #        local $TODO = "BUG or subject to change Date handling to support unix time <= 0";
474 #        $date->Unix(0);
475 #        $date->AddSeconds(-2);
476 #        ok($date->Unix > 0);
477 #    }
478
479     $date->Unix(0);
480     $date->AddDay;
481     is($date->ISO, '1970-01-02 00:00:00', "added one day");
482     $date->AddDays(2);
483     is($date->ISO, '1970-01-04 00:00:00', "added two days");
484     $date->AddDays(-1);
485     is($date->ISO, '1970-01-03 00:00:00', "substructed one day");
486     
487     $date->Unix(0);
488     $date->AddDays(31);
489     is($date->ISO, '1970-02-01 00:00:00', "added one month");
490
491     $date->Unix(0);
492     $date->AddDays(0);
493     is($date->ISO, '1970-01-01 00:00:00', "added no days");
494
495     $date->Unix(0);
496     $date->AddDays();
497     is($date->ISO, '1970-01-02 00:00:00', "added one day with no argument");
498 }
499
500 {
501     $current_user->UserObj->__Set( Field => 'Timezone', Value => '');
502     my $date = RT::Date->new( $current_user );
503     is($date->AsString, "Not set", "AsString returns 'Not set'");
504
505     RT->Config->Set( DateTimeFormat => '');
506     $date->Unix(1);
507     is($date->AsString, 'Thu Jan 01 00:00:01 1970', "correct string");
508     is($date->AsString(Date => 0), '00:00:01', "correct string");
509     is($date->AsString(Time => 0), 'Thu Jan 01 1970', "correct string");
510     is($date->AsString(Date => 0, Time => 0), 'Thu Jan 01 00:00:01 1970', "invalid input");
511
512     RT->Config->Set( DateTimeFormat => 'RFC2822' );
513     $date->Unix(1);
514     is($date->AsString, 'Thu, 01 Jan 1970 00:00:01 +0000', "correct string");
515
516     RT->Config->Set( DateTimeFormat => { Format => 'RFC2822', Seconds => 0 } );
517     $date->Unix(1);
518     is($date->AsString, 'Thu, 01 Jan 1970 00:00 +0000', "correct string");
519     is($date->AsString(Seconds => 1), 'Thu, 01 Jan 1970 00:00:01 +0000', "correct string");
520 }
521
522 { # DurationAsString
523     my $date = RT::Date->new(RT->SystemUser);
524
525     is($date->DurationAsString(1), '1 second', '1 sec');
526     is($date->DurationAsString(59), '59 seconds', '59 sec');
527     is($date->DurationAsString(60), '1 minute', '1 min');
528     is($date->DurationAsString(60*119), '119 minutes', '119 min');
529     is($date->DurationAsString(60*60*2-1), '120 minutes', '120 min');
530     is($date->DurationAsString(60*60*2), '2 hours', '2 hours');
531     is($date->DurationAsString(60*60*48-1), '48 hours', '48 hours');
532     is($date->DurationAsString(60*60*48), '2 days', '2 days');
533     is($date->DurationAsString(60*60*24*14-1), '14 days', '14 days');
534     is($date->DurationAsString(60*60*24*14), '2 weeks', '2 weeks');
535     is($date->DurationAsString(60*60*24*7*8-1), '8 weeks', '8 weeks');
536     is($date->DurationAsString(60*60*24*61), '2 months', '2 months');
537     is($date->DurationAsString(60*60*24*365-1), '12 months', '12 months');
538     is($date->DurationAsString(60*60*24*366), '1 year', '1 year');
539
540     is($date->DurationAsString(-1), '1 second ago', '1 sec ago');
541 }
542
543 { # DiffAsString
544     my $date = RT::Date->new(RT->SystemUser);
545     is($date->DiffAsString(1), '', 'no diff, wrong input');
546     is($date->DiffAsString(-1), '', 'no diff, wrong input');
547     is($date->DiffAsString('qwe'), '', 'no diff, wrong input');
548
549     $date->Unix(2);
550     is($date->DiffAsString(-1), '', 'no diff, wrong input');
551
552     is($date->DiffAsString(3), '1 second ago', 'diff: 1 sec ago');
553     is($date->DiffAsString(1), '1 second', 'diff: 1 sec');
554
555     my $ndate = RT::Date->new(RT->SystemUser);
556     is($date->DiffAsString($ndate), '', 'no diff, wrong input');
557     $ndate->Unix(3);
558     is($date->DiffAsString($ndate), '1 second ago', 'diff: 1 sec ago');
559 }
560
561 { # Diff
562     my $date = RT::Date->new(RT->SystemUser);
563     $date->SetToNow;
564     my $diff = $date->Diff;
565     ok($diff <= 0, 'close enought');
566     ok($diff > -5, 'close enought');
567 }
568
569 { # AgeAsString
570     my $date = RT::Date->new(RT->SystemUser);
571     $date->SetToNow;
572     my $diff = $date->AgeAsString;
573     like($diff, qr/^(0 seconds|(1 second|[2-5] seconds) ago)$/, 'close enought');
574 }
575
576 { # GetWeekday
577     my $date = RT::Date->new(RT->SystemUser);
578     is($date->GetWeekday(7),  '',    '7 and greater are invalid');
579     is($date->GetWeekday(6),  'Sat', '6 is Saturday');
580     is($date->GetWeekday(0),  'Sun', '0 is Sunday');
581     is($date->GetWeekday(-1), 'Sat', '-1 is Saturday');
582     is($date->GetWeekday(-7), 'Sun', '-7 is Sunday');
583     is($date->GetWeekday(-8), '',    '-8 and lesser are invalid');
584 }
585
586 { # GetMonth
587     my $date = RT::Date->new(RT->SystemUser);
588     is($date->GetMonth(12),  '',     '12 and greater are invalid');
589     is($date->GetMonth(11),  'Dec', '11 is December');
590     is($date->GetMonth(0),   'Jan', '0 is January');
591     is($date->GetMonth(-1),  'Dec', '11 is December');
592     is($date->GetMonth(-12), 'Jan', '0 is January');
593     is($date->GetMonth(-13),  '',    '-13 and lesser are invalid');
594 }
595
596 {
597     # set unknown format: edge cases
598     my $date = RT::Date->new(RT->SystemUser);
599     $date->Set( Value => 0, Format => 'unknown' );
600     ok( !$date->IsSet, "unix is 0 with Value => 0, Format => 'unknown'" );
601
602     $date->Set( Value => '', Format => 'unknown' );
603     ok( !$date->IsSet, "unix is 0 with Value => '', Format => 'unknown'" );
604
605     $date->Set( Value => ' ', Format => 'unknown' );
606     ok( !$date->IsSet, "unix is 0 with Value => ' ', Format => 'unknown'" );
607 }
608
609 #TODO: AsString
610 #TODO: RFC2822, W3CDTF with Timezones
611
612 done_testing;