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