X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=rt%2Ft%2Fapi%2Fdate.t;h=dd22943c1982c122ef2627d6f1bae2ac2319408b;hb=ebd7d1be21224877db7dff9972d02aedc9b35cd6;hp=cc1c694cc2326846c7029987824dadd6b9b440aa;hpb=45d35d5739d05e602bc317739485693e0e9ff0b5;p=freeside.git diff --git a/rt/t/api/date.t b/rt/t/api/date.t index cc1c694cc..dd22943c1 100644 --- a/rt/t/api/date.t +++ b/rt/t/api/date.t @@ -4,7 +4,7 @@ use DateTime; use warnings; use strict; -use RT::Test tests => 173; +use RT::Test tests => undef; use RT::User; use Test::Warn; @@ -57,7 +57,7 @@ my $current_user; is($date->Timezone('user'), 'Europe/Moscow', "in user context still returns user's timezone"); - + $current_user->UserObj->__Set( Field => 'Timezone', Value => ''); is_empty($current_user->UserObj->Timezone, "successfuly changed user's timezone"); @@ -83,6 +83,7 @@ my $current_user; { my $date = RT::Date->new(RT->SystemUser); + is($date->IsSet,0, "new date isn't set"); is($date->Unix, 0, "new date returns 0 in Unix format"); is($date->Get, '1970-01-01 00:00:00', "default is ISO format"); warning_like { @@ -207,6 +208,7 @@ my $current_user; $current_user->UserObj->__Set( Field => 'Timezone', Value => 'Europe/Moscow'); my $date = RT::Date->new( $current_user ); $date->Set( Format => 'ISO', Timezone => 'utc', Value => '2005-01-01 15:10:00' ); + is($date->IsSet,1,"Date has been set"); is($date->ISO( Timezone => 'user' ), '2005-01-01 18:10:00', "ISO"); is($date->W3CDTF( Timezone => 'user' ), '2005-01-01T18:10:00+03:00', "W3C DTF"); is($date->RFC2822( Timezone => 'user' ), 'Sat, 01 Jan 2005 18:10:00 +0300', "RFC2822"); @@ -239,7 +241,7 @@ warning_like { # bad format my $date = RT::Date->new(RT->SystemUser); $date->Set( Format => 'bad' ); - is($date->Unix, 0, "bad format"); + ok(!$date->IsSet, "bad format"); } qr{Unknown Date format: bad}; @@ -248,13 +250,26 @@ warning_like $date->Unix(1); is($date->ISO, '1970-01-01 00:00:01', "correct value"); - foreach (undef, 0, ''){ + foreach (undef, 0, '', -5){ $date->Unix(1); is($date->ISO, '1970-01-01 00:00:01', "correct value"); + is($date->IsSet,1,"Date has been set to a value"); $date->Set(Format => 'unix', Value => $_); is($date->ISO, '1970-01-01 00:00:00', "Set a date to midnight 1/1/1970 GMT due to wrong call"); is($date->Unix, 0, "unix is 0 => unset"); + is($date->IsSet,0,"Date has been unset"); + } + + foreach (undef, 0, '', -5){ + $date->Unix(1); + is($date->ISO, '1970-01-01 00:00:01', "correct value"); + is($date->IsSet,1,"Date has been set to a value"); + + $date->Unix($_); + is($date->ISO, '1970-01-01 00:00:00', "Set a date to midnight 1/1/1970 GMT due to wrong call"); + is($date->Unix, 0, "unix is 0 => unset"); + is($date->IsSet,0,"Date has been unset"); } } @@ -265,7 +280,7 @@ my $year = (localtime(time))[5] + 1900; warning_like { $date->Set(Format => 'ISO', Value => 'weird date'); } qr/Couldn't parse date 'weird date' as a ISO format/; - is($date->Unix, 0, "date was wrong => unix == 0"); + ok(!$date->IsSet, "date was wrong => unix == 0"); # XXX: ISO format has more feature than we suport # http://www.cl.cam.ac.uk/~mgk25/iso-time.html @@ -294,14 +309,18 @@ my $year = (localtime(time))[5] + 1900; $date->Set(Format => 'ISO', Value => '112815:10:00'); is($date->ISO, $year .'-11-28 15:10:00', "DDMMhh:mm:ss"); - $date->Set(Format => 'ISO', Value => '2005-13-28 15:10:00'); - is($date->Unix, 0, "wrong month value"); + warning_like { + $date->Set(Format => 'ISO', Value => '2005-13-28 15:10:00'); + } qr/Invalid date/; + ok(!$date->IsSet, "wrong month value"); - $date->Set(Format => 'ISO', Value => '2005-00-28 15:10:00'); - is($date->Unix, 0, "wrong month value"); + warning_like { + $date->Set(Format => 'ISO', Value => '2005-00-28 15:10:00'); + } qr/Invalid date/; + ok(!$date->IsSet, "wrong month value"); $date->Set(Format => 'ISO', Value => '1960-01-28 15:10:00'); - is($date->Unix, 0, "too old, we don't support"); + ok(!$date->IsSet, "too old, we don't support"); } { # set+datemanip format(Time::ParseDate) @@ -326,7 +345,7 @@ my $year = (localtime(time))[5] + 1900; warnings_like { $date->Set(Format => 'unknown', Value => 'weird date'); } qr{Couldn't parse date 'weird date' by Time::ParseDate}; - is($date->Unix, 0, "date was wrong"); + ok(!$date->IsSet, "date was wrong"); RT->Config->Set( Timezone => 'Europe/Moscow' ); $date->Set(Format => 'unknown', Value => '2005-11-28 15:10:00'); @@ -360,6 +379,34 @@ my $year = (localtime(time))[5] + 1900; is($date->ISO, '2005-11-28 15:10:00', "YYYY-DD-MM hh:mm:ss"); } +{ # 'tomorrow 10am' with TZ + $current_user->UserObj->__Set( Field => 'Timezone', Value => 'Europe/Moscow'); + + set_fixed_time("2012-06-14T15:10:00Z"); # 14th in UTC and Moscow + my $date = RT::Date->new( $current_user ); + $date->Set(Format => 'unknown', Value => 'tomorrow 10am'); + is($date->ISO, '2012-06-15 06:00:00', "YYYY-DD-MM hh:mm:ss"); + + set_fixed_time("2012-06-13T23:10:00Z"); # 13th in UTC and 14th in Moscow + $date = RT::Date->new( $current_user ); + $date->Set(Format => 'unknown', Value => 'tomorrow 10am'); + is($date->ISO, '2012-06-15 06:00:00', "YYYY-DD-MM hh:mm:ss"); + + $current_user->UserObj->__Set( Field => 'Timezone', Value => 'US/Hawaii'); + + set_fixed_time("2012-06-14T20:10:00Z"); # 14th in UTC and Hawaii + $date = RT::Date->new( $current_user ); + $date->Set(Format => 'unknown', Value => 'tomorrow 10am'); + is($date->ISO, '2012-06-15 20:00:00', "YYYY-DD-MM hh:mm:ss"); + + set_fixed_time("2012-06-15T05:10:00Z"); # 15th in UTC and 14th in Hawaii + $date = RT::Date->new( $current_user ); + $date->Set(Format => 'unknown', Value => 'tomorrow 10am'); + is($date->ISO, '2012-06-15 20:00:00', "YYYY-DD-MM hh:mm:ss"); + + restore_time(); +} + { # SetToMidnight my $date = RT::Date->new(RT->SystemUser); @@ -440,6 +487,14 @@ my $year = (localtime(time))[5] + 1900; $date->Unix(0); $date->AddDays(31); is($date->ISO, '1970-02-01 00:00:00', "added one month"); + + $date->Unix(0); + $date->AddDays(0); + is($date->ISO, '1970-01-01 00:00:00', "added no days"); + + $date->Unix(0); + $date->AddDays(); + is($date->ISO, '1970-01-02 00:00:00', "added one day with no argument"); } { @@ -467,11 +522,11 @@ my $year = (localtime(time))[5] + 1900; { # DurationAsString my $date = RT::Date->new(RT->SystemUser); - is($date->DurationAsString(1), '1 sec', '1 sec'); - is($date->DurationAsString(59), '59 sec', '59 sec'); - is($date->DurationAsString(60), '1 min', '1 min'); - is($date->DurationAsString(60*119), '119 min', '119 min'); - is($date->DurationAsString(60*60*2-1), '120 min', '120 min'); + is($date->DurationAsString(1), '1 second', '1 sec'); + is($date->DurationAsString(59), '59 seconds', '59 sec'); + is($date->DurationAsString(60), '1 minute', '1 min'); + is($date->DurationAsString(60*119), '119 minutes', '119 min'); + is($date->DurationAsString(60*60*2-1), '120 minutes', '120 min'); is($date->DurationAsString(60*60*2), '2 hours', '2 hours'); is($date->DurationAsString(60*60*48-1), '48 hours', '48 hours'); is($date->DurationAsString(60*60*48), '2 days', '2 days'); @@ -480,9 +535,9 @@ my $year = (localtime(time))[5] + 1900; is($date->DurationAsString(60*60*24*7*8-1), '8 weeks', '8 weeks'); is($date->DurationAsString(60*60*24*61), '2 months', '2 months'); is($date->DurationAsString(60*60*24*365-1), '12 months', '12 months'); - is($date->DurationAsString(60*60*24*366), '1 years', '1 years'); + is($date->DurationAsString(60*60*24*366), '1 year', '1 year'); - is($date->DurationAsString(-1), '1 sec ago', '1 sec ago'); + is($date->DurationAsString(-1), '1 second ago', '1 sec ago'); } { # DiffAsString @@ -494,13 +549,13 @@ my $year = (localtime(time))[5] + 1900; $date->Unix(2); is($date->DiffAsString(-1), '', 'no diff, wrong input'); - is($date->DiffAsString(3), '1 sec ago', 'diff: 1 sec ago'); - is($date->DiffAsString(1), '1 sec', 'diff: 1 sec'); + is($date->DiffAsString(3), '1 second ago', 'diff: 1 sec ago'); + is($date->DiffAsString(1), '1 second', 'diff: 1 sec'); my $ndate = RT::Date->new(RT->SystemUser); is($date->DiffAsString($ndate), '', 'no diff, wrong input'); $ndate->Unix(3); - is($date->DiffAsString($ndate), '1 sec ago', 'diff: 1 sec ago'); + is($date->DiffAsString($ndate), '1 second ago', 'diff: 1 sec ago'); } { # Diff @@ -515,7 +570,7 @@ my $year = (localtime(time))[5] + 1900; my $date = RT::Date->new(RT->SystemUser); $date->SetToNow; my $diff = $date->AgeAsString; - like($diff, qr/^(0 sec|[1-5] sec ago)$/, 'close enought'); + like($diff, qr/^(0 seconds|(1 second|[2-5] seconds) ago)$/, 'close enought'); } { # GetWeekday @@ -542,15 +597,16 @@ my $year = (localtime(time))[5] + 1900; # set unknown format: edge cases my $date = RT::Date->new(RT->SystemUser); $date->Set( Value => 0, Format => 'unknown' ); - is( $date->Unix(), 0, "unix is 0 with Value => 0, Format => 'unknown'" ); + ok( !$date->IsSet, "unix is 0 with Value => 0, Format => 'unknown'" ); $date->Set( Value => '', Format => 'unknown' ); - is( $date->Unix(), 0, "unix is 0 with Value => '', Format => 'unknown'" ); + ok( !$date->IsSet, "unix is 0 with Value => '', Format => 'unknown'" ); $date->Set( Value => ' ', Format => 'unknown' ); - is( $date->Unix(), 0, "unix is 0 with Value => ' ', Format => 'unknown'" ); + ok( !$date->IsSet, "unix is 0 with Value => ' ', Format => 'unknown'" ); } #TODO: AsString #TODO: RFC2822, W3CDTF with Timezones +done_testing;