relative_test.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.LOCALE = 'en_US';
  15. goog.provide('goog.date.relativeTest');
  16. goog.setTestOnly('goog.date.relativeTest');
  17. goog.require('goog.date.DateTime');
  18. goog.require('goog.date.relative');
  19. goog.require('goog.i18n.DateTimeFormat');
  20. goog.require('goog.testing.jsunit');
  21. // Timestamp to base times for test on.
  22. var baseTime = new Date(2009, 2, 23, 14, 31, 6).getTime();
  23. // Ensure goog.now returns a constant timestamp.
  24. goog.now = function() {
  25. return baseTime;
  26. };
  27. function testFormatRelativeForPastDates() {
  28. var fn = goog.date.relative.format;
  29. assertEquals(
  30. 'Should round seconds to the minute below', '0 minutes ago',
  31. fn(timestamp('23 March 2009 14:30:10')));
  32. assertEquals(
  33. 'Should round seconds to the minute below', '1 minute ago',
  34. fn(timestamp('23 March 2009 14:29:56')));
  35. assertEquals(
  36. 'Should round seconds to the minute below', '2 minutes ago',
  37. fn(timestamp('23 March 2009 14:29:00')));
  38. assertEquals('10 minutes ago', fn(timestamp('23 March 2009 14:20:10')));
  39. assertEquals('59 minutes ago', fn(timestamp('23 March 2009 13:31:42')));
  40. assertEquals('2 hours ago', fn(timestamp('23 March 2009 12:20:56')));
  41. assertEquals('23 hours ago', fn(timestamp('22 March 2009 15:30:56')));
  42. assertEquals('1 day ago', fn(timestamp('22 March 2009 12:11:04')));
  43. assertEquals('1 day ago', fn(timestamp('22 March 2009 00:00:00')));
  44. assertEquals('2 days ago', fn(timestamp('21 March 2009 23:59:59')));
  45. assertEquals('2 days ago', fn(timestamp('21 March 2009 10:30:56')));
  46. assertEquals('2 days ago', fn(timestamp('21 March 2009 00:00:00')));
  47. assertEquals('3 days ago', fn(timestamp('20 March 2009 23:59:59')));
  48. }
  49. function testFormatRelativeForFutureDates() {
  50. var fn = goog.date.relative.format;
  51. assertEquals(
  52. 'Should round seconds to the minute below', 'in 1 minute',
  53. fn(timestamp('23 March 2009 14:32:05')));
  54. assertEquals(
  55. 'Should round seconds to the minute below', 'in 2 minutes',
  56. fn(timestamp('23 March 2009 14:33:00')));
  57. assertEquals('in 10 minutes', fn(timestamp('23 March 2009 14:40:10')));
  58. assertEquals('in 59 minutes', fn(timestamp('23 March 2009 15:29:15')));
  59. assertEquals('in 2 hours', fn(timestamp('23 March 2009 17:20:56')));
  60. assertEquals('in 23 hours', fn(timestamp('24 March 2009 13:30:56')));
  61. assertEquals('in 1 day', fn(timestamp('24 March 2009 14:31:07')));
  62. assertEquals('in 1 day', fn(timestamp('24 March 2009 16:11:04')));
  63. assertEquals('in 1 day', fn(timestamp('24 March 2009 23:59:59')));
  64. assertEquals('in 2 days', fn(timestamp('25 March 2009 00:00:00')));
  65. assertEquals('in 2 days', fn(timestamp('25 March 2009 10:30:56')));
  66. assertEquals('in 2 days', fn(timestamp('25 March 2009 23:59:59')));
  67. assertEquals('in 3 days', fn(timestamp('26 March 2009 00:00:00')));
  68. }
  69. function testFormatPast() {
  70. var fn = goog.date.relative.formatPast;
  71. assertEquals('59 minutes ago', fn(timestamp('23 March 2009 13:31:42')));
  72. assertEquals('0 minutes ago', fn(timestamp('23 March 2009 14:32:05')));
  73. assertEquals('0 minutes ago', fn(timestamp('23 March 2009 14:33:00')));
  74. assertEquals('0 minutes ago', fn(timestamp('25 March 2009 10:30:56')));
  75. }
  76. function testFormatDay() {
  77. var fn = goog.date.relative.formatDay;
  78. var formatter =
  79. new goog.i18n.DateTimeFormat(goog.i18n.DateTimeFormat.Format.SHORT_DATE);
  80. var format = goog.bind(formatter.format, formatter);
  81. assertEquals('Sep 25', fn(timestamp('25 September 2009 10:31:06')));
  82. assertEquals('Mar 25', fn(timestamp('25 March 2009 00:12:19')));
  83. assertEquals('Tomorrow', fn(timestamp('24 March 2009 10:31:06')));
  84. assertEquals('Tomorrow', fn(timestamp('24 March 2009 00:12:19')));
  85. assertEquals('Today', fn(timestamp('23 March 2009 10:31:06')));
  86. assertEquals('Today', fn(timestamp('23 March 2009 00:12:19')));
  87. assertEquals('Yesterday', fn(timestamp('22 March 2009 23:48:12')));
  88. assertEquals('Yesterday', fn(timestamp('22 March 2009 04:11:23')));
  89. assertEquals('Mar 21', fn(timestamp('21 March 2009 15:54:45')));
  90. assertEquals('Mar 19', fn(timestamp('19 March 2009 01:22:11')));
  91. // Test that a formatter can also be accepted as input.
  92. assertEquals('Today', fn(timestamp('23 March 2009 10:31:06'), format));
  93. assertEquals('Today', fn(timestamp('23 March 2009 00:12:19'), format));
  94. assertEquals('Yesterday', fn(timestamp('22 March 2009 23:48:12'), format));
  95. assertEquals('Yesterday', fn(timestamp('22 March 2009 04:11:23'), format));
  96. assertEquals(
  97. format(gdatetime(timestamp('21 March 2009 15:54:45'))),
  98. fn(timestamp('21 March 2009 15:54:45'), format));
  99. assertEquals(
  100. format(gdatetime(timestamp('19 March 2009 01:22:11'))),
  101. fn(timestamp('19 March 2009 01:22:11'), format));
  102. }
  103. function testGetDateString() {
  104. var fn = goog.date.relative.getDateString;
  105. assertEquals(
  106. '2:21 PM (10 minutes ago)', fn(new Date(baseTime - 10 * 60 * 1000)));
  107. assertEquals(
  108. '4:31 AM (10 hours ago)', fn(new Date(baseTime - 10 * 60 * 60 * 1000)));
  109. assertEquals(
  110. 'Friday, March 13, 2009 (10 days ago)',
  111. fn(new Date(baseTime - 10 * 24 * 60 * 60 * 1000)));
  112. assertEquals(
  113. 'Tuesday, March 3, 2009',
  114. fn(new Date(baseTime - 20 * 24 * 60 * 60 * 1000)));
  115. // Test that goog.date.DateTime can also be accepted as input.
  116. assertEquals(
  117. '2:21 PM (10 minutes ago)', fn(gdatetime(baseTime - 10 * 60 * 1000)));
  118. assertEquals(
  119. '4:31 AM (10 hours ago)', fn(gdatetime(baseTime - 10 * 60 * 60 * 1000)));
  120. assertEquals(
  121. 'Friday, March 13, 2009 (10 days ago)',
  122. fn(gdatetime(baseTime - 10 * 24 * 60 * 60 * 1000)));
  123. assertEquals(
  124. 'Tuesday, March 3, 2009',
  125. fn(gdatetime(baseTime - 20 * 24 * 60 * 60 * 1000)));
  126. }
  127. function testGetPastDateString() {
  128. var fn = goog.date.relative.getPastDateString;
  129. assertEquals(
  130. '2:21 PM (10 minutes ago)', fn(new Date(baseTime - 10 * 60 * 1000)));
  131. assertEquals(
  132. '2:41 PM (0 minutes ago)', fn(new Date(baseTime + 10 * 60 * 1000)));
  133. // Test that goog.date.DateTime can also be accepted as input.
  134. assertEquals(
  135. '2:21 PM (10 minutes ago)', fn(gdatetime(baseTime - 10 * 60 * 1000)));
  136. assertEquals(
  137. '2:41 PM (0 minutes ago)', fn(gdatetime(baseTime + 10 * 60 * 1000)));
  138. }
  139. function gdatetime(timestamp) {
  140. return new goog.date.DateTime(new Date(timestamp));
  141. }
  142. function timestamp(str) {
  143. return new Date(str).getTime();
  144. }