utcdatetime_test.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2009 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.date.UtcDateTimeTest');
  15. goog.setTestOnly('goog.date.UtcDateTimeTest');
  16. goog.require('goog.date.Interval');
  17. goog.require('goog.date.UtcDateTime');
  18. goog.require('goog.date.month');
  19. goog.require('goog.date.weekDay');
  20. goog.require('goog.testing.jsunit');
  21. function testConstructor() {
  22. goog.now = function() { return new Date(2001, 2, 3, 4).getTime(); };
  23. var d = new goog.date.UtcDateTime();
  24. assertTrue('default constructor', d.equals(new Date(goog.now())));
  25. var d = new goog.date.UtcDateTime(2001);
  26. assertTrue('year only', d.equals(new Date(Date.UTC(2001, 0, 1, 0, 0, 0))));
  27. var d = new goog.date.UtcDateTime(2001, 2, 3, 4, 5, 6, 7);
  28. assertTrue(
  29. 'full date/time', d.equals(new Date(Date.UTC(2001, 2, 3, 4, 5, 6, 7))));
  30. var d = new goog.date.UtcDateTime(new Date(0));
  31. assertTrue(
  32. 'copy constructor', d.equals(new Date(Date.UTC(1970, 0, 1, 0, 0, 0))));
  33. }
  34. function testClone() {
  35. var d = new goog.date.UtcDateTime(2001, 2, 3, 4, 5, 6, 7);
  36. assertTrue('clone of UtcDateTime', d.equals(d.clone()));
  37. }
  38. function testAdd() {
  39. var date = new goog.date.UtcDateTime(2007, goog.date.month.OCT, 5);
  40. date.add(new goog.date.Interval(-1, 2));
  41. var expected = new goog.date.UtcDateTime(2006, goog.date.month.DEC, 5);
  42. assertTrue('UTC date + years + months', expected.equals(date));
  43. var date = new goog.date.UtcDateTime(2007, goog.date.month.OCT, 1);
  44. date.add(new goog.date.Interval(0, 0, 60));
  45. var expected = new goog.date.UtcDateTime(2007, goog.date.month.NOV, 30);
  46. assertTrue('UTC date + days', expected.equals(date));
  47. var date = new goog.date.UtcDateTime(2007, goog.date.month.OCT, 1);
  48. date.add(new goog.date.Interval(0, 0, 0, 60 * 24 - 12, -30, -30.5));
  49. var expected =
  50. new goog.date.UtcDateTime(2007, goog.date.month.NOV, 29, 11, 29, 29, 500);
  51. assertTrue('UTC date + time, daylight saving ignored', expected.equals(date));
  52. }
  53. function testGetYear() {
  54. var date = new goog.date.UtcDateTime(2000, goog.date.month.JAN, 1);
  55. assertEquals('year of 2000-01-01 00:00:00', 2000, date.getYear());
  56. var date = new goog.date.UtcDateTime(1999, goog.date.month.DEC, 31, 23, 59);
  57. assertEquals('year of 1999-12-31 23:59:00', 1999, date.getYear());
  58. }
  59. function testGetDay() {
  60. var date = new goog.date.UtcDateTime(2000, goog.date.month.JAN, 1);
  61. assertEquals(
  62. '2000-01-01 00:00:00 is Saturday (UTC + ISO)', goog.date.weekDay.SAT,
  63. date.getUTCIsoWeekday());
  64. assertEquals(
  65. '2000-01-01 00:00:00 is Saturday (ISO)', goog.date.weekDay.SAT,
  66. date.getIsoWeekday());
  67. assertEquals('2000-01-01 00:00:00 is Saturday (UTC)', 6, date.getUTCDay());
  68. assertEquals('2000-01-01 00:00:00 is Saturday', 6, date.getDay());
  69. var date = new goog.date.UtcDateTime(2000, goog.date.month.JAN, 1, 23, 59);
  70. assertEquals(
  71. '2000-01-01 23:59:00 is Saturday (UTC + ISO)', goog.date.weekDay.SAT,
  72. date.getUTCIsoWeekday());
  73. assertEquals(
  74. '2000-01-01 23:59:00 is Saturday (ISO)', goog.date.weekDay.SAT,
  75. date.getIsoWeekday());
  76. assertEquals('2000-01-01 23:59:00 is Saturday (UTC)', 6, date.getUTCDay());
  77. assertEquals('2000-01-01 23:59:00 is Saturday', 6, date.getDay());
  78. }
  79. function testFromIsoString() {
  80. var dateString = '2000-01-02';
  81. var date = goog.date.UtcDateTime.fromIsoString(dateString);
  82. var exp = new goog.date.UtcDateTime(2000, goog.date.month.JAN, 2);
  83. assertTrue('parsed ISO date', exp.equals(date));
  84. var dateTimeString = '2000-01-02 03:04:05';
  85. var dateTime = goog.date.UtcDateTime.fromIsoString(dateTimeString);
  86. var exp = new goog.date.UtcDateTime(2000, goog.date.month.JAN, 2, 3, 4, 5);
  87. assertTrue('parsed ISO date/time', exp.equals(dateTime));
  88. }
  89. function testToIsoString() {
  90. var date = new goog.date.UtcDateTime(2000, goog.date.month.JAN, 2, 3, 4, 5);
  91. assertEquals(
  92. 'serialize date/time', '2000-01-02 03:04:05', date.toIsoString(true));
  93. assertEquals('serialize time only', '03:04:05', date.toIsoTimeString(true));
  94. assertEquals(
  95. 'serialize date/time to XML', '2000-01-02T03:04:05',
  96. date.toXmlDateTime());
  97. }
  98. function testIsMidnight() {
  99. assertTrue(new goog.date.UtcDateTime(2000, 0, 1).isMidnight());
  100. assertFalse(new goog.date.UtcDateTime(2000, 0, 1, 0, 0, 0, 1).isMidnight());
  101. }
  102. function testFromTimestamp() {
  103. assertEquals(0, goog.date.UtcDateTime.fromTimestamp(0).getTime());
  104. assertEquals(1234, goog.date.UtcDateTime.fromTimestamp(1234).getTime());
  105. }