DateParse2.js 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. myPrint("Phase 1 - known dates");
  6. myPrint("A --");
  7. testDate(new Date(-2012, 1, 2, 1, 2, 3));
  8. testParseDate(new Date(-2012, 1, 2, 1, 2, 3).toString());
  9. testParseDate(new Date(-2012, 1, 2, 1, 2, 3).toUTCString());
  10. testParseDate(new Date(-2012, 1, 2, 1, 2, 3).toISOString());
  11. myPrint("B --");
  12. testDate(new Date(2012, 1, 2, 1, 2, 3));
  13. testParseDate(new Date(2012, 1, 2, 1, 2, 3).toString());
  14. testParseDate(new Date(2012, 1, 2, 1, 2, 3).toUTCString());
  15. testParseDate(new Date(2012, 1, 2, 1, 2, 3).toISOString());
  16. myPrint("C --");
  17. testDate(new Date(99999, 1, 2, 1, 2, 3));
  18. testParseDate(new Date(99999, 1, 2, 1, 2, 3).toString());
  19. testParseDate(new Date(99999, 1, 2, 1, 2, 3).toUTCString());
  20. testParseDate(new Date(99999, 1, 2, 1, 2, 3).toISOString());
  21. myPrint("D --");
  22. testDate(new Date(-99999, 1, 2, 1, 2, 3));
  23. testParseDate(new Date(-99999, 1, 2, 1, 2, 3).toString());
  24. testParseDate(new Date(-99999, 1, 2, 1, 2, 3).toUTCString());
  25. testParseDate(new Date(-99999, 1, 2, 1, 2, 3).toISOString());
  26. myPrint("E --");
  27. testDate(new Date(-12, 1, 2, 1, 2, 3));
  28. testParseDate(new Date(-12, 1, 2, 1, 2, 3).toString());
  29. testParseDate(new Date(-12, 1, 2, 1, 2, 3).toUTCString());
  30. testParseDate(new Date(-12, 1, 2, 1, 2, 3).toISOString());
  31. myPrint("F --");
  32. testDate(new Date(12, 1, 2, 1, 2, 3));
  33. testParseDate(new Date(12, 1, 2, 1, 2, 3).toString());
  34. testParseDate(new Date(12, 1, 2, 1, 2, 3).toUTCString());
  35. testParseDate(new Date(12, 1, 2, 1, 2, 3).toISOString());
  36. myPrint("Phase 2 - parsing sample date strings");
  37. testParseDate("Tue Feb 02 2012 01:02:03 GMT-0800 (Pacific Standard Time)");
  38. testParseDate("Tue Feb 02 2012 01:02:03 GMT+0800 (prisec)");
  39. testParseDate2("Tue Feb 02 2012 01:02:03 GMT+0000", " (ハヌ)");
  40. testParseDate("Tue Feb 02 2012 01:02:03 GMT-0000");
  41. testParseDate("Tue Feb 02 2012 01:02:03 GMT+0430 (prisec@)");
  42. testParseDate("Tue Feb 2 01:02:03 PST 2013 B.C.");
  43. testParseDate("Thu Feb 2 01:02:03 PST 2012");
  44. function CUT_NAME(str) {
  45. return str.replace("(PST)", "(Pacific Standard Time)")
  46. .replace("(PDT)", "(Pacific Daylight Time)");
  47. }
  48. function testDate(date) {
  49. testParseDate(date.toString());
  50. }
  51. function testParseDate(dateStr) {
  52. myPrint("Date string:\t\t" + dateStr);
  53. var d = Date.parse(dateStr);
  54. testParseDateCore(d);
  55. }
  56. // This is to avoid printing non-printable chars
  57. function testParseDate2(dateStr, appendThis) {
  58. myPrint("Date string:\t\t" + dateStr);
  59. var d = Date.parse(dateStr + appendThis);
  60. testParseDateCore(d);
  61. }
  62. function testParseDateCore(d) {
  63. myPrint("\t raw:\t\t" + d);
  64. d = new Date(d);
  65. myPrint("\t toString:\t" + d.toString());
  66. myPrint("\t toUTCString:\t" + d.toUTCString());
  67. myPrint("\t toGMTString:\t" + d.toGMTString());
  68. if (isNaN(d) === false) {
  69. myPrint("\t toISOString:\t" + d.toISOString());
  70. myPrint("\t\t\t" + d.getDate() + " " + d.getTime() + " " + d.getTimezoneOffset());
  71. myPrint("\t\t\t" + d.getFullYear() + "/" + d.getMonth() + "/" + d.getDay());
  72. myPrint("\t\t\t" + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds() + "." + d.getMilliseconds());
  73. }
  74. myPrint("");
  75. }
  76. function myPrint(str) {
  77. if (WScript.Echo !== undefined) {
  78. WScript.Echo(CUT_NAME(str));
  79. }
  80. else {
  81. throw "no print!";
  82. }
  83. }