Conversions.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. // This test file verifies conversion from local to UTC, and UTC to local, including DST adjustments, for old and new years.
  6. // NOTE: As other date test files, these test cases use times that are relevant to the Pacific time zone (PST/PDT). Since the
  7. // test is zone-specific, in other zones, the test will most likely fail. The result is only valid in the Pacific time zone.
  8. // NOTE: If DST rules change again in the PST time zone, these test cases will need to be changed to use different boundary date
  9. // values as appropriate for the new DST rules compared with the old DST rules.
  10. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  11. writeLine("--- Standard Time (ST) conversions ---");
  12. writeLine("");
  13. writeLine("January 31, 2006 - UTC to local - ST");
  14. var t = new Date(Date.UTC(2006, 0, 31, 18, 0));
  15. writeLine(t.toUTCString());
  16. writeLine(t.toLocaleString());
  17. writeLine("");
  18. writeLine("January 31, 2006 - local to UTC - ST");
  19. var t = new Date(Date.parse("January 31, 2006 11:00 AM"));
  20. writeLine(t.toLocaleString());
  21. writeLine(t.toUTCString());
  22. writeLine("");
  23. writeLine("January 31, 2007 - UTC to local - ST");
  24. var t = new Date(Date.UTC(2007, 0, 31, 18, 0));
  25. writeLine(t.toUTCString());
  26. writeLine(t.toLocaleString());
  27. writeLine("");
  28. writeLine("January 31, 2007 - local to UTC - ST");
  29. var t = new Date(Date.parse("January 31, 2007 11:00 AM"));
  30. writeLine(t.toLocaleString());
  31. writeLine(t.toUTCString());
  32. writeLine("");
  33. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  34. writeLine("--- Daylight Time (DT) conversions ---");
  35. writeLine("");
  36. writeLine("July 15, 2006 - UTC to local - DT");
  37. var t = new Date(Date.UTC(2006, 6, 31, 18, 0));
  38. writeLine(t.toUTCString());
  39. writeLine(t.toLocaleString());
  40. writeLine("");
  41. writeLine("July 15, 2006 - local to UTC - DT");
  42. var t = new Date(Date.parse("July 15, 2006 11:00 AM"));
  43. writeLine(t.toLocaleString());
  44. writeLine(t.toUTCString());
  45. writeLine("");
  46. writeLine("July 15, 2007 - UTC to local - DT");
  47. var t = new Date(Date.UTC(2007, 6, 31, 18, 0));
  48. writeLine(t.toUTCString());
  49. writeLine(t.toLocaleString());
  50. writeLine("");
  51. writeLine("July 15, 2007 - local to UTC - DT");
  52. var t = new Date(Date.parse("July 15, 2007 11:00 AM"));
  53. writeLine(t.toLocaleString());
  54. writeLine(t.toUTCString());
  55. writeLine("");
  56. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  57. writeLine("--- Boundary conversions between old and new rules ---");
  58. writeLine("");
  59. // In 2007, the DST rules in the Pacific time zone changed. Instead of transitioning into DST in April, the transition
  60. // happens in March, and instead of transitioning into Standard Time in October, the transition happens in November.
  61. // So, we test using March 31, 2006/2007 and October 31, 2006/2007. These dates fall in between the transition times in 2006
  62. // and 2007. The expected behavior is that we always use the current year's DST rules when applying the DST adjustment. So
  63. // even for the dates in 2006, we should use the current rules for conversion.
  64. writeLine("March 31, 2006 - UTC to local - ST in 2006 rules");
  65. var t = new Date(Date.UTC(2006, 2, 31, 18, 0));
  66. writeLine(t.toUTCString());
  67. writeLine(t.toLocaleString());
  68. writeLine("");
  69. writeLine("March 31, 2006 - local to UTC - ST in 2006 rules");
  70. var t = new Date(Date.parse("March 31, 2006 11:00 AM"));
  71. writeLine(t.toLocaleString());
  72. writeLine(t.toUTCString());
  73. writeLine("");
  74. writeLine("March 31, 2007 - UTC to local - DT in 2007 rules");
  75. var t = new Date(Date.UTC(2007, 2, 31, 18, 0));
  76. writeLine(t.toUTCString());
  77. writeLine(t.toLocaleString());
  78. writeLine("");
  79. writeLine("March 31, 2007 - local to UTC - DT in 2007 rules");
  80. var t = new Date(Date.parse("March 31, 2007 11:00 AM"));
  81. writeLine(t.toLocaleString());
  82. writeLine(t.toUTCString());
  83. writeLine("");
  84. writeLine("October 31, 2006 - UTC to local - ST in 2006 rules");
  85. var t = new Date(Date.UTC(2006, 9, 31, 18, 0));
  86. writeLine(t.toUTCString());
  87. writeLine(t.toLocaleString());
  88. writeLine("");
  89. writeLine("October 31, 2006 - local to UTC - ST in 2006 rules");
  90. var t = new Date(Date.parse("October 31, 2006 11:00 AM"));
  91. writeLine(t.toLocaleString());
  92. writeLine(t.toUTCString());
  93. writeLine("");
  94. writeLine("October 31, 2007 - UTC to local - DT in 2007 rules");
  95. var t = new Date(Date.UTC(2007, 9, 31, 18, 0));
  96. writeLine(t.toUTCString());
  97. writeLine(t.toLocaleString());
  98. writeLine("");
  99. writeLine("October 31, 2007 - local to UTC - DT in 2007 rules");
  100. var t = new Date(Date.parse("October 31, 2007 11:00 AM"));
  101. writeLine(t.toLocaleString());
  102. writeLine(t.toUTCString());
  103. writeLine("");
  104. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  105. writeLine("--- Boundary conversions when transitioning into DT ---");
  106. writeLine("");
  107. writeLine("March 11, 2007 2:00 AM transition - UTC to local");
  108. var t = new Date(Date.UTC(2007, 2, 11, 9, 30));
  109. writeLine(t.toUTCString());
  110. writeLine(t.toLocaleString());
  111. var t = new Date(Date.UTC(2007, 2, 11, 10, 30));
  112. writeLine(t.toUTCString());
  113. writeLine(t.toLocaleString());
  114. var t = new Date(Date.UTC(2007, 2, 11, 11, 30));
  115. writeLine(t.toUTCString());
  116. writeLine(t.toLocaleString());
  117. writeLine("");
  118. writeLine("March 11, 2007 2:00 AM transition - local to UTC");
  119. var t = new Date(Date.parse("March 11, 2007 1:30 AM"));
  120. writeLine(t.toLocaleString());
  121. writeLine(t.toUTCString());
  122. var t = new Date(Date.parse("March 11, 2007 2:30 AM"));
  123. writeLine(t.toLocaleString());
  124. writeLine(t.toUTCString());
  125. var t = new Date(Date.parse("March 11, 2007 3:30 AM"));
  126. writeLine(t.toLocaleString());
  127. writeLine(t.toUTCString());
  128. writeLine("");
  129. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  130. writeLine("--- Boundary conversions when transitioning into ST ---");
  131. writeLine("");
  132. writeLine("November 4, 2007 2:00 AM transition - UTC to local");
  133. var t = new Date(Date.UTC(2007, 10, 4, 8, 30));
  134. writeLine(t.toUTCString());
  135. writeLine(t.toLocaleString());
  136. var t = new Date(Date.UTC(2007, 10, 4, 9, 30));
  137. writeLine(t.toUTCString());
  138. writeLine(t.toLocaleString());
  139. var t = new Date(Date.UTC(2007, 10, 4, 10, 30));
  140. writeLine(t.toUTCString());
  141. writeLine(t.toLocaleString());
  142. writeLine("");
  143. writeLine("November 4, 2007 2:00 AM transition - local to UTC");
  144. var t = new Date(Date.parse("November 4, 2007 12:30 AM"));
  145. writeLine(t.toLocaleString());
  146. writeLine(t.toUTCString());
  147. var t = new Date(Date.parse("November 4, 2007 1:30 AM"));
  148. writeLine(t.toLocaleString());
  149. writeLine(t.toUTCString());
  150. var t = new Date(Date.parse("November 4, 2007 2:30 AM"));
  151. writeLine(t.toLocaleString());
  152. writeLine(t.toUTCString());
  153. writeLine("");
  154. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  155. // Helpers
  156. function writeLine(str)
  157. {
  158. WScript.Echo("" + str);
  159. }