DateParse.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. // 1. Verifying parsing with date string in GMT format.
  6. var datestring = "Sat, 12 Aug 1995 13:30:00GMT";
  7. WScript.Echo(Date.parse(datestring));
  8. // 2. Verifying parse date with timezone given in paranthesis.
  9. // This parses the date to local timezone and not the timezone given in paranthesis.
  10. datestring = "Sat, 12 Aug 1995 13:30:00 ( GMT )";
  11. // Covers the code for space and ( )s int the parser.
  12. WScript.Echo(Date.parse(datestring));
  13. // 3. Verifying parse date with invalid timezone.
  14. // This should cover the error condition.
  15. datestring = "Sat, 12 Aug 1995 13:30:00NX-01";
  16. var actualResult = Date.parse(datestring);
  17. WScript.Echo(actualResult);
  18. // 4. Verifying parse date with timezone.
  19. datestring = "Sat, 12 Aug 1995 13:00:00 z";
  20. WScript.Echo(Date.parse(datestring));
  21. // 5. Verifying conversion behaviors
  22. var toStrings;
  23. var valueOfs;
  24. var toStringCalled;
  25. var valueOfCalled;
  26. toStrings =
  27. [
  28. {},
  29. function ()
  30. {
  31. toStringCalled = true;
  32. return {};
  33. },
  34. function ()
  35. {
  36. toStringCalled = true;
  37. return undefined;
  38. },
  39. function ()
  40. {
  41. toStringCalled = true;
  42. return "1/1/1970 12:00 am";
  43. }
  44. ];
  45. valueOfs =
  46. [
  47. {},
  48. function ()
  49. {
  50. valueOfCalled = true;
  51. return {};
  52. },
  53. function ()
  54. {
  55. valueOfCalled = true;
  56. return undefined;
  57. },
  58. function ()
  59. {
  60. valueOfCalled = true;
  61. return "1/1/1970 1:00 am";
  62. },
  63. function ()
  64. {
  65. valueOfCalled = true;
  66. return "84";
  67. }
  68. ];
  69. for (var ts in toStrings)
  70. {
  71. for (var vo in valueOfs)
  72. {
  73. toStringCalled = false;
  74. valueOfCalled = false;
  75. var obj = { toString: toStrings[ts], valueOf: valueOfs[vo] };
  76. try
  77. {
  78. WScript.Echo(Date.parse(obj));
  79. }
  80. catch (ex)
  81. {
  82. WScript.Echo("Got error:");
  83. WScript.Echo(" name: " + ex.name);
  84. WScript.Echo(" message: " + ex.message);
  85. }
  86. WScript.Echo("toString called: " + (toStringCalled ? "yes" : "no"));
  87. WScript.Echo("valueOf called: " + (valueOfCalled ? "yes" : "no"));
  88. }
  89. }
  90. // Verifying different precisions for the milliseconds
  91. // No milliseconds
  92. WScript.Echo(Date.parse("2011-11-08T19:48:43"));
  93. // Missing digits after .
  94. WScript.Echo(Date.parse("2011-11-08T19:48:43."));
  95. // milliseconds, 1 digit only, treat it as hundreds
  96. WScript.Echo(Date.parse("2011-11-08T19:48:43.1"));
  97. WScript.Echo(Date.parse("2011-11-08T19:48:43.1a"));
  98. WScript.Echo(Date.parse("2011-11-08T19:48:43.0"));
  99. WScript.Echo(Date.parse("2011-11-08T19:48:43.0a"));
  100. // milliseconds, 2 digit only, treat it as tens
  101. WScript.Echo(Date.parse("2011-11-08T19:48:43.12"));
  102. WScript.Echo(Date.parse("2011-11-08T19:48:43.12a"));
  103. WScript.Echo(Date.parse("2011-11-08T19:48:43.01"));
  104. WScript.Echo(Date.parse("2011-11-08T19:48:43.01a"));
  105. WScript.Echo(Date.parse("2011-11-08T19:48:43.00"));
  106. WScript.Echo(Date.parse("2011-11-08T19:48:43.00a"));
  107. // milliseconds, canonical 3 digit, per EcmaScript spec
  108. WScript.Echo(Date.parse("2011-11-08T19:48:43.123"));
  109. WScript.Echo(Date.parse("2011-11-08T19:48:43.123a"));
  110. WScript.Echo(Date.parse("2011-11-08T19:48:43.001"));
  111. WScript.Echo(Date.parse("2011-11-08T19:48:43.001a"));
  112. WScript.Echo(Date.parse("2011-11-08T19:48:43.000"));
  113. WScript.Echo(Date.parse("2011-11-08T19:48:43.000a"));
  114. // milliseconds, with more than 3 digits, ignore all digits after the third
  115. WScript.Echo(Date.parse("2011-11-08T19:48:43.1234"));
  116. WScript.Echo(Date.parse("2011-11-08T19:48:43.1234a"));
  117. WScript.Echo(Date.parse("2011-11-08T19:48:43.0011"));
  118. WScript.Echo(Date.parse("2011-11-08T19:48:43.0011a"));
  119. WScript.Echo(Date.parse("2011-11-08T19:48:43.0001"));
  120. WScript.Echo(Date.parse("2011-11-08T19:48:43.0001a"));
  121. WScript.Echo(Date.parse("2011-11-08T19:48:43.0000"));
  122. WScript.Echo(Date.parse("2011-11-08T19:48:43.0000a"));
  123. WScript.Echo(Date.parse("2011-11-08T19:48:43.12345"));
  124. WScript.Echo(Date.parse("2011-11-08T19:48:43.12345a"))
  125. WScript.Echo(Date.parse("2011-11-08T19:48:43.00111"));;
  126. WScript.Echo(Date.parse("2011-11-08T19:48:43.00111a"));;
  127. WScript.Echo(Date.parse("2011-11-08T19:48:43.00001"));
  128. WScript.Echo(Date.parse("2011-11-08T19:48:43.00001a"));
  129. WScript.Echo(Date.parse("2011-11-08T19:48:43.00000"));
  130. WScript.Echo(Date.parse("2011-11-08T19:48:43.00000a"));
  131. WScript.Echo(Date.parse("2011-11-08T19:48:43.00000000000001"));
  132. WScript.Echo(Date.parse("2011-11-08T19:48:43.00000000000001a"));
  133. // Verifying non-standard delimiters
  134. // Some delimiters are allowed to be compatible with other browsers
  135. WScript.Echo(Date.parse("2011-11-08/19:48:43"));
  136. WScript.Echo(Date.parse("2011-11-08:19:48:43"));
  137. WScript.Echo(Date.parse("2011-11-08 19:48:43"));
  138. WScript.Echo(Date.parse("2011-11-08/: 19:48:43"));
  139. WScript.Echo(Date.parse("2011/11/08/19:48:43"));
  140. WScript.Echo(Date.parse("2011/11/08:19:48:43"));
  141. WScript.Echo(Date.parse("2011/11/08 19:48:43"));
  142. WScript.Echo(Date.parse("2011/11/08/: 19:48:43"));
  143. // Mixed delimiters in the date are allowed
  144. WScript.Echo(Date.parse("2011-11/08 19:48:43"));
  145. WScript.Echo(Date.parse("2011/11-08 19:48:43"));
  146. // Multiple 'T's aren't allowed
  147. WScript.Echo(Date.parse("2011-11-08TT19:48:43"));