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 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
|
// Copyright 2006 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
goog.provide('goog.dateTest');
goog.setTestOnly('goog.dateTest');
goog.require('goog.array');
goog.require('goog.date');
goog.require('goog.date.Date');
goog.require('goog.date.DateTime');
goog.require('goog.date.Interval');
goog.require('goog.date.month');
goog.require('goog.date.weekDay');
goog.require('goog.i18n.DateTimeSymbols');
goog.require('goog.testing.ExpectedFailures');
goog.require('goog.testing.jsunit');
goog.require('goog.userAgent');
goog.require('goog.userAgent.platform');
goog.require('goog.userAgent.product');
goog.require('goog.userAgent.product.isVersion');
var expectedFailures;
function shouldRunTests() {
// Test disabled in Chrome-vista due to flakiness. See b/2753939.
if (goog.userAgent.product.CHROME && goog.userAgent.WINDOWS &&
goog.userAgent.platform.VERSION == '6.0') {
return false;
}
return true;
}
function setUpPage() {
expectedFailures = new goog.testing.ExpectedFailures();
}
function tearDown() {
expectedFailures.handleTearDown();
}
/**
* Unit test for Closure's 'goog.date'.
*/
function testIsLeapYear() {
var f = goog.date.isLeapYear;
assertFalse('Year 1900 was not a leap year (the 100 rule)', f(1900));
assertFalse('Year 1901 was not a leap year (the 100 rule)', f(1901));
assertTrue('Year 1904 was a leap year', f(1904));
assertFalse('Year 1999 was not a leap year', f(1999));
assertTrue('Year 2000 was a leap year (the 400 rule)', f(2000));
assertTrue('Year 2004 was a leap year', f(2004));
assertFalse('Year 2006 was not a leap year', f(2006));
}
function testIsLongIsoYear() {
var f = goog.date.isLongIsoYear;
// see http://en.wikipedia.org/wiki/ISO_week_date#The_leap_year_cycle
assertFalse('1902 was not an ISO leap year', f(1902));
assertTrue('1903 was an ISO leap year', f(1903));
assertFalse('1904 was not an ISO leap year', f(1904));
assertTrue('1981 was an ISO leap year', f(1981));
assertTrue('1987 was an ISO leap year', f(1987));
assertFalse('2000 was not an ISO leap year', f(2000));
}
function testGetNumberOfDaysInMonth() {
var f = goog.date.getNumberOfDaysInMonth;
assertEquals('January has 31 days', f(2006, goog.date.month.JAN, 2000), 31);
assertEquals('February has 28 days', f(2006, goog.date.month.FEB), 28);
assertEquals(
'February has 29 days (leap year)', f(2008, goog.date.month.FEB), 29);
assertEquals('March has 31 days', f(2006, goog.date.month.MAR), 31);
assertEquals('April has 30 days', f(2006, goog.date.month.APR), 30);
assertEquals('May has 31 days', f(2006, goog.date.month.MAY), 31);
assertEquals('June has 30 days', f(2006, goog.date.month.JUN), 30);
assertEquals('July has 31 days', f(2006, goog.date.month.JUL), 31);
assertEquals('August has 31 days', f(2006, goog.date.month.AUG), 31);
assertEquals('September has 30 days', f(2006, goog.date.month.SEP), 30);
assertEquals('October has 31 days', f(2006, goog.date.month.OCT), 31);
assertEquals('November has 30 days', f(2006, goog.date.month.NOV), 30);
assertEquals('December has 31 days', f(2006, goog.date.month.DEC), 31);
}
function testIsSameDay() {
assertTrue(
'Dates are on the same day',
goog.date.isSameDay(
new Date('2009/02/01 12:45:12'), new Date('2009/02/01 01:15:49')));
assertFalse(
'Days are different',
goog.date.isSameDay(
new Date('2009/02/01 12:45:12'), new Date('2009/02/02 01:15:49')));
assertFalse(
'Months are different',
goog.date.isSameDay(
new Date('2009/02/01 12:45:12'), new Date('2009/03/01 01:15:49')));
assertFalse(
'Years are different',
goog.date.isSameDay(
new Date('2009/02/01 12:45:12'), new Date('2010/02/01 01:15:49')));
assertFalse(
'Wrong millennium',
goog.date.isSameDay(
new Date('2009/02/01 12:45:12'), new Date('1009/02/01 01:15:49')));
}
function testIsSameMonth() {
assertTrue(
'Dates are on the same day',
goog.date.isSameMonth(
new Date('2009/02/01 12:45:12'), new Date('2009/02/01 01:15:49')));
assertTrue(
'Dates are in the same month',
goog.date.isSameMonth(
new Date('2009/02/01 12:45:12'), new Date('2009/02/10 01:15:49')));
assertFalse(
'Month is different',
goog.date.isSameMonth(
new Date('2009/02/01 12:45:12'), new Date('2009/03/01 01:15:49')));
assertFalse(
'Year is different',
goog.date.isSameMonth(
new Date('2008/02/01 12:45:12'), new Date('2009/02/01 01:15:49')));
assertFalse(
'Wrong millennium',
goog.date.isSameMonth(
new Date('2009/02/01 12:45:12'), new Date('1009/02/01 01:15:49')));
}
function testIsSameYear() {
assertTrue(
'Dates are on the same day',
goog.date.isSameYear(
new Date('2009/02/01 12:45:12'), new Date('2009/02/01 01:15:49')));
assertTrue(
'Only days are different',
goog.date.isSameYear(
new Date('2009/02/01 12:45:12'), new Date('2009/02/11 01:15:49')));
assertTrue(
'Only months are different',
goog.date.isSameYear(
new Date('2009/02/01 12:45:12'), new Date('2009/02/01 01:15:49')));
assertFalse(
'Years are different',
goog.date.isSameYear(
new Date('2009/02/01 12:45:12'), new Date('2010/02/01 01:15:49')));
assertFalse(
'Years are different',
goog.date.isSameYear(
new Date('2009/02/01 12:45:12'), new Date('2008/02/01 01:15:49')));
}
function testGetWeekNumber() {
var f = goog.date.getWeekNumber;
// Test cases from http://en.wikipedia.org/wiki/ISO_week_date#Examples
assertEquals(
'2005-01-01 is the week 53 of the previous year', 53,
f(2005, goog.date.month.JAN, 1));
assertEquals(
'2005-01-02 is the week 53 of the previous year', 53,
f(2005, goog.date.month.JAN, 2));
assertEquals(
'2005-12-31 is the week 52', 52, f(2005, goog.date.month.DEC, 31));
assertEquals('2007-01-01 is the week 1', 1, f(2007, goog.date.month.JAN, 1));
assertEquals(
'2007-12-30 is the week 52', 52, f(2007, goog.date.month.DEC, 30));
assertEquals(
'2007-12-31 is the week 1 of the following year', 1,
f(2007, goog.date.month.DEC, 31));
assertEquals('2008-01-01 is the week 1', 1, f(2008, goog.date.month.JAN, 1));
assertEquals(
'2008-12-28 is the week 52', 52, f(2008, goog.date.month.DEC, 28));
assertEquals(
'2008-12-29 is the week 1 of the following year', 1,
f(2008, goog.date.month.DEC, 29));
assertEquals(
'2008-12-31 is the week 1 of the following year', 1,
f(2008, goog.date.month.DEC, 31));
assertEquals('2009-01-01 is the week 1', 1, f(2009, goog.date.month.JAN, 1));
assertEquals(
'2009-12-31 is the week 53 of the previous year', 53,
f(2009, goog.date.month.DEC, 31));
assertEquals(
'2010-01-01 is the week 53 of the previous year', 53,
f(2010, goog.date.month.JAN, 1));
assertEquals(
'2010-01-03 is the week 53 of the previous year', 53,
f(2010, goog.date.month.JAN, 3));
assertEquals('2010-01-04 is the week 1', 1, f(2010, goog.date.month.JAN, 4));
assertEquals(
'2006-01-01 is in week 52 of the following year', 52,
f(2006, goog.date.month.JAN, 1));
assertEquals('2006-01-02 is in week 1', 1, f(2006, goog.date.month.JAN, 2));
assertEquals(
'2006-10-16 is in week 42', 42, f(2006, goog.date.month.OCT, 16));
assertEquals(
'2006-10-19 is in week 42', 42, f(2006, goog.date.month.OCT, 19));
assertEquals(
'2006-10-22 is in week 42', 42, f(2006, goog.date.month.OCT, 22));
assertEquals(
'2006-10-23 is in week 43', 43, f(2006, goog.date.month.OCT, 23));
assertEquals(
'2008-12-29 is in week 1 of the following year', 1,
f(2008, goog.date.month.DEC, 29));
assertEquals(
'2010-01-03 is in week 53 of the previous year', 53,
f(2010, goog.date.month.JAN, 3));
assertEquals('2008-02-01 is in week 5', 5, f(2008, goog.date.month.FEB, 1));
assertEquals('2008-02-04 is in week 6', 6, f(2008, goog.date.month.FEB, 4));
// Tests for different cutoff days.
assertEquals(
'2006-01-01 is in week 52 of the prev. year (cutoff=Monday)', 52,
f(2006, goog.date.month.JAN, 1, goog.date.weekDay.MON));
assertEquals(
'2006-01-01 is in week 1 (cutoff=Sunday)', 1,
f(2006, goog.date.month.JAN, 1, goog.date.weekDay.SUN));
assertEquals(
'2006-12-31 is in week 52 (cutoff=Monday)', 52,
f(2006, goog.date.month.DEC, 31, goog.date.weekDay.MON));
assertEquals(
'2006-12-31 is in week 53 (cutoff=Sunday)', 53,
f(2006, goog.date.month.DEC, 31, goog.date.weekDay.SUN));
assertEquals(
'2007-01-01 is in week 1 (cutoff=Monday)', 1,
f(2007, goog.date.month.JAN, 1, goog.date.weekDay.MON));
assertEquals(
'2007-01-01 is in week 1 (cutoff=Sunday)', 1,
f(2007, goog.date.month.JAN, 1, goog.date.weekDay.SUN));
assertEquals(
'2015-01-01 is in week 52 of the previous year (cutoff=Monday)', 52,
f(2015, goog.date.month.JAN, 1, goog.date.weekDay.MON));
// Tests for leap year 2000.
assertEquals('2000-02-27 is in week 8', 8, f(2000, goog.date.month.FEB, 27));
assertEquals('2000-02-28 is in week 9', 9, f(2000, goog.date.month.FEB, 28));
assertEquals('2000-02-29 is in week 9', 9, f(2000, goog.date.month.FEB, 29));
assertEquals('2000-03-01 is in week 9', 9, f(2000, goog.date.month.MAR, 1));
assertEquals('2000-03-05 is in week 9', 9, f(2000, goog.date.month.MAR, 5));
assertEquals('2000-03-06 is in week 10', 10, f(2000, goog.date.month.MAR, 6));
// Check that week number is strictly incremented by 1.
var dt = new goog.date.Date(2008, goog.date.month.JAN, 1);
for (var i = 0; i < 52; ++i) {
var expected_week = i + 1;
assertEquals(
dt.toUTCIsoString(true) + ' is in week ' + expected_week, expected_week,
dt.getWeekNumber());
dt.add(new goog.date.Interval(goog.date.Interval.DAYS, 7));
}
}
function testFormatMonthAndYear() {
var f = goog.date.formatMonthAndYear;
assertEquals(
'January 2008',
f(goog.i18n.DateTimeSymbols.MONTHS[goog.date.month.JAN], 2008));
assertEquals(
'Jun 2007',
f(goog.i18n.DateTimeSymbols.SHORTMONTHS[goog.date.month.JUN], 2007));
}
//=== tests for goog.date.Date ===
function testIsDateLikeWithGoogDate() {
var jsDate = new Date();
var googDate = new goog.date.Date();
var string = 'foo';
var number = 1;
var nullVar = null;
var notDefined;
assertTrue('js Date should be date-like', goog.isDateLike(jsDate));
assertTrue('goog Date should be date-like', goog.isDateLike(googDate));
assertFalse('string should not be date-like', goog.isDateLike(string));
assertFalse('number should not be date-like', goog.isDateLike(number));
assertFalse('nullVar should not be date-like', goog.isDateLike(nullVar));
assertFalse('undefined should not be date-like', goog.isDateLike(notDefined));
}
function testDateConstructor() {
var date = new goog.date.Date(2001, 2, 3);
assertEquals(2001, date.getFullYear());
assertEquals(2, date.getMonth());
assertEquals(3, date.getDate());
date = new goog.date.Date(2001);
assertEquals(2001, date.getFullYear());
assertEquals(0, date.getMonth());
assertEquals(1, date.getDate());
date = new goog.date.Date(new Date(2001, 2, 3, 4, 5, 6, 7));
assertEquals(2001, date.getFullYear());
assertEquals(2, date.getMonth());
assertEquals(3, date.getDate());
assertEquals(new Date(2001, 2, 3).getTime(), date.getTime());
goog.now = function() { return new Date(2001, 2, 3, 4).getTime(); };
date = new goog.date.Date();
assertEquals(2001, date.getFullYear());
assertEquals(2, date.getMonth());
assertEquals(3, date.getDate());
assertEquals(new Date(2001, 2, 3).getTime(), date.getTime());
}
function testDateConstructor_yearBelow100() {
var date = new goog.date.Date(14, 7, 19);
assertEquals(
'Date constructor should respect passed in full year', 14,
date.getFullYear());
var copied = new goog.date.Date(date);
assertEquals(
'Copying a should return identical date', date.getTime(),
copied.getTime());
assertEquals(
'Full year should be left intact by copying', 14, copied.getFullYear());
// Test boundaries.
assertEquals(-1, new goog.date.Date(-1, 0, 1).getFullYear());
assertEquals(
'There is no year zero, but JS dates accept it', 0,
new goog.date.Date(0, 0, 1).getFullYear());
assertEquals(1, new goog.date.Date(1, 0, 1).getFullYear());
assertEquals(99, new goog.date.Date(99, 0, 1).getFullYear());
assertEquals(100, new goog.date.Date(100, 0, 1).getFullYear());
}
function testDateConstructor_flipOver() {
var date = new goog.date.Date(2012, 12, 1);
assertEquals('20130101', date.toIsoString());
date = new goog.date.Date(12, 12, 1);
assertEquals('130101', date.toIsoString());
}
function testDateToIsoString() {
var d = new goog.date.Date(2006, goog.date.month.JAN, 1);
assertEquals('1 Jan 2006 is 20060101', d.toIsoString(), '20060101');
d = new goog.date.Date(2007, goog.date.month.JUN, 12);
assertEquals('12 Jun 2007 is 20070612', d.toIsoString(), '20070612');
d = new goog.date.Date(2218, goog.date.month.DEC, 31);
assertEquals('31 Dec 2218 is 22181231', d.toIsoString(), '22181231');
}
function testDateTimeFromTimestamp() {
assertEquals(0, goog.date.DateTime.fromTimestamp(0).getTime());
assertEquals(1234, goog.date.DateTime.fromTimestamp(1234).getTime());
}
function testRfc822StringToDate() {
var date = goog.date.DateTime.fromRfc822String('October 2, 2002 8:00:00');
assertNotNull(date);
assertEquals(2002, date.getFullYear());
assertEquals(9, date.getMonth());
assertEquals(2, date.getDate());
assertEquals(8, date.getHours());
assertEquals(0, date.getMinutes());
assertEquals(0, date.getSeconds());
assertEquals(0, date.getMilliseconds());
assertEquals(new Date(2002, 9, 2, 8).getTime(), date.getTime());
date = goog.date.DateTime.fromRfc822String('Sat, 02 Oct 2010 08:00:00 UTC');
assertEquals(2010, date.getFullYear());
assertEquals(9, date.getUTCMonth());
assertEquals(2, date.getUTCDate());
assertEquals(8, date.getUTCHours());
assertEquals(0, date.getUTCMinutes());
assertEquals(0, date.getUTCSeconds());
assertEquals(0, date.getUTCMilliseconds());
date = goog.date.DateTime.fromRfc822String('');
assertEquals(null, date);
date = goog.date.DateTime.fromRfc822String('Invalid Date String');
assertEquals(null, date);
date = goog.date.DateTime.fromRfc822String('Sat, 02 Oct 2010');
assertEquals(2010, date.getFullYear());
assertEquals(9, date.getMonth());
assertEquals(2, date.getDate());
assertEquals(0, date.getHours());
assertEquals(0, date.getMinutes());
assertEquals(0, date.getSeconds());
assertEquals(0, date.getMilliseconds());
assertEquals(new Date(2010, 9, 2).getTime(), date.getTime());
}
function testIsoStringToDate() {
var iso = '20060210T000000Z';
var date = goog.date.fromIsoString(iso);
assertEquals('Got 2006 from ' + iso, 2006, date.getFullYear());
assertEquals('Got February from ' + iso, 1, date.getMonth());
// use getUTCDate() here, since in 'iso' var we specified timezone
// as being a zero offset from GMT
assertEquals('Got 10th from ' + iso, 10, date.getUTCDate());
// YYYY-MM-DD
iso = '2005-02-22';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
assertEquals('Got February from ' + iso, 1, date.getMonth());
assertEquals('Got 22nd from ' + iso, 22, date.getDate());
// YYYYMMDD
iso = '20050222';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
assertEquals('Got February from ' + iso, 1, date.getMonth());
assertEquals('Got 22nd from ' + iso, 22, date.getDate());
// YYYY-MM
iso = '2005-08';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
assertEquals('Got August from ' + iso, 7, date.getMonth());
// YYYYMM
iso = '200502';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
assertEquals('Got February from ' + iso, 1, date.getMonth());
// YYYY
iso = '2005';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
// 1997-W01 or 1997W01
iso = '2005-W22';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
assertEquals('Got May from ' + iso, 4, date.getMonth());
assertEquals('Got 30th from ' + iso, 30, date.getDate());
iso = '2005W22';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
assertEquals('Got May from ' + iso, 4, date.getMonth());
assertEquals('Got 30th from ' + iso, 30, date.getDate());
// 1997-W01-2 or 1997W012
iso = '2005-W22-4';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
assertEquals('Got June from ' + iso, 5, date.getMonth());
assertEquals('Got 2nd from ' + iso, 2, date.getDate());
iso = '2005W224';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
assertEquals('Got June from ' + iso, 5, date.getMonth());
assertEquals('Got 2nd from ' + iso, 2, date.getDate());
iso = '2004-W53-6';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
assertEquals('Got January from ' + iso, 0, date.getMonth());
assertEquals('Got 1st from ' + iso, 1, date.getDate());
iso = '2004-W53-7';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
assertEquals('Got January from ' + iso, 0, date.getMonth());
assertEquals('Got 2nd from ' + iso, 2, date.getDate());
iso = '2005-W52-6';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
assertEquals('Got December from ' + iso, 11, date.getMonth());
assertEquals('Got 31st from ' + iso, 31, date.getDate());
// both years 2007 start with the same day
iso = '2007-W01-1';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2007 from ' + iso, 2007, date.getFullYear());
assertEquals('Got January from ' + iso, 0, date.getMonth());
assertEquals('Got 1st from ' + iso, 1, date.getDate());
iso = '2007-W52-7';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2007 from ' + iso, 2007, date.getFullYear());
assertEquals('Got December from ' + iso, 11, date.getMonth());
assertEquals('Got 30th from ' + iso, 30, date.getDate());
iso = '2008-W01-1';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2007 from ' + iso, 2007, date.getFullYear());
assertEquals('Got December from ' + iso, 11, date.getMonth());
assertEquals('Got 31st from ' + iso, 31, date.getDate());
// Gregorian year 2008 is a leap year,
// ISO year 2008 is 2 days shorter:
// 1 day longer at the start, 3 days shorter at the end
iso = '2008-W01-2';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2008 from ' + iso, 2008, date.getFullYear());
assertEquals('Got Jan from ' + iso, 0, date.getMonth());
assertEquals('Got 1st from ' + iso, 1, date.getDate());
// ISO year is three days into the previous gregorian year
iso = '2009-W01-1';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2008 from ' + iso, 2008, date.getFullYear());
assertEquals('Got December from ' + iso, 11, date.getMonth());
assertEquals('Got 29th from ' + iso, 29, date.getDate());
iso = '2009-W01-3';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2008 from ' + iso, 2008, date.getFullYear());
assertEquals('Got December from ' + iso, 11, date.getMonth());
assertEquals('Got 31st from ' + iso, 31, date.getDate());
iso = '2009-W01-4';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2009 from ' + iso, 2009, date.getFullYear());
assertEquals('Got January from ' + iso, 0, date.getMonth());
assertEquals('Got 1st from ' + iso, 1, date.getDate());
// ISO year is three days into the next gregorian year
iso = '2009-W53-4';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2009 from ' + iso, 2009, date.getFullYear());
assertEquals('Got December from ' + iso, 11, date.getMonth());
assertEquals('Got 31st from ' + iso, 31, date.getDate());
iso = '2009-W53-5';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2010 from ' + iso, 2010, date.getFullYear());
assertEquals('Got January from ' + iso, 0, date.getMonth());
assertEquals('Got 1st from ' + iso, 1, date.getDate());
iso = '2009-W53-6';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2010 from ' + iso, 2010, date.getFullYear());
assertEquals('Got January from ' + iso, 0, date.getMonth());
assertEquals('Got 2nd from ' + iso, 2, date.getDate());
iso = '2009-W53-7';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2010 from ' + iso, 2010, date.getFullYear());
assertEquals('Got January from ' + iso, 0, date.getMonth());
assertEquals('Got 3rd from ' + iso, 3, date.getDate());
iso = '2010-W01-1';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2010 from ' + iso, 2010, date.getFullYear());
assertEquals('Got January from ' + iso, 0, date.getMonth());
assertEquals('Got 4th from ' + iso, 4, date.getDate());
// Examples where the ISO year is three days into the previous gregorian year
// 1995-035 or 1995035
iso = '2005-146';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
assertEquals('Got May from ' + iso, 4, date.getMonth());
assertEquals('Got 26th from ' + iso, 26, date.getDate());
iso = '2005146';
date = goog.date.fromIsoString(iso);
assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
assertEquals('Got May from ' + iso, 4, date.getMonth());
assertEquals('Got 26th from ' + iso, 26, date.getDate());
}
function testDate_fromIsoString() {
// YYYY-MM-DD
var iso = '2005-02-22';
var date = goog.date.Date.fromIsoString(iso);
assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
assertEquals('Got February from ' + iso, 1, date.getMonth());
assertEquals('Got 22nd from ' + iso, 22, date.getDate());
// YYYY-MM-DDTHH:MM:SS
iso = '2005-02-22T11:22:33';
date = goog.date.Date.fromIsoString(iso);
assertNull('Got null from ' + iso, date);
}
function testDateTime_fromIsoString() {
// YYYY-MM-DD
var iso = '2005-02-22';
var date = goog.date.DateTime.fromIsoString(iso);
assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
assertEquals('Got February from ' + iso, 1, date.getMonth());
assertEquals('Got 22nd from ' + iso, 22, date.getDate());
assertEquals('Got 0 hours from ' + iso, 0, date.getHours());
assertEquals('Got 0 minutes from ' + iso, 0, date.getMinutes());
assertEquals('Got 0 seconds from ' + iso, 0, date.getSeconds());
// YYYY-MM-DDTHH:MM:SS
iso = '2005-02-22T11:22:33';
date = goog.date.DateTime.fromIsoString(iso);
assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
assertEquals('Got February from ' + iso, 1, date.getMonth());
assertEquals('Got 22nd from ' + iso, 22, date.getDate());
assertEquals('Got 11 hours from ' + iso, 11, date.getHours());
assertEquals('Got 22 minutes from ' + iso, 22, date.getMinutes());
assertEquals('Got 33 seconds from ' + iso, 33, date.getSeconds());
}
// test private function used by goog.date.Date.toIsoString()
function test_setIso8601TimeOnly_() {
if (goog.userAgent.product.SAFARI) {
// TODO(b/20733468): Disabled so we can get the rest of the Closure test
// suite running in a continuous build. Will investigate later.
return;
}
// 23:59:59
var d = new goog.date.DateTime(0, 0);
var iso = '18:46:39';
assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
assertEquals('Got 18 hours from ' + iso, 18, d.getHours());
assertEquals('Got 46 minutes from ' + iso, 46, d.getMinutes());
assertEquals('Got 39 seconds from ' + iso, 39, d.getSeconds());
// 235959
d = new goog.date.DateTime(0, 0);
iso = '184639';
assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
assertEquals('Got 18 hours from ' + iso, 18, d.getHours());
assertEquals('Got 46 minutes from ' + iso, 46, d.getMinutes());
assertEquals('Got 39 seconds from ' + iso, 39, d.getSeconds());
// 23:59, 2359, or 23
d = new goog.date.DateTime(0, 0);
iso = '18:46';
assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
assertEquals('Got 18 hours from ' + iso, 18, d.getHours());
assertEquals('Got 46 minutes from ' + iso, 46, d.getMinutes());
d = new goog.date.DateTime(0, 0);
iso = '1846';
assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
assertEquals('Got 18 hours from ' + iso, 18, d.getHours());
assertEquals('Got 46 minutes from ' + iso, 46, d.getMinutes());
d = new goog.date.DateTime(0, 0);
iso = '18';
assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
assertEquals('Got 18 hours from ' + iso, 18, d.getHours());
d = new goog.date.DateTime(0, 0);
iso = '18463';
assertFalse('failed to parse ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
assertTrue('date did not change', d.equals(new goog.date.DateTime(0, 0)));
// 23:59:59.9942 or 235959.9942
d = new goog.date.DateTime(0, 0);
iso = '18:46:39.9942';
assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
assertEquals('Got 18 hours from ' + iso, 18, d.getHours());
assertEquals('Got 46 minutes from ' + iso, 46, d.getMinutes());
assertEquals('Got 39 seconds from ' + iso, 39, d.getSeconds());
// Fails in Safari4 Winxp, temporarily disabled
expectedFailures.expectFailureFor(isWinxpSafari4());
try {
if (goog.userAgent.WEBKIT && goog.userAgent.MAC) {
// Both Safari 3.1 and WebKit (on Mac) return floating-point values.
assertRoughlyEquals(
'Got roughly 994.2 milliseconds from ' + iso, 994.2,
d.getMilliseconds(), 0.01);
} else {
// Other browsers, including WebKit on Windows, return integers.
assertEquals(
'Got 994 milliseconds from ' + iso, 994, d.getMilliseconds());
}
d = new goog.date.DateTime(0, 0);
iso = '184639.9942';
assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
assertEquals('Got 18 hours from ' + iso, 18, d.getHours());
assertEquals('Got 46 minutes from ' + iso, 46, d.getMinutes());
assertEquals('Got 39 seconds from ' + iso, 39, d.getSeconds());
if (goog.userAgent.WEBKIT && goog.userAgent.MAC) {
// Both Safari 3.1 and WebKit (on Mac) return floating-point values.
assertRoughlyEquals(
'Got roughly 994.2 milliseconds from ' + iso, 994.2,
d.getMilliseconds(), 0.01);
} else {
// Other browsers, including WebKit on Windows, return integers.
assertEquals(
'Got 994 milliseconds from ' + iso, 994, d.getMilliseconds());
}
} catch (e) {
expectedFailures.handleException(e);
}
// 1995-02-04 24:00 = 1995-02-05 00:00
// timezone tests
var offset = new Date().getTimezoneOffset() / 60;
d = new goog.date.DateTime(0, 0);
iso = '18:46:39+07:00';
assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
assertEquals('Got an 11-hour GMT offset from ' + iso, 11, d.getUTCHours());
d = new goog.date.DateTime(0, 0);
iso = '18:46:39+00:00';
assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
assertEquals('Got an 18-hour GMT offset from ' + iso, 18, d.getUTCHours());
d = new goog.date.DateTime(0, 0);
iso = '16:46:39-07:00';
assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
assertEquals('Got a 23-hour GMT offset from ' + iso, 23, d.getUTCHours());
}
function testDateIntervalAdd() {
// -1m, cross year boundary
var d = new goog.date.Date(2007, goog.date.month.JAN, 1);
d.add(new goog.date.Interval(goog.date.Interval.MONTHS, -1));
assertEquals('2007-01-01 - 1m = 2006-12-01', '20061201', d.toIsoString());
// +1y2m3d
d = new goog.date.Date(2006, goog.date.month.JAN, 1);
d.add(new goog.date.Interval(1, 2, 3));
assertEquals('2006-01-01 + 1y2m3d = 2007-03-04', '20070304', d.toIsoString());
// -1y2m3d (negative interval)
d = new goog.date.Date(2007, goog.date.month.MAR, 4);
d.add(new goog.date.Interval(-1, -2, -3));
assertEquals('2007-03-04 - 1y2m3d = 2006-01-01', '20060101', d.toIsoString());
// 2007-12-30 + 3d (roll over to next year)
d = new goog.date.Date(2007, goog.date.month.DEC, 30);
d.add(new goog.date.Interval(goog.date.Interval.DAYS, 3));
assertEquals('2007-12-30 + 3d = 2008-01-02', '20080102', d.toIsoString());
// 2004-03-01 - 1d (handle leap year)
d = new goog.date.Date(2004, goog.date.month.MAR, 1);
d.add(new goog.date.Interval(goog.date.Interval.DAYS, -1));
assertEquals('2004-03-01 - 1d = 2004-02-29', '20040229', d.toIsoString());
// 2004-02-29 + 1y (stays at end of Feb, doesn't roll to Mar)
d = new goog.date.Date(2004, goog.date.month.FEB, 29);
d.add(new goog.date.Interval(goog.date.Interval.YEARS, 1));
assertEquals('2004-02-29 + 1y = 2005-02-28', '20050228', d.toIsoString());
// 2004-02-29 - 1y (stays at end of Feb, doesn't roll to Mar)
d = new goog.date.Date(2004, goog.date.month.FEB, 29);
d.add(new goog.date.Interval(goog.date.Interval.YEARS, -1));
assertEquals('2004-02-29 - 1y = 2003-02-28', '20030228', d.toIsoString());
// 2003-02-28 + 1y (stays at 28, doesn't go to leap year end of Feb)
d = new goog.date.Date(2003, goog.date.month.FEB, 28);
d.add(new goog.date.Interval(goog.date.Interval.YEARS, 1));
assertEquals('2003-02-28 + 1y = 2004-02-28', '20040228', d.toIsoString());
// 2005-02-28 - 1y (stays at 28, doesn't go to leap year end of Feb)
d = new goog.date.Date(2005, goog.date.month.FEB, 28);
d.add(new goog.date.Interval(goog.date.Interval.YEARS, -1));
assertEquals('2005-02-28 - 1y = 2004-02-28', '20040228', d.toIsoString());
// 2003-01-31 + 1y (stays at end of Jan, standard case)
d = new goog.date.Date(2003, goog.date.month.JAN, 31);
d.add(new goog.date.Interval(goog.date.Interval.YEARS, 1));
assertEquals('2003-01-31 + 1y = 2004-01-31', '20040131', d.toIsoString());
// 2005-01-31 - 1y (stays at end of Jan, standard case)
d = new goog.date.Date(2005, goog.date.month.JAN, 31);
d.add(new goog.date.Interval(goog.date.Interval.YEARS, -1));
assertEquals('2005-01-31 - 1y = 2004-01-31', '20040131', d.toIsoString());
// 2006-01-31 + 1m (stays at end of Feb, doesn't roll to Mar, non-leap-year)
d = new goog.date.Date(2006, goog.date.month.JAN, 31);
d.add(new goog.date.Interval(goog.date.Interval.MONTHS, 1));
assertEquals('2006-01-31 + 1m = 2006-02-28', '20060228', d.toIsoString());
// 2004-02-29 + 1m (stays at 29, standard case)
d = new goog.date.Date(2004, goog.date.month.FEB, 29);
d.add(new goog.date.Interval(goog.date.Interval.MONTHS, +1));
assertEquals('2004-02-29 + 1m = 2004-03-29', '20040329', d.toIsoString());
// 2004-02-29 - 1m (stays at 29, standard case)
d = new goog.date.Date(2004, goog.date.month.FEB, 29);
d.add(new goog.date.Interval(goog.date.Interval.MONTHS, -1));
assertEquals('2004-02-29 - 1m = 2004-01-29', '20040129', d.toIsoString());
// 2004-01-30 + 1m (snaps to Feb 29)
d = new goog.date.Date(2004, goog.date.month.JAN, 30);
d.add(new goog.date.Interval(goog.date.Interval.MONTHS, 1));
assertEquals('2004-01-30 + 1m = 2004-02-29', '20040229', d.toIsoString());
// 2004-03-30 - 1m (snaps to Feb 29)
d = new goog.date.Date(2004, goog.date.month.MAR, 30);
d.add(new goog.date.Interval(goog.date.Interval.MONTHS, -1));
assertEquals('2004-03-30 - 1m = 2004-02-29', '20040229', d.toIsoString());
// 2004-03-30 + 1m (stays at 30, standard case)
d = new goog.date.Date(2004, goog.date.month.MAR, 30);
d.add(new goog.date.Interval(goog.date.Interval.MONTHS, 1));
assertEquals('2004-03-30 + 1m = 2004-04-30', '20040430', d.toIsoString());
// 2008-10-30 + 1d (roll over to 31).
d = new goog.date.Date(2008, goog.date.month.OCT, 30);
d.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
assertEquals('2008-10-30 + 1d = 2008-10-31', '20081031', d.toIsoString());
// 2008-10-31 + 1d (roll over to November, not December).
d = new goog.date.Date(2008, goog.date.month.OCT, 31);
d.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
assertEquals('2008-10-31 + 1d = 2008-11-01', '20081101', d.toIsoString());
// 2008-10-17 + 1d (Brasilia dst).
d = new goog.date.Date(2008, goog.date.month.OCT, 17);
d.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
assertEquals('2008-10-17 + 1d = 2008-10-18', '20081018', d.toIsoString());
// 2008-10-18 + 1d (Brasilia dst).
d = new goog.date.Date(2008, goog.date.month.OCT, 18);
d.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
assertEquals('2008-10-18 + 1d = 2008-10-19', '20081019', d.toIsoString());
// 2008-10-19 + 1d (Brasilia dst).
d = new goog.date.Date(2008, goog.date.month.OCT, 19);
d.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
assertEquals('2008-10-19 + 1d = 2008-10-20', '20081020', d.toIsoString());
// 2008-02-16 + 1d (Brasilia dst).
d = new goog.date.Date(2008, goog.date.month.FEB, 16);
d.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
assertEquals('2008-02-16 + 1d = 2008-02-17', '20080217', d.toIsoString());
// 2008-02-17 + 1d (Brasilia dst).
d = new goog.date.Date(2008, goog.date.month.FEB, 17);
d.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
assertEquals('2008-02-17 + 1d = 2008-02-18', '20080218', d.toIsoString());
}
function testDateEquals() {
var d1 = new goog.date.Date(2004, goog.date.month.MAR, 1);
var d2 = new goog.date.Date(2004, goog.date.month.MAR, 1);
assertFalse('d1 != null', d1.equals(null));
assertFalse('d2 != undefined', d2.equals(undefined));
assertTrue('d1 == d2', d1.equals(d2));
assertTrue('d2 == d1', d2.equals(d1));
d1 = new goog.date.Date(2005, goog.date.month.MAR, 1);
d2 = new goog.date.Date(2004, goog.date.month.MAR, 1);
assertFalse('different year', d1.equals(d2));
d1 = new goog.date.Date(2004, goog.date.month.FEB, 1);
d2 = new goog.date.Date(2004, goog.date.month.MAR, 1);
assertFalse('different month', d1.equals(d2));
d1 = new goog.date.Date(2004, goog.date.month.MAR, 2);
d2 = new goog.date.Date(2004, goog.date.month.MAR, 1);
assertFalse('different date', d1.equals(d2));
// try passing in DateTime, time fields should be ignored
d1 = new goog.date.Date(2004, goog.date.month.MAR, 1);
d2 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30);
assertTrue('using goog.date.DateTime, same date', d1.equals(d2));
}
function testDateTimeConstructor() {
var date = new goog.date.DateTime(2001, 2, 3, 4, 5, 6, 7);
assertEquals(2001, date.getFullYear());
assertEquals(2, date.getMonth());
assertEquals(3, date.getDate());
assertEquals(4, date.getHours());
assertEquals(5, date.getMinutes());
assertEquals(6, date.getSeconds());
assertEquals(7, date.getMilliseconds());
assertEquals(new Date(2001, 2, 3, 4, 5, 6, 7).getTime(), date.getTime());
date = new goog.date.DateTime(2001);
assertEquals(2001, date.getFullYear());
assertEquals(0, date.getMonth());
assertEquals(1, date.getDate());
assertEquals(0, date.getHours());
assertEquals(0, date.getMinutes());
assertEquals(0, date.getSeconds());
assertEquals(0, date.getMilliseconds());
date = new goog.date.DateTime(new Date(2001, 2, 3, 4, 5, 6, 7));
assertEquals(2001, date.getFullYear());
assertEquals(2, date.getMonth());
assertEquals(3, date.getDate());
assertEquals(4, date.getHours());
assertEquals(5, date.getMinutes());
assertEquals(6, date.getSeconds());
assertEquals(7, date.getMilliseconds());
assertEquals(new Date(2001, 2, 3, 4, 5, 6, 7).getTime(), date.getTime());
goog.now = function() { return new Date(2001, 2, 3, 4).getTime(); };
date = new goog.date.DateTime();
assertEquals(2001, date.getFullYear());
assertEquals(2, date.getMonth());
assertEquals(3, date.getDate());
assertEquals(4, date.getHours());
assertEquals(0, date.getMinutes());
assertEquals(0, date.getSeconds());
assertEquals(0, date.getMilliseconds());
assertEquals(new Date(2001, 2, 3, 4).getTime(), date.getTime());
date = new goog.date.DateTime(new Date('October 2, 2002 8:00:00'));
assertEquals(2002, date.getFullYear());
assertEquals(9, date.getMonth());
assertEquals(2, date.getDate());
assertEquals(8, date.getHours());
assertEquals(0, date.getMinutes());
assertEquals(0, date.getSeconds());
assertEquals(0, date.getMilliseconds());
assertEquals(new Date(2002, 9, 2, 8).getTime(), date.getTime());
}
function testDateTimeEquals() {
var d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30);
var d2 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30);
assertTrue('d1 == d2', d1.equals(d2));
assertTrue('d2 == d1', d2.equals(d1));
d1 = new goog.date.DateTime(2007, goog.date.month.JAN, 1);
d2 = new goog.date.DateTime(); // today
assertFalse('different date', d1.equals(d2));
d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1);
d2 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12);
assertFalse('different hours', d1.equals(d2));
d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 29, 30);
d2 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30);
assertFalse('different minutes', d1.equals(d2));
d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 29);
d2 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30);
assertFalse('different seconds', d1.equals(d2));
d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30, 500);
d2 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30, 500);
assertTrue('same milliseconds', d1.equals(d2));
d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30, 499);
d2 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30, 500);
assertFalse('different milliseconds', d1.equals(d2));
// try milliseconds again, this time comparing against a native Date
d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30);
d2 = new Date(2004, 2, 1, 12, 30, 30, 999);
assertFalse('different milliseconds, native Date', d1.equals(d2));
// pass in a goog.date.Date rather than a goog.date.DateTime
d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30);
d2 = new goog.date.Date(2004, goog.date.month.MAR, 1);
assertFalse('using goog.date.Date, different times', d1.equals(d2));
d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 0, 0, 0);
d2 = new goog.date.Date(2004, goog.date.month.MAR, 1);
assertTrue('using goog.date.Date, same time (midnight)', d1.equals(d2));
}
function testIntervalIsZero() {
assertTrue('zero interval', new goog.date.Interval().isZero());
var i = new goog.date.Interval(0, 0, 1, -24, 0, 0);
assertFalse('1 day minus 24 hours is not considered zero', i.isZero());
}
function testIntervalGetInverse() {
var i1 = new goog.date.Interval(goog.date.Interval.DAYS, -1);
var i2 = i1.getInverse();
var d = new goog.date.Date(2004, goog.date.month.MAR, 1);
d.add(i1);
d.add(i2);
var label = '2004-03-01 - 1d + 1d = 2004-03-01';
assertEquals(label, d.toIsoString(), '20040301');
i1 = new goog.date.Interval(1, 2, 3);
i2 = i1.getInverse();
d = new goog.date.Date(2004, goog.date.month.MAR, 1);
d.add(i1);
d.add(i2);
label = '2004-03-01 - 1y2m3d + 1y2m3d = 2004-03-01';
assertEquals(label, d.toIsoString(), '20040301');
}
function testIntervalTimes() {
var i = new goog.date.Interval(1, 2, 3, 4, 5, 6);
var expected = new goog.date.Interval(2, 4, 6, 8, 10, 12);
assertTrue('double interval', expected.equals(i.times(2)));
}
//=== tests for goog.date.Interval.equals() ===
function testIntervalEquals() {
var i1 = new goog.date.Interval(goog.date.Interval.DAYS, -1);
var i2 = new goog.date.Interval(goog.date.Interval.DAYS, -1);
assertTrue('-1d == -1d, aka i1 == i2', i1.equals(i2));
assertTrue('-1d == -1d, aka i2 == i1', i2.equals(i1));
i1 = new goog.date.Interval(goog.date.Interval.DAYS, -1);
i2 = new goog.date.Interval(goog.date.Interval.DAYS, 1);
assertFalse('-1d != +1d, aka i1 == i2', i1.equals(i2));
assertFalse('-1d != +1d, aka i2 == i1', i2.equals(i1));
i1 = new goog.date.Interval(0, 3); // Three months
i2 = new goog.date.Interval(goog.date.Interval.MONTHS, 3);
assertTrue('3m == 3m, aka i1 == i2', i1.equals(i2));
assertTrue('3m == 3m, aka i2 == i1', i2.equals(i1));
// 1 year, 6 months, 15 days, 12 hours, 30 minutes, 30 seconds
i1 = new goog.date.Interval(1, 6, 15, 12, 30, 30);
i2 = new goog.date.Interval(1, 6, 15, 12, 30, 30);
assertTrue('1y6m15d12h30M30s == 1y6m15d12h30M30s', i1.equals(i2));
assertTrue('1y6m15d12h30M30s == 1y6m15d12h30M30s', i2.equals(i1));
}
//=== tests for adding two goog.date.Interval intervals ===
function testIntervalIntervalAdd() {
var i1 = new goog.date.Interval(1, 6, 15, 12, 30, 30);
var i2 = new goog.date.Interval(0, 3, 20, 10, 50, -25);
i1.add(i2);
assertTrue('i1 + i2', i1.equals(new goog.date.Interval(1, 9, 35, 22, 80, 5)));
i1 = new goog.date.Interval(1, 6, 15, 12, 30, 30);
i2 = new goog.date.Interval(0, 3, 20, 10, 50, -25);
i2.add(i1);
assertTrue('i2 + i1', i2.equals(new goog.date.Interval(1, 9, 35, 22, 80, 5)));
i1 = new goog.date.Interval(1, 6, 15, 12, 30, 30);
i2 = i1.getInverse();
i1.add(i2);
assertTrue('i1 + (-i1)', i1.equals(new goog.date.Interval()));
i1 = new goog.date.Interval(goog.date.Interval.DAYS, 1);
i2 = new goog.date.Interval(0, -2, -2);
i1.add(i2);
assertTrue('1d + (-2m-2d)', i1.equals(new goog.date.Interval(0, -2, -1)));
}
//=== tests conversion to and from ISO 8601 duration string ===
function testIsoDuration() {
var interval1 = new goog.date.Interval(123, 456, 678, 11, 12, 455.5);
var duration1 = 'P123Y456M678DT11H12M455.5S';
assertTrue(
'parse full duration',
interval1.equals(goog.date.Interval.fromIsoString(duration1)));
assertEquals('create full duration', duration1, interval1.toIsoString());
var interval2 = new goog.date.Interval(123);
var duration2 = 'P123Y';
var duration2v = 'P123Y0M0DT0H0M0S';
assertTrue(
'parse year',
interval2.equals(goog.date.Interval.fromIsoString(duration2)));
assertEquals('create year', duration2, interval2.toIsoString());
assertEquals('create year, verbose', duration2v, interval2.toIsoString(true));
var interval3 = new goog.date.Interval(0, 0, 0, 11, 12, 40);
var duration3 = 'PT11H12M40S';
var duration3v = 'P0Y0M0DT11H12M40S';
assertTrue(
'parse time duration',
interval3.equals(goog.date.Interval.fromIsoString(duration3)));
assertEquals('create time duration', duration3, interval3.toIsoString());
assertEquals(
'create time duration, verbove', duration3v, interval3.toIsoString(true));
var interval4 = new goog.date.Interval(7, 8, 9, 1, 2, 4);
var duration4 = 'P7Y8M9DT1H2M4S';
assertTrue(
'parse one-digit duration',
interval4.equals(goog.date.Interval.fromIsoString(duration4)));
assertEquals('create one-digit duration', duration4, interval4.toIsoString());
var interval5 = new goog.date.Interval(-123, -456, -678, -11, -12, -455.5);
var duration5 = '-P123Y456M678DT11H12M455.5S';
assertTrue(
'parse full negative duration',
interval5.equals(goog.date.Interval.fromIsoString(duration5)));
assertEquals(
'create full negative duration', duration5, interval5.toIsoString());
var interval6 = new goog.date.Interval(0, 0, -1);
var duration6 = '-P1D';
var duration6v = '-P0Y0M1DT0H0M0S';
assertTrue(
'parse partial negative duration',
interval6.equals(goog.date.Interval.fromIsoString(duration6)));
assertEquals(
'create partial negative duration', duration6, interval6.toIsoString());
assertEquals(
'create partial negative duration, verbose', duration6v,
interval6.toIsoString(true));
var interval7 = new goog.date.Interval(0, 0, 9, 0, 0, 4);
var duration7 = 'P9DT4S';
var duration7v = 'P0Y0M9DT0H0M4S';
assertTrue(
'parse partial one-digit duration',
interval7.equals(goog.date.Interval.fromIsoString(duration7)));
assertTrue(
'parse partial one-digit duration, verbose',
interval7.equals(goog.date.Interval.fromIsoString(duration7v)));
assertEquals(
'create partial one-digit duration', duration7, interval7.toIsoString());
assertEquals(
'create partial one-digit duration, verbose', duration7v,
interval7.toIsoString(true));
var interval8 = new goog.date.Interval(1, -1, 1, -1, 1, -1);
assertNull('create mixed sign duration', interval8.toIsoString());
var duration9 = '1Y1M1DT1H1M1S';
assertNull('missing P', goog.date.Interval.fromIsoString(duration9));
var duration10 = 'P1Y1M1D1H1M1S';
assertNull('missing T', goog.date.Interval.fromIsoString(duration10));
var duration11 = 'P1Y1M1DT';
assertNull('extra T', goog.date.Interval.fromIsoString(duration11));
var duration12 = 'PT.5S';
assertNull(
'invalid seconds, missing integer part',
goog.date.Interval.fromIsoString(duration12));
var duration13 = 'PT1.S';
assertNull(
'invalid seconds, missing fractional part',
goog.date.Interval.fromIsoString(duration13));
}
function testGetTotalSeconds() {
var duration = new goog.date.Interval(0, 0, 2, 3, 4, 5);
assertEquals(
'seconds in 2d3h4m5s', 2 * 86400 + 3 * 3600 + 4 * 60 + 5,
duration.getTotalSeconds());
}
function testIsDateLikeWithGoogDateTime() {
var jsDate = new Date();
var googDate = new goog.date.DateTime();
var string = 'foo';
var number = 1;
var nullVar = null;
var notDefined;
assertTrue('js Date should be date-like', goog.isDateLike(jsDate));
assertTrue('goog Date should be date-like', goog.isDateLike(googDate));
assertFalse('string should not be date-like', goog.isDateLike(string));
assertFalse('number should not be date-like', goog.isDateLike(number));
assertFalse('nullVar should not be date-like', goog.isDateLike(nullVar));
assertFalse('undefined should not be date-like', goog.isDateLike(notDefined));
}
function testToUTCRfc3339String() {
var date = goog.date.fromIsoString('19850412T232050Z');
date.setUTCMilliseconds(52);
assertEquals(date.toUTCRfc3339String(), '1985-04-12T23:20:50.052Z');
assertNotEquals(
'Diverges from ISO 8601', date.toUTCRfc3339String(),
date.toUTCIsoString(true, true));
var date = goog.date.fromIsoString('19901231T235959Z');
assertEquals(date.toUTCRfc3339String(), '1990-12-31T23:59:59Z');
assertNotEquals(
'Diverges from ISO 8601', date.toUTCRfc3339String(),
date.toUTCIsoString(true, true));
var date = goog.date.fromIsoString('19370101T120027Z');
date.setUTCMilliseconds(87);
assertEquals(date.toUTCRfc3339String(), '1937-01-01T12:00:27.087Z');
assertNotEquals(
'Diverges from ISO 8601', date.toUTCRfc3339String(),
date.toUTCIsoString(true, true));
}
function testDateTimezone() {
var d = new goog.date.DateTime(2006, 1, 1, 12, 0, 0);
d.add(
new goog.date.Interval(
goog.date.Interval.MINUTES, d.getTimezoneOffset()));
var d2 = new goog.date.DateTime(2006, 1, 1, 12, 0, 0);
assertEquals(
'Compensate for timezone and compare with UTC date/time',
d.toIsoString(true), d2.toUTCIsoString(true));
}
function testToUsTimeString() {
var doPad = true;
var doShowPm = true;
var dontPad = false;
var dontShowPm = false;
// 12am
var d = new goog.date.DateTime(2007, 1, 14);
assertEquals('12am test 1', '12:00 AM', d.toUsTimeString());
assertEquals('12am test 2', '12:00 AM', d.toUsTimeString(doPad));
assertEquals('12am test 3', '12:00 AM', d.toUsTimeString(dontPad));
assertEquals('12am test 4', '12:00 AM', d.toUsTimeString(doPad, doShowPm));
assertEquals('12am test 5', '00:00', d.toUsTimeString(doPad, dontShowPm));
assertEquals('12am test 6', '12:00 AM', d.toUsTimeString(dontPad, doShowPm));
assertEquals('12am test 7', '0:00', d.toUsTimeString(dontPad, dontShowPm));
// 9am
d = new goog.date.DateTime(2007, 1, 14, 9);
assertEquals('9am test 1', '9:00 AM', d.toUsTimeString());
assertEquals('9am test 2', '09:00 AM', d.toUsTimeString(doPad));
assertEquals('9am test 3', '9:00 AM', d.toUsTimeString(dontPad));
assertEquals('9am test 4', '09:00 AM', d.toUsTimeString(doPad, doShowPm));
assertEquals('9am test 5', '09:00', d.toUsTimeString(doPad, dontShowPm));
assertEquals('9am test 6', '9:00 AM', d.toUsTimeString(dontPad, doShowPm));
assertEquals('9am test 7', '9:00', d.toUsTimeString(dontPad, dontShowPm));
// 12pm
d = new goog.date.DateTime(2007, 1, 14, 12);
assertEquals('12pm test 1', '12:00 PM', d.toUsTimeString());
assertEquals('12pm test 2', '12:00 PM', d.toUsTimeString(doPad));
assertEquals('12pm test 3', '12:00 PM', d.toUsTimeString(dontPad));
assertEquals('12pm test 4', '12:00 PM', d.toUsTimeString(doPad, doShowPm));
assertEquals('12pm test 5', '12:00', d.toUsTimeString(doPad, dontShowPm));
assertEquals('12pm test 6', '12:00 PM', d.toUsTimeString(dontPad, doShowPm));
assertEquals('12pm test 7', '12:00', d.toUsTimeString(dontPad, dontShowPm));
// 6 PM
d = new goog.date.DateTime(2007, 1, 14, 18);
assertEquals('6 PM test 1', '6:00 PM', d.toUsTimeString());
assertEquals('6 PM test 2', '06:00 PM', d.toUsTimeString(doPad));
assertEquals('6 PM test 3', '6:00 PM', d.toUsTimeString(dontPad));
assertEquals('6 PM test 4', '06:00 PM', d.toUsTimeString(doPad, doShowPm));
assertEquals('6 PM test 5', '06:00', d.toUsTimeString(doPad, dontShowPm));
assertEquals('6 PM test 6', '6:00 PM', d.toUsTimeString(dontPad, doShowPm));
assertEquals('6 PM test 7', '6:00', d.toUsTimeString(dontPad, dontShowPm));
// 6:01 PM
d = new goog.date.DateTime(2007, 1, 14, 18, 1);
assertEquals('6:01 PM test 1', '6:01 PM', d.toUsTimeString());
assertEquals('6:01 PM test 2', '06:01 PM', d.toUsTimeString(doPad));
assertEquals('6:01 PM test 3', '6:01 PM', d.toUsTimeString(dontPad));
assertEquals('6:01 PM test 4', '06:01 PM', d.toUsTimeString(doPad, doShowPm));
assertEquals('6:01 PM test 5', '06:01', d.toUsTimeString(doPad, dontShowPm));
assertEquals(
'6:01 PM test 6', '6:01 PM', d.toUsTimeString(dontPad, doShowPm));
assertEquals('6:01 PM test 7', '6:01', d.toUsTimeString(dontPad, dontShowPm));
// 6:35 PM
d = new goog.date.DateTime(2007, 1, 14, 18, 35);
assertEquals('6:35 PM test 1', '6:35 PM', d.toUsTimeString());
assertEquals('6:35 PM test 2', '06:35 PM', d.toUsTimeString(doPad));
assertEquals('6:35 PM test 3', '6:35 PM', d.toUsTimeString(dontPad));
assertEquals('6:35 PM test 4', '06:35 PM', d.toUsTimeString(doPad, doShowPm));
assertEquals('6:35 PM test 5', '06:35', d.toUsTimeString(doPad, dontShowPm));
assertEquals(
'6:35 PM test 6', '6:35 PM', d.toUsTimeString(dontPad, doShowPm));
assertEquals('6:35 PM test 7', '6:35', d.toUsTimeString(dontPad, dontShowPm));
// omit zero minutes
d = new goog.date.DateTime(2007, 1, 14, 18);
assertEquals(
'omit zero 1', '6:00 PM', d.toUsTimeString(dontPad, doShowPm, false));
assertEquals(
'omit zero 2', '6 PM', d.toUsTimeString(dontPad, doShowPm, true));
// but don't omit zero minutes if not actually zero minutes
d = new goog.date.DateTime(2007, 1, 14, 18, 1);
assertEquals(
'omit zero 3', '6:01 PM', d.toUsTimeString(dontPad, doShowPm, false));
assertEquals(
'omit zero 4', '6:01 PM', d.toUsTimeString(dontPad, doShowPm, true));
}
function testToIsoTimeString() {
// 00:00
var d = new goog.date.DateTime(2007, 1, 14);
assertEquals('00:00', '00:00:00', d.toIsoTimeString());
// 09:00
d = new goog.date.DateTime(2007, 1, 14, 9);
assertEquals('09:00', '09:00:00', d.toIsoTimeString());
// 12:00
d = new goog.date.DateTime(2007, 1, 14, 12);
assertEquals('12:00', '12:00:00', d.toIsoTimeString());
// 18:00
d = new goog.date.DateTime(2007, 1, 14, 18);
assertEquals('18:00', '18:00:00', d.toIsoTimeString());
// 18:01
d = new goog.date.DateTime(2007, 1, 14, 18, 1);
assertEquals('18:01', '18:01:00', d.toIsoTimeString());
// 18:35
d = new goog.date.DateTime(2007, 1, 14, 18, 35);
assertEquals('18:35', '18:35:00', d.toIsoTimeString());
// 18:35:01
d = new goog.date.DateTime(2007, 1, 14, 18, 35, 1);
assertEquals('18:35:01', '18:35:01', d.toIsoTimeString());
// 18:35:11
d = new goog.date.DateTime(2007, 1, 14, 18, 35, 11);
assertEquals('18:35:11', '18:35:11', d.toIsoTimeString());
// 18:35:11 >> 18:35
d = new goog.date.DateTime(2007, 1, 14, 18, 35, 11);
assertEquals('18:35:11 no secs', '18:35', d.toIsoTimeString(false));
}
function testToXmlDateTimeString() {
var d = new goog.date.DateTime(2007, 1, 14);
assertEquals('2007-02-14', '2007-02-14T00:00:00', d.toXmlDateTime());
d = new goog.date.DateTime(2007, 1, 14, 18, 35, 1);
assertEquals(
'2007-02-14, 8:35:01, timezone==undefined', '2007-02-14T18:35:01',
d.toXmlDateTime());
d = new goog.date.DateTime(2007, 1, 14, 18, 35, 1);
assertEquals(
'2007-02-14, 8:35:01, timezone==false', '2007-02-14T18:35:01',
d.toXmlDateTime(false));
d = new goog.date.DateTime(2007, 1, 14, 18, 35, 1);
assertEquals(
'2007-02-14, 8:35:01, timezone==true',
'2007-02-14T18:35:01' + d.getTimezoneOffsetString(),
d.toXmlDateTime(true));
}
function testClone() {
var d = new goog.date.DateTime(2007, 1, 14, 18, 35, 1);
var d2 = d.clone();
assertTrue('datetimes equal', d.equals(d2));
d = new goog.date.DateTime(2007, 1, 14, 18, 35, 1, 310);
d2 = d.clone();
assertTrue('datetimes with milliseconds equal', d.equals(d2));
d = new goog.date.Date(2007, 1, 14);
d2 = d.clone();
assertTrue('dates equal', d.equals(d2));
// 1 year, 6 months, 15 days, 12 hours, 30 minutes, 30 seconds
var i = new goog.date.Interval(1, 6, 15, 12, 30, 30);
var i2 = i.clone();
assertTrue('intervals equal', i.equals(i2));
i = new goog.date.Interval(goog.date.Interval.DAYS, -1);
i2 = i.clone();
assertTrue('day intervals equal', i.equals(i2));
// Brasilia dst
d = new goog.date.Date(2008, goog.date.month.OCT, 18);
d.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
d2 = d.clone();
assertTrue('dates equal', d.equals(d2));
}
function testValueOf() {
var date1 = new goog.date.DateTime(2008, 11, 26, 15, 40, 0);
var date2 = new goog.date.Date(2008, 11, 27);
var date3 = new goog.date.DateTime(2008, 11, 26, 15, 40, 1);
var nativeDate = new Date();
nativeDate.setFullYear(2008, 11, 26);
nativeDate.setHours(15, 40, 0, 0);
assertEquals(date1.valueOf(), nativeDate.valueOf());
assertFalse(date1 < date1);
assertTrue(date1 <= date1);
assertTrue(date1 < date2);
assertTrue(date2 > date3);
}
function isWinxpSafari4() {
return goog.userAgent.product.SAFARI &&
goog.userAgent.product.isVersion('4') &&
!goog.userAgent.product.isVersion('5') && goog.userAgent.WINDOWS &&
goog.userAgent.platform.isVersion('5.0') &&
!goog.userAgent.platform.isVersion('6.0');
}
function testDateCompare() {
// May 16th, 2011, 3:17:36.500
var date1 =
new goog.date.DateTime(2011, goog.date.month.MAY, 16, 15, 17, 36, 500);
// May 16th, 2011, 3:17:36.501
var date2 =
new goog.date.DateTime(2011, goog.date.month.MAY, 16, 15, 17, 36, 501);
// May 16th, 2011, 3:17:36.501
var date3 =
new goog.date.DateTime(2011, goog.date.month.MAY, 16, 15, 17, 36, 502);
assertEquals(0, goog.date.Date.compare(date1.clone(), date1.clone()));
assertEquals(-1, goog.date.Date.compare(date1, date2));
assertEquals(1, goog.date.Date.compare(date2, date1));
var dates = [date2, date3, date1];
goog.array.sort(dates, goog.date.Date.compare);
assertArrayEquals(
'Dates should be sorted in time.', [date1, date2, date3], dates);
// Assert a known millisecond difference between two points in time.
assertEquals(
-19129478,
goog.date.Date.compare(
new goog.date.DateTime(1982, goog.date.month.MAR, 12, 6, 48, 32, 354),
new goog.date.DateTime(
1982, goog.date.month.MAR, 12, 12, 7, 21, 832)));
// Test dates before the year 0. Dates are Talk Like a Pirate Day, and
// Towel Day, 300 B.C. (and before pirates).
var pirateDay = new goog.date.Date(-300, goog.date.month.SEP, 2);
var towelDay = new goog.date.Date(-300, goog.date.month.MAY, 12);
assertEquals(
'Dates should be 113 days apart.', 113 * 24 * 60 * 60 * 1000,
goog.date.Date.compare(pirateDay, towelDay));
}
function testDateCompareDateLikes() {
var nativeDate = new Date(2011, 4, 16, 15, 17, 36, 500);
var closureDate =
new goog.date.DateTime(2011, goog.date.month.MAY, 16, 15, 17, 36, 500);
assertEquals(0, goog.date.Date.compare(nativeDate, closureDate));
nativeDate.setMilliseconds(499);
assertEquals(-1, goog.date.Date.compare(nativeDate, closureDate));
nativeDate.setMilliseconds(501);
assertEquals(1, goog.date.Date.compare(nativeDate, closureDate));
}
function testIsMidnight() {
assertTrue(new goog.date.DateTime(2013, 0, 1).isMidnight());
assertFalse(new goog.date.DateTime(2013, 0, 1, 1).isMidnight());
assertFalse(new goog.date.DateTime(2013, 0, 1, 0, 1).isMidnight());
assertFalse(new goog.date.DateTime(2013, 0, 1, 0, 0, 1).isMidnight());
assertFalse(new goog.date.DateTime(2013, 0, 1, 0, 0, 0, 1).isMidnight());
}
function testMinMax() {
// Comparison of two goog.date.DateTimes
var dateTime1 = new goog.date.DateTime(2000, 0, 1);
var dateTime2 = new goog.date.DateTime(2000, 0, 1, 0, 0, 0, 1);
assertEquals(dateTime1, goog.date.min(dateTime1, dateTime2));
assertEquals(dateTime1, goog.date.min(dateTime2, dateTime1));
assertEquals(dateTime2, goog.date.max(dateTime1, dateTime2));
assertEquals(dateTime2, goog.date.max(dateTime2, dateTime1));
// Comparison of two goog.date.Dates
var date1 = new goog.date.Date(2000, 0, 1);
var date2 = new goog.date.Date(2000, 0, 2);
assertEquals(date1, goog.date.min(date1, date2));
// Comparison of native Dates.
var jsDate1 = new Date(2000, 0, 1);
var jsDate2 = new Date(2000, 0, 2);
assertEquals(jsDate1, goog.date.min(jsDate1, jsDate2));
assertEquals(jsDate2, goog.date.max(jsDate1, jsDate2));
// Comparison of different types.
assertEquals(date1, goog.date.min(date1, dateTime2));
assertEquals(dateTime2, goog.date.min(date2, dateTime2));
assertEquals(date1, goog.date.min(date1, jsDate2));
assertEquals(jsDate2, goog.date.max(dateTime1, jsDate2));
}
function testDateTimeIntervalAdd() {
// Add hours
var d = new goog.date.DateTime(2007, goog.date.month.JAN, 1, 10, 20, 30);
d.add(new goog.date.Interval(goog.date.Interval.HOURS, 10));
assertEquals(20, d.getHours());
// Add negative hours
d.add(new goog.date.Interval(goog.date.Interval.HOURS, -5));
assertEquals(15, d.getHours());
// Add hours to the next day
d.add(new goog.date.Interval(goog.date.Interval.HOURS, 10));
assertEquals(2, d.getDay());
assertEquals(1, d.getHours());
// Add minutes
d = new goog.date.DateTime(2007, goog.date.month.JAN, 1, 22, 20, 30);
d.add(new goog.date.Interval(goog.date.Interval.MINUTES, 10));
assertEquals(30, d.getMinutes());
// Add negative minutes
d.add(new goog.date.Interval(goog.date.Interval.MINUTES, -5));
assertEquals(25, d.getMinutes());
// Add minutes to the next day
d.add(new goog.date.Interval(goog.date.Interval.MINUTES, 130));
assertEquals(2, d.getDay());
assertEquals(0, d.getHours());
assertEquals(35, d.getMinutes());
// Add seconds
d = new goog.date.DateTime(2007, goog.date.month.JAN, 1, 23, 45, 30);
d.add(new goog.date.Interval(goog.date.Interval.SECONDS, 10));
assertEquals(40, d.getSeconds());
// Add negative seconds
d.add(new goog.date.Interval(goog.date.Interval.SECONDS, -5));
assertEquals(35, d.getSeconds());
// Add seconds to the next day
d.add(new goog.date.Interval(goog.date.Interval.SECONDS, 1200));
assertEquals(2, d.getDay());
assertEquals(0, d.getHours());
assertEquals(5, d.getMinutes());
assertEquals(35, d.getSeconds());
// Test daylight savings day 2015-11-1
d = new goog.date.DateTime(2015, goog.date.month.NOV, 1, 0, 50, 30);
d.add(new goog.date.Interval(goog.date.Interval.MINUTES, 15));
assertEquals(1, d.getHours());
assertEquals(5, d.getMinutes());
d.add(new goog.date.Interval(goog.date.Interval.HOURS, 1));
assertEquals(1, d.getHours());
// Test daylight savings day 2015-3-8
d = new goog.date.DateTime(2015, goog.date.month.MAR, 8, 0, 50, 30);
d.add(new goog.date.Interval(goog.date.Interval.MINUTES, 15));
assertEquals(1, d.getHours());
assertEquals(5, d.getMinutes());
d.add(new goog.date.Interval(goog.date.Interval.HOURS, 1));
assertEquals(3, d.getHours());
}
|