DateParse2.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. testParseDate("Thu, 23 Sep -");
  45. testParseDate("Thu, 23 Sep-");
  46. testParseDate("Tue Feb 02 -");
  47. testParseDate("Tue Feb 02-");
  48. function CUT_NAME(str) {
  49. return str.replace("(PST)", "(Pacific Standard Time)")
  50. .replace("(PDT)", "(Pacific Daylight Time)");
  51. }
  52. function testDate(date) {
  53. testParseDate(date.toString());
  54. }
  55. function testParseDate(dateStr) {
  56. myPrint("Date string:\t\t" + dateStr);
  57. var d = Date.parse(dateStr);
  58. testParseDateCore(d);
  59. }
  60. // This is to avoid printing non-printable chars
  61. function testParseDate2(dateStr, appendThis) {
  62. myPrint("Date string:\t\t" + dateStr);
  63. var d = Date.parse(dateStr + appendThis);
  64. testParseDateCore(d);
  65. }
  66. function testParseDateCore(d) {
  67. myPrint("\t raw:\t\t" + d);
  68. d = new Date(d);
  69. myPrint("\t toString:\t" + d.toString());
  70. myPrint("\t toUTCString:\t" + d.toUTCString());
  71. myPrint("\t toGMTString:\t" + d.toGMTString());
  72. if (isNaN(d) === false) {
  73. myPrint("\t toISOString:\t" + d.toISOString());
  74. myPrint("\t\t\t" + d.getDate() + " " + d.getTime() + " " + d.getTimezoneOffset());
  75. myPrint("\t\t\t" + d.getFullYear() + "/" + d.getMonth() + "/" + d.getDay());
  76. myPrint("\t\t\t" + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds() + "." + d.getMilliseconds());
  77. }
  78. myPrint("");
  79. }
  80. function myPrint(str) {
  81. if (WScript.Echo !== undefined) {
  82. WScript.Echo(CUT_NAME(str));
  83. }
  84. else {
  85. throw "no print!";
  86. }
  87. }