parse-test.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.setFullYear(2018);
  31. testDate.setMonth(8);
  32. testDate.setDate(13);
  33. testDate.setHours(18);
  34. testDate.setMinutes(10);
  35. testDate.setSeconds(12);
  36. testDate.setMilliseconds(392);
  37. testDate.getTimezoneOffset = function() {
  38. return 600;
  39. };
  40. dateFormat
  41. .parse(pattern, "2018-09-13 08:10:12.392+1000")
  42. .getTime()
  43. .should.eql(testDate.getTime());
  44. });
  45. it("should throw if the string does not match", function() {
  46. (function() {
  47. dateFormat.parse(pattern, "biscuits");
  48. }.should.throw(
  49. /String 'biscuits' could not be parsed as 'yyyy-MM-dd hh:mm:ss.SSSO'/
  50. ));
  51. });
  52. });
  53. describe("with a partial pattern", function() {
  54. var testDate = new Date();
  55. dateFormat.now = function() {
  56. return testDate;
  57. };
  58. function verifyDate(actual, expected) {
  59. actual.getFullYear().should.eql(expected.year || testDate.getFullYear());
  60. actual.getMonth().should.eql(expected.month || testDate.getMonth());
  61. actual.getDate().should.eql(expected.day || testDate.getDate());
  62. actual.getHours().should.eql(expected.hours || testDate.getHours());
  63. actual.getMinutes().should.eql(expected.minutes || testDate.getMinutes());
  64. actual.getSeconds().should.eql(expected.seconds || testDate.getSeconds());
  65. actual
  66. .getMilliseconds()
  67. .should.eql(expected.milliseconds || testDate.getMilliseconds());
  68. }
  69. it("should return a date with missing values defaulting to current time", function() {
  70. var date = dateFormat.parse("yyyy-MM", "2015-09");
  71. verifyDate(date, { year: 2015, month: 8 });
  72. });
  73. it("should use a passed in date for missing values", function() {
  74. var missingValueDate = new Date(2010, 1, 11, 10, 30, 12, 100);
  75. var date = dateFormat.parse("yyyy-MM", "2015-09", missingValueDate);
  76. verifyDate(date, {
  77. year: 2015,
  78. month: 8,
  79. day: 11,
  80. hours: 10,
  81. minutes: 30,
  82. seconds: 12,
  83. milliseconds: 100
  84. });
  85. });
  86. it("should handle variations on the same pattern", function() {
  87. var date = dateFormat.parse("MM-yyyy", "09-2015");
  88. verifyDate(date, { year: 2015, month: 8 });
  89. date = dateFormat.parse("yyyy MM", "2015 09");
  90. verifyDate(date, { year: 2015, month: 8 });
  91. date = dateFormat.parse("MM, yyyy.", "09, 2015.");
  92. verifyDate(date, { year: 2015, month: 8 });
  93. });
  94. it("should match all the date parts", function() {
  95. var date = dateFormat.parse("dd", "21");
  96. verifyDate(date, { day: 21 });
  97. date = dateFormat.parse("hh", "12");
  98. verifyDate(date, { hours: 12 });
  99. date = dateFormat.parse("mm", "34");
  100. verifyDate(date, { minutes: 34 });
  101. date = dateFormat.parse("ss", "59");
  102. verifyDate(date, { seconds: 59 });
  103. date = dateFormat.parse("ss.SSS", "23.452");
  104. verifyDate(date, { seconds: 23, milliseconds: 452 });
  105. date = dateFormat.parse("hh:mm O", "05:23 +1000");
  106. verifyDate(date, { hours: 15, minutes: 23 });
  107. date = dateFormat.parse("hh:mm O", "05:23 -200");
  108. verifyDate(date, { hours: 3, minutes: 23 });
  109. date = dateFormat.parse("hh:mm O", "05:23 +0930");
  110. verifyDate(date, { hours: 14, minutes: 53 });
  111. });
  112. });
  113. describe("with a date formatted by this library", function() {
  114. var testDate = new Date();
  115. testDate.setUTCFullYear(2018);
  116. testDate.setUTCMonth(8);
  117. testDate.setUTCDate(13);
  118. testDate.setUTCHours(18);
  119. testDate.setUTCMinutes(10);
  120. testDate.setUTCSeconds(12);
  121. testDate.setUTCMilliseconds(392);
  122. it("should format and then parse back to the same date", function() {
  123. dateFormat
  124. .parse(
  125. dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT,
  126. dateFormat(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, testDate)
  127. )
  128. .should.eql(testDate);
  129. dateFormat
  130. .parse(
  131. dateFormat.ISO8601_FORMAT,
  132. dateFormat(dateFormat.ISO8601_FORMAT, testDate)
  133. )
  134. .should.eql(testDate);
  135. dateFormat
  136. .parse(
  137. dateFormat.DATETIME_FORMAT,
  138. dateFormat(dateFormat.DATETIME_FORMAT, testDate)
  139. )
  140. .should.eql(testDate);
  141. dateFormat
  142. .parse(
  143. dateFormat.ABSOLUTETIME_FORMAT,
  144. dateFormat(dateFormat.ABSOLUTETIME_FORMAT, testDate)
  145. )
  146. .should.eql(testDate);
  147. });
  148. });
  149. });