first pass RT4 merge, RT#13852
[freeside.git] / rt / t / api / date.t
1 #!/usr/bin/perl
2
3 use Test::MockTime qw(set_fixed_time restore_time);
4 use DateTime;
5
6 use warnings; use strict;
7 use RT::Test tests => 172;
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->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");
100
101     is($date->ISO(Time => 0),
102        '1970-01-01',
103        "ISO format without time part");
104     is($date->W3CDTF(Time => 0),
105        '1970-01-01',
106        "W3CDTF format without time part");
107     is($date->RFC2822(Time => 0),
108        'Thu, 1 Jan 1970',
109        "RFC2822 format without time part");
110     is($date->LocalizedDateTime(Time => 0),
111        'Thu, Jan 1, 1970',
112        "LocalizedDateTime format without time part");
113
114     is($date->ISO(Date => 0),
115        '00:00:00',
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),
121        '00:00:00 +0000',
122        "RFC2822 format without date part");
123     is($date->LocalizedDateTime(Date => 0),
124        '12:00:00 AM',
125        "LocalizedDateTime format without date part");
126
127     is($date->ISO(Date => 0, Seconds => 0),
128        '00:00',
129        "ISO format without date part and seconds");
130     is($date->W3CDTF(Date => 0, Seconds => 0),
131        '1970-01-01T00:00Z',
132        "W3CDTF format without seconds, but we ship date part even if Date is false");
133     is($date->RFC2822(Date => 0, Seconds => 0),
134        '00:00 +0000',
135        "RFC2822 format without date part and seconds");
136
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),
141        '00:00:00 +0000',
142        "RFC2822 format without 'day of week' and date parts(corner case test)");
143
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");
156
157     is($date->Date,
158        '1970-01-01',
159        "the default format for the 'Date' method is ISO");
160     is($date->Date(Format => 'W3CDTF'),
161        '1970-01-01',
162        "'Date' method, W3CDTF format");
163     is($date->Date(Format => 'RFC2822'),
164        'Thu, 1 Jan 1970',
165        "'Date' method, RFC2822 format");
166     is($date->Date(Time => 1),
167        '1970-01-01',
168        "'Date' method doesn't pass through 'Time' argument");
169     is($date->Date(Date => 0),
170        '1970-01-01',
171        "'Date' method overrides 'Date' argument");
172
173     is($date->Time,
174        '00:00:00',
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'),
180        '00:00:00 +0000',
181        "'Time' method, RFC2822 format");
182     is($date->Time(Date => 1),
183        '00:00:00',
184        "'Time' method doesn't pass through 'Date' argument");
185     is($date->Time(Time => 0),
186        '00:00:00',
187        "'Time' method overrides 'Time' argument");
188
189     is($date->DateTime,
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");
201 }
202
203
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");
211
212     # DST
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");
218 }
219
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");
227
228     # DST
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");
234 }
235
236 warning_like
237 { # bad format
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};
242
243
244 { # setting value via Unix method
245     my $date = RT::Date->new(RT->SystemUser);
246     $date->Unix(1);
247     is($date->ISO, '1970-01-01 00:00:01', "correct value");
248
249     foreach (undef, 0, ''){
250         $date->Unix(1);
251         is($date->ISO, '1970-01-01 00:00:01', "correct value");
252
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");
256     }
257 }
258
259 my $year = (localtime(time))[5] + 1900;
260
261 { # set+ISO format
262     my $date = RT::Date->new(RT->SystemUser);
263     warning_like {
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");
267
268     # XXX: ISO format has more feature than we suport
269     # http://www.cl.cam.ac.uk/~mgk25/iso-time.html
270
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");
273
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");
276
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");
279
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");
282
283     $date->Set(Format => 'ISO', Value => '20051128151000');
284     is($date->ISO, '2005-11-28 15:10:00', "YYYYDDMMhhmmss");
285
286     $date->Set(Format => 'ISO', Value => '1128151000');
287     is($date->ISO, $year .'-11-28 15:10:00', "DDMMhhmmss");
288
289     $date->Set(Format => 'ISO', Value => '2005112815:10:00');
290     is($date->ISO, '2005-11-28 15:10:00', "YYYYDDMMhh:mm:ss");
291
292     $date->Set(Format => 'ISO', Value => '112815:10:00');
293     is($date->ISO, $year .'-11-28 15:10:00', "DDMMhh:mm:ss");
294
295     $date->Set(Format => 'ISO', Value => '2005-13-28 15:10:00');
296     is($date->Unix, 0, "wrong month value");
297
298     $date->Set(Format => 'ISO', Value => '2005-00-28 15:10:00');
299     is($date->Unix, 0, "wrong month value");
300
301     $date->Set(Format => 'ISO', Value => '1960-01-28 15:10:00');
302     is($date->Unix, 0, "too old, we don't support");
303 }
304
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");
309
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");
313
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");
317
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");
322 }
323
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");
328
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");
332
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");
335
336     # test relative dates
337     {
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");
341
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");
344         restore_time();
345     }
346
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");
350
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");
359 }
360
361 { # SetToMidnight
362     my $date = RT::Date->new(RT->SystemUser);
363
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");
377
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");
391
392     RT->Config->Set( Timezone => 'UTC' );
393 }
394
395 { # SetToNow
396     my $date = RT::Date->new(RT->SystemUser);
397     my $time = time;
398     $date->SetToNow;
399     ok($date->Unix >= $time, 'close enough');
400     ok($date->Unix < $time+5, 'difference is less than five seconds');
401 }
402
403 {
404     my $date = RT::Date->new(RT->SystemUser);
405     
406     $date->Unix(0);
407     $date->AddSeconds;
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");
411     
412     $date->Unix(0);
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");
417     
418     $date->Unix(0);
419     $date->AddSeconds(3661);
420     is($date->ISO, '1970-01-01 01:01:01', "added one hour, minute and a second");
421
422 # XXX: TODO, doesn't work with Test::Warn
423 #    TODO: {
424 #        local $TODO = "BUG or subject to change Date handling to support unix time <= 0";
425 #        $date->Unix(0);
426 #        $date->AddSeconds(-2);
427 #        ok($date->Unix > 0);
428 #    }
429
430     $date->Unix(0);
431     $date->AddDay;
432     is($date->ISO, '1970-01-02 00:00:00', "added one day");
433     $date->AddDays(2);
434     is($date->ISO, '1970-01-04 00:00:00', "added two days");
435     $date->AddDays(-1);
436     is($date->ISO, '1970-01-03 00:00:00', "substructed one day");
437     
438     $date->Unix(0);
439     $date->AddDays(31);
440     is($date->ISO, '1970-02-01 00:00:00', "added one month");
441 }
442
443 {
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'");
447
448     RT->Config->Set( DateTimeFormat => '');
449     $date->Unix(1);
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");
454
455     RT->Config->Set( DateTimeFormat => 'RFC2822' );
456     $date->Unix(1);
457     is($date->AsString, 'Thu, 1 Jan 1970 00:00:01 +0000', "correct string");
458
459     RT->Config->Set( DateTimeFormat => { Format => 'RFC2822', Seconds => 0 } );
460     $date->Unix(1);
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");
463 }
464
465 { # DurationAsString
466     my $date = RT::Date->new(RT->SystemUser);
467
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');
482
483     is($date->DurationAsString(-1), '1 sec ago', '1 sec ago');
484 }
485
486 { # DiffAsString
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');
491
492     $date->Unix(2);
493     is($date->DiffAsString(-1), '', 'no diff, wrong input');
494
495     is($date->DiffAsString(3), '1 sec ago', 'diff: 1 sec ago');
496     is($date->DiffAsString(1), '1 sec', 'diff: 1 sec');
497
498     my $ndate = RT::Date->new(RT->SystemUser);
499     is($date->DiffAsString($ndate), '', 'no diff, wrong input');
500     $ndate->Unix(3);
501     is($date->DiffAsString($ndate), '1 sec ago', 'diff: 1 sec ago');
502 }
503
504 { # Diff
505     my $date = RT::Date->new(RT->SystemUser);
506     $date->SetToNow;
507     my $diff = $date->Diff;
508     ok($diff <= 0, 'close enought');
509     ok($diff > -5, 'close enought');
510 }
511
512 { # AgeAsString
513     my $date = RT::Date->new(RT->SystemUser);
514     $date->SetToNow;
515     my $diff = $date->AgeAsString;
516     like($diff, qr/^(0 sec|[1-5] sec ago)$/, 'close enought');
517 }
518
519 { # GetWeekday
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');
527 }
528
529 { # GetMonth
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');
537 }
538
539 {
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'" );
544
545     $date->Set( Value => '', Format => 'unknown' );
546     is( $date->Unix(), 0, "unix is 0 with Value => '', Format => 'unknown'" );
547
548     $date->Set( Value => ' ', Format => 'unknown' );
549     is( $date->Unix(), 0, "unix is 0 with Value => ' ', Format => 'unknown'" );
550 }
551
552 #TODO: AsString
553 #TODO: RFC2822, W3CDTF with Timezones
554