parse-test.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. "use strict";
  2. require("should");
  3. var dateFormat = require("../lib");
  4. describe("dateFormat.parse", function() {
  5. it("should require a pattern", function() {
  6. (function() {
  7. dateFormat.parse();
  8. }.should.throw(/pattern must be supplied/));
  9. (function() {
  10. dateFormat.parse(null);
  11. }.should.throw(/pattern must be supplied/));
  12. (function() {
  13. dateFormat.parse("");
  14. }.should.throw(/pattern must be supplied/));
  15. });
  16. describe("with a pattern that has no replacements", function() {
  17. it("should return a new date when the string matches", function() {
  18. dateFormat.parse("cheese", "cheese").should.be.a.Date();
  19. });
  20. it("should throw if the string does not match", function() {
  21. (function() {
  22. dateFormat.parse("cheese", "biscuits");
  23. }.should.throw(/String 'biscuits' could not be parsed as 'cheese'/));
  24. });
  25. });
  26. describe("with a full pattern", function() {
  27. var pattern = "yyyy-MM-dd hh:mm:ss.SSSO";
  28. it("should return the correct date if the string matches", function() {
  29. var testDate = new Date();
  30. testDate.setUTCFullYear(2018);
  31. testDate.setUTCMonth(8);
  32. testDate.setUTCDate(13);
  33. testDate.setUTCHours(18);
  34. testDate.setUTCMinutes(10);
  35. testDate.setUTCSeconds(12);
  36. testDate.setUTCMilliseconds(392);
  37. dateFormat
  38. .parse(pattern, "2018-09-14 04:10:12.392+1000")
  39. .getTime()
  40. .should.eql(testDate.getTime())
  41. ;
  42. });
  43. it("should throw if the string does not match", function() {
  44. (function() {
  45. dateFormat.parse(pattern, "biscuits");
  46. }.should.throw(
  47. /String 'biscuits' could not be parsed as 'yyyy-MM-dd hh:mm:ss.SSSO'/
  48. ));
  49. });
  50. });
  51. describe("with a partial pattern", function() {
  52. var testDate = new Date();
  53. dateFormat.now = function() {
  54. return testDate;
  55. };
  56. /**
  57. * If there's no timezone in the format, then we verify against the local date
  58. */
  59. function verifyLocalDate(actual, expected) {
  60. actual.getFullYear().should.eql(expected.year || testDate.getFullYear());
  61. actual.getMonth().should.eql(expected.month || testDate.getMonth());
  62. actual.getDate().should.eql(expected.day || testDate.getDate());
  63. actual.getHours().should.eql(expected.hours || testDate.getHours());
  64. actual.getMinutes().should.eql(expected.minutes || testDate.getMinutes());
  65. actual.getSeconds().should.eql(expected.seconds || testDate.getSeconds());
  66. actual
  67. .getMilliseconds()
  68. .should.eql(expected.milliseconds || testDate.getMilliseconds());
  69. }
  70. /**
  71. * If a timezone is specified, let's verify against the UTC time it is supposed to be
  72. */
  73. function verifyDate(actual, expected) {
  74. actual.getUTCFullYear().should.eql(expected.year || testDate.getUTCFullYear());
  75. actual.getUTCMonth().should.eql(expected.month || testDate.getUTCMonth());
  76. actual.getUTCDate().should.eql(expected.day || testDate.getUTCDate());
  77. actual.getUTCHours().should.eql(expected.hours || testDate.getUTCHours());
  78. actual.getUTCMinutes().should.eql(expected.minutes || testDate.getUTCMinutes());
  79. actual.getUTCSeconds().should.eql(expected.seconds || testDate.getUTCSeconds());
  80. actual
  81. .getMilliseconds()
  82. .should.eql(expected.milliseconds || testDate.getMilliseconds());
  83. }
  84. it("should return a date with missing values defaulting to current time", function() {
  85. var date = dateFormat.parse("yyyy-MM", "2015-09");
  86. verifyLocalDate(date, { year: 2015, month: 8 });
  87. });
  88. it("should use a passed in date for missing values", function() {
  89. var missingValueDate = new Date(2010, 1, 8, 22, 30, 12, 100);
  90. var date = dateFormat.parse("yyyy-MM", "2015-09", missingValueDate);
  91. verifyLocalDate(date, {
  92. year: 2015,
  93. month: 8,
  94. day: 8,
  95. hours: 22,
  96. minutes: 30,
  97. seconds: 12,
  98. milliseconds: 100
  99. });
  100. });
  101. it("should handle variations on the same pattern", function() {
  102. var date = dateFormat.parse("MM-yyyy", "09-2015");
  103. verifyLocalDate(date, { year: 2015, month: 8 });
  104. date = dateFormat.parse("yyyy MM", "2015 09");
  105. verifyLocalDate(date, { year: 2015, month: 8 });
  106. date = dateFormat.parse("MM, yyyy.", "09, 2015.");
  107. verifyLocalDate(date, { year: 2015, month: 8 });
  108. });
  109. describe("should match all the date parts", function() {
  110. it("works with dd", function() {
  111. var date = dateFormat.parse("dd", "21");
  112. verifyLocalDate(date, { day: 21 });
  113. });
  114. it("works with hh", function() {
  115. var date = dateFormat.parse("hh", "12");
  116. verifyLocalDate(date, { hours: 12 });
  117. });
  118. it("works with mm", function() {
  119. var date = dateFormat.parse("mm", "34");
  120. verifyLocalDate(date, { minutes: 34 });
  121. });
  122. it("works with ss", function() {
  123. var date = dateFormat.parse("ss", "59");
  124. verifyLocalDate(date, { seconds: 59 });
  125. });
  126. it("works with ss.SSS", function() {
  127. var date = dateFormat.parse("ss.SSS", "23.452");
  128. verifyLocalDate(date, { seconds: 23, milliseconds: 452 });
  129. });
  130. it("works with hh:mm O (+1000)", function() {
  131. var date = dateFormat.parse("hh:mm O", "05:23 +1000");
  132. verifyDate(date, { hours: 19, minutes: 23 });
  133. });
  134. it("works with hh:mm O (-200)", function() {
  135. var date = dateFormat.parse("hh:mm O", "05:23 -200");
  136. verifyDate(date, { hours: 7, minutes: 23 });
  137. });
  138. it("works with hh:mm O (+0930)", function() {
  139. var date = dateFormat.parse("hh:mm O", "05:23 +0930");
  140. verifyDate(date, { hours: 19, minutes: 53 });
  141. });
  142. });
  143. });
  144. describe("with a date formatted by this library", function() {
  145. describe("should format and then parse back to the same date", function() {
  146. function testDateInitWithUTC() {
  147. var td = new Date();
  148. td.setUTCFullYear(2018);
  149. td.setUTCMonth(8);
  150. td.setUTCDate(13);
  151. td.setUTCHours(18);
  152. td.setUTCMinutes(10);
  153. td.setUTCSeconds(12);
  154. td.setUTCMilliseconds(392);
  155. return td;
  156. }
  157. it("works with ISO8601_WITH_TZ_OFFSET_FORMAT", function() {
  158. // For this test case to work, the date object must be initialized with
  159. // UTC timezone
  160. var td = testDateInitWithUTC();
  161. var d = dateFormat(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, td);
  162. dateFormat.parse(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, d)
  163. .should.eql(td);
  164. });
  165. it("works with ISO8601_FORMAT", function() {
  166. var td = new Date();
  167. var d = dateFormat(dateFormat.ISO8601_FORMAT, td);
  168. var actual = dateFormat.parse(dateFormat.ISO8601_FORMAT, d);
  169. actual.should.eql(td);
  170. });
  171. it("works with DATETIME_FORMAT", function() {
  172. var testDate = new Date();
  173. dateFormat
  174. .parse(
  175. dateFormat.DATETIME_FORMAT,
  176. dateFormat(dateFormat.DATETIME_FORMAT, testDate)
  177. )
  178. .should.eql(testDate);
  179. });
  180. it("works with ABSOLUTETIME_FORMAT", function() {
  181. var testDate = new Date();
  182. dateFormat
  183. .parse(
  184. dateFormat.ABSOLUTETIME_FORMAT,
  185. dateFormat(dateFormat.ABSOLUTETIME_FORMAT, testDate)
  186. )
  187. .should.eql(testDate);
  188. });
  189. });
  190. });
  191. });