parseISO.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. var total = 0, accepted = 0, failed = 0;
  6. echo("////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////");
  7. echo("// Definitely valid ISO strings");
  8. echo("");
  9. echo("// Auto-generated");
  10. echo("");
  11. initializeGenerateDateStrings();
  12. var yearDigits = 0, monthDigits = 0, dayDigits = 0, hourMinuteDigits = 0, secondDigits = 0, millisecondDigits = 0;
  13. for (yearDigits = 4; yearDigits <= 6; yearDigits += 2) {
  14. dayDigits = monthDigits = 0;
  15. runGenerateTestWithValidTime();
  16. monthDigits = 2;
  17. runGenerateTestWithValidTime();
  18. dayDigits = 2;
  19. runGenerateTestWithValidTime();
  20. }
  21. function runGenerateTestWithValidTime() {
  22. millisecondDigits = secondDigits = hourMinuteDigits = 0;
  23. runGenerateTest();
  24. hourMinuteDigits = 2;
  25. runGenerateTest();
  26. secondDigits = 2;
  27. runGenerateTest();
  28. millisecondDigits = 3;
  29. runGenerateTest();
  30. }
  31. writeStats();
  32. echo("////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////");
  33. echo("// Definitely invalid ISO strings");
  34. echo("");
  35. echo("// Field value outside valid range");
  36. echo("");
  37. runTest("0001-00-01T01:01:01.001Z");
  38. runTest("0001-13-01T01:01:01.001Z");
  39. runTest("0001-01-00T01:01:01.001Z");
  40. runTest("0001-01-32T01:01:01.001Z");
  41. runTest("0001-01-01T25:01:01.001Z");
  42. runTest("0001-01-01T01:01:01.001+25:00");
  43. runTest("0001-01-01T01:60:01.001Z");
  44. runTest("0001-01-01T01:01:01.001+00:60");
  45. runTest("0001-01-01T01:01:60.001Z");
  46. echo("// Time value outside valid range");
  47. echo("");
  48. runTest("-300000-01-01T01:01:01.001Z");
  49. runTest("+300000-01-01T01:01:01.001Z");
  50. // Many other invalid ISO strings are tested in "potential cross-browser compatibility issues" section
  51. writeStats();
  52. echo("////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////");
  53. echo("// Potential cross-browser compatilibity issues");
  54. echo("");
  55. echo("// Leading and trailing whitespace, nulls, or non-whitespace non-nulls");
  56. echo("");
  57. var s = "0001-01-01T01:01:01.001Z";
  58. var spaceNulls = ["", "\0", "\t", "\n", "\v", "\f", "\r", " ", "\u00a0", "\u2028", "\u2029", "\ufeff"];
  59. for (var i = 0; i < spaceNulls.length; ++i) {
  60. if (s !== "") {
  61. runTest(spaceNulls[i] + s);
  62. runTest(s + spaceNulls[i]);
  63. }
  64. runTest(s + spaceNulls[i] + "x");
  65. }
  66. echo("// Less and more digits per field");
  67. echo("");
  68. runTest("001-01-01T01:01:01.001Z");
  69. runTest("00001-01-01T01:01:01.001Z");
  70. runTest("0001-1-01T01:01:01.001Z");
  71. runTest("0001-001-01T01:01:01.001Z");
  72. runTest("0001-01-1T01:01:01.001Z");
  73. runTest("0001-01-001T01:01:01.001Z");
  74. runTest("0001-01-01T1:01:01.001Z");
  75. runTest("0001-01-01T001:01:01.001Z");
  76. runTest("0001-01-01T01:1:01.001Z");
  77. runTest("0001-01-01T01:001:01.001Z");
  78. runTest("0001-01-01T01:01:1.001Z");
  79. runTest("0001-01-01T01:01:001.001Z");
  80. runTest("0001-01-01T01:01:01.01Z");
  81. runTest("0001-01-01T01:01:01.0001Z");
  82. echo("// Date-only forms with UTC offset");
  83. echo("");
  84. runTest("0001Z");
  85. runTest("0001-01Z");
  86. runTest("0001-01-01Z"); // note: this is rejected by the ISO parser as it should be, but it's accepted by the fallback parser
  87. echo("// Optionality of minutes");
  88. echo("");
  89. runTest("0001-01-01T01Z");
  90. runTest("0001-01-01T01:01:01.001+01");
  91. echo("// Time-only forms");
  92. echo("");
  93. runTest("T01:01Z");
  94. runTest("T01:01:01Z");
  95. runTest("T01:01:01.001Z");
  96. echo("// Field before missing optional field ending with separator");
  97. echo("");
  98. runTest("0001-");
  99. runTest("0001-01-");
  100. runTest("0001-T01:01:01.001Z");
  101. runTest("0001-01-T01:01:01.001Z");
  102. runTest("0001-01-01T01:01:Z");
  103. runTest("0001-01-01T01:01:01.Z");
  104. echo("// Optionality and type of sign on years");
  105. echo("");
  106. runTest("+0001-01-01T01:01:01.001Z");
  107. runTest("-0001-01-01T01:01:01.001Z");
  108. runTest("010000-01-01T01:01:01.001Z");
  109. runTest("-000000-01-01T01:01:01.001Z");
  110. echo("// Test support for zones without colons (DEVDIV2: 481975)");
  111. echo("");
  112. runTest("2012-02-22T03:08:26+0000");
  113. writeStats();
  114. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  115. // Test-specific helpers
  116. function runTest(s) {
  117. ++total;
  118. echo(s);
  119. safeCall(function () {
  120. var iso = new Date(s);
  121. var timeValue1 = iso.getTime();
  122. if (isNaN(timeValue1)) {
  123. echo(iso);
  124. } else {
  125. iso = iso.toISOString();
  126. echo(iso);
  127. var timeValue2 = new Date(iso).getTime();
  128. echo(timeValue1 + " " + (timeValue1 === timeValue2 ? "===" : "!==") + " " + timeValue2);
  129. if (iso.indexOf("Invalid", 0) === -1) {
  130. if (timeValue1 === timeValue2)
  131. ++accepted;
  132. else
  133. ++failed;
  134. }
  135. }
  136. });
  137. echo("");
  138. }
  139. function runGenerateTest() {
  140. var s =
  141. generateDateStrings(
  142. yearDigits,
  143. monthDigits,
  144. dayDigits,
  145. hourMinuteDigits,
  146. hourMinuteDigits,
  147. secondDigits,
  148. millisecondDigits);
  149. for (var i = 0; i < s.length; ++i)
  150. runTest(s[i]);
  151. }
  152. var signs, zones;
  153. function initializeGenerateDateStrings() {
  154. signs = ["+", "-"];
  155. zones = ["", "Z"];
  156. var zoneDigitCombinations = ["00", "01", "10"];
  157. for (var i = 0; i < zoneDigitCombinations.length; ++i)
  158. for (var j = 0; j < zoneDigitCombinations.length; ++j)
  159. for (var k = 0; k < signs.length; ++k)
  160. zones.push(signs[k] + zoneDigitCombinations[i] + ":" + zoneDigitCombinations[j]);
  161. }
  162. // Generates date strings in the following format:
  163. // date format: "[+|-]YYYYYY[-MM[-DD]]"
  164. // separator: "T| "
  165. // time format: "HH:mm[:ss[.sss]]"
  166. // time zone: "Z|(+|-)HH:mm"
  167. // - The separator is required only if both the date and time portions are included in the string.
  168. // - Zero-padding is optional
  169. // - Positive sign (+) is optional when the year is nonnegative
  170. // - Negative sign (-) is optional when the year is zero
  171. // - Time zone is optional
  172. //
  173. // The function will return an array of strings to test against, based on the parameters.
  174. function generateDateStrings(
  175. yearDigits, // number of digits to include for the year (0-6), 0 - exclude the year (monthDigits must also be 0)
  176. monthDigits, // number of digits to include for the month (0-2), 0 - exclude the month (dayDigits must also be 0)
  177. dayDigits, // number of digits to include for the day (0-2), 0 - exclude the day
  178. hourDigits, // number of digits to include for the hour (0-2), 0 - exclude the hour (minuteDigits must also be 0)
  179. minuteDigits, // number of digits to include for the minute (0-2), 0 - exclude the minute (hourDigits and secondDigits must also be 0)
  180. secondDigits, // number of digits to include for the second (0-2), 0 - exclude the second (millisecondDigits must also be 0)
  181. millisecondDigits) // number of digits to include for the millisecond (0-3), 0 - exclude the millisecond
  182. {
  183. if (yearDigits === 0 && monthDigits !== 0 ||
  184. monthDigits === 0 && dayDigits !== 0 ||
  185. hourDigits === 0 && minuteDigits !== 0 ||
  186. minuteDigits === 0 && (hourDigits !== 0 || secondDigits !== 0) ||
  187. secondDigits === 0 && millisecondDigits !== 0 ||
  188. yearDigits === 0 && (hourDigits === 0 || minuteDigits === 0))
  189. return [];
  190. var s = [""];
  191. if (yearDigits !== 0) {
  192. appendDigits(s, yearDigits, true);
  193. if (monthDigits !== 0) {
  194. append(s, ["-"]);
  195. appendDigits(s, monthDigits, false);
  196. if (dayDigits !== 0) {
  197. append(s, ["-"]);
  198. appendDigits(s, dayDigits, false);
  199. }
  200. }
  201. }
  202. if (hourDigits !== 0 && minuteDigits !== 0) {
  203. append(s, ["T"]);
  204. appendDigits(s, hourDigits, true);
  205. append(s, [":"]);
  206. appendDigits(s, minuteDigits, true);
  207. if (secondDigits !== 0) {
  208. append(s, [":"]);
  209. appendDigits(s, secondDigits, true);
  210. if (millisecondDigits !== 0) {
  211. append(s, ["."]);
  212. appendDigits(s, millisecondDigits, true);
  213. }
  214. }
  215. }
  216. if (yearDigits !== 0 && hourDigits !== 0 && minuteDigits !== 0)
  217. s = applyToEach(s, zones, function (str, zone) { return str + zone; });
  218. if(yearDigits === 6) {
  219. s =
  220. applyToEach(
  221. s,
  222. signs,
  223. function (str, sign) {
  224. if(sign === "-" && str.length >= 6 && str.substring(0, 6) === "000000")
  225. return undefined; // "-000000" is not allowed
  226. return sign + str;
  227. });
  228. }
  229. return s;
  230. }
  231. // Appends interesting combinations of n digits to the string array
  232. function appendDigits(a, n, includeZero) {
  233. var d = [];
  234. switch (n) {
  235. case 0:
  236. break;
  237. case 1:
  238. if (includeZero)
  239. d.push("0");
  240. d.push("1");
  241. append(a, d);
  242. break;
  243. case 3:
  244. case 6:
  245. if (n === 3)
  246. d.push("010");
  247. else
  248. d.push("010010");
  249. default:
  250. var z = zeroes(n - 1);
  251. if (includeZero)
  252. d.push(z + "0");
  253. d.push(z + "1");
  254. d.push("1" + z);
  255. append(a, d);
  256. break;
  257. }
  258. }
  259. // Returns a string of n zeroes
  260. function zeroes(n) {
  261. var s = "";
  262. while (n-- > 0)
  263. s += "0";
  264. return s;
  265. }
  266. // Appends patterns to the string array. The array is extended to acommodate the number of patterns, and the patterns are
  267. // repeated to acommodate the length of the array.
  268. function append(a, p) {
  269. extend(a, p.length);
  270. for (var i = 0; i < a.length; ++i)
  271. a[i] += p[i % p.length];
  272. }
  273. // Applies the function 'f' to each combination of elements in 'a' and 'p'. 'f' will receive the element of 'a' on which it
  274. // should apply the pattern from 'p' and it should return the modified string. The string returned by 'f' will be pushed onto a
  275. // new array, which will be returned.
  276. function applyToEach(a, p, f) {
  277. var a2 = [];
  278. for(var i = 0; i < a.length; ++i) {
  279. for(var j = 0; j < p.length; ++j) {
  280. var transformed = f(a[i], p[j]);
  281. if(transformed !== undefined)
  282. a2.push(transformed);
  283. }
  284. }
  285. return a2;
  286. }
  287. // Extends an array to have length n, by copying the last element as necessary
  288. function extend(a, n) {
  289. var originalLength = a.length;
  290. for (var i = originalLength; i < n; ++i)
  291. a.push(a[originalLength - 1]);
  292. }
  293. function writeStats() {
  294. echo("Total: " + total);
  295. echo("Accepted: " + accepted);
  296. echo("Rejected: " + (total - accepted - failed));
  297. echo("Failed: " + failed);
  298. echo("");
  299. failed = accepted = total = 0;
  300. }
  301. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  302. // General helpers
  303. function toString(o, quoteStrings) {
  304. switch (o) {
  305. case null:
  306. case undefined:
  307. return "" + o;
  308. }
  309. switch (typeof o) {
  310. case "boolean":
  311. case "number":
  312. return "" + o;
  313. case "string":
  314. {
  315. var hex = "0123456789abcdef";
  316. var s = "";
  317. for (var i = 0; i < o.length; ++i) {
  318. var c = o.charCodeAt(i);
  319. if (c === 0)
  320. s += "\\0";
  321. else if (c >= 0x20 && c < 0x7f)
  322. s += quoteStrings && o.charAt(i) === "\"" ? "\\\"" : o.charAt(i);
  323. else if (c <= 0xff)
  324. s += "\\x" + hex.charAt((c >> 4) & 0xf) + hex.charAt(c & 0xf);
  325. else
  326. s += "\\u" + hex.charAt((c >> 12) & 0xf) + hex.charAt((c >> 8) & 0xf) + hex.charAt((c >> 4) & 0xf) + hex.charAt(c & 0xf);
  327. }
  328. if (quoteStrings)
  329. s = "\"" + s + "\"";
  330. return s;
  331. }
  332. case "object":
  333. case "function":
  334. break;
  335. default:
  336. return "<unknown type '" + typeof o + "'>";
  337. }
  338. if (o instanceof Array) {
  339. var s = "[";
  340. for (var i = 0; i < o.length; ++i) {
  341. if (i)
  342. s += ", ";
  343. s += this.toString(o[i], true);
  344. }
  345. return s + "]";
  346. }
  347. if (o instanceof Error)
  348. return o.name + ": " + o.message;
  349. if (o instanceof RegExp)
  350. return o.toString() + (o.lastIndex === 0 ? "" : " (lastIndex: " + o.lastIndex + ")");
  351. return "" + o;
  352. }
  353. function echo(o) {
  354. var s = this.toString(o);
  355. try {
  356. document.write(s + "<br/>");
  357. } catch (ex) {
  358. try {
  359. WScript.Echo(s);
  360. } catch (ex2) {
  361. print(s);
  362. }
  363. }
  364. }
  365. function safeCall(f) {
  366. var args = [];
  367. for (var a = 1; a < arguments.length; ++a)
  368. args.push(arguments[a]);
  369. try {
  370. return f.apply(this, args);
  371. } catch (ex) {
  372. echo(ex);
  373. }
  374. }