toStringAndToUTCStringYearPadding.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. // For dates whose years are less than 1000, Date.toString() and Date.toUTCString() should pad the years
  6. // to 4 digits.
  7. // See https://github.com/Microsoft/ChakraCore/pull/4067
  8. /// <reference path="../UnitTestFramework/UnitTestFramework.js" />
  9. if (this.WScript && this.WScript.LoadScriptFile) { // Check for running in ch
  10. this.WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  11. }
  12. function testDate(DateObj, toStringExpected, toUTCStringExpected) {
  13. assert.areEqual(toStringExpected, DateObj.toString(), "Date.toString() returns invalid value.");
  14. assert.areEqual(toUTCStringExpected, DateObj.toUTCString(), "Date.toUTCString() returns invalid value.");
  15. }
  16. let tests = [{
  17. name: "test if Date.toString() and Date.toUTCString() pad positive years to four digits",
  18. body: function () {
  19. testDate(new Date("0001-10-13T05:16:33Z"), "Fri Oct 12 0001 22:16:33 GMT-0700 (Pacific Daylight Time)",
  20. "Sat, 13 Oct 0001 05:16:33 GMT");
  21. testDate(new Date("0011-10-13T05:16:33Z"), "Wed Oct 12 0011 22:16:33 GMT-0700 (Pacific Daylight Time)",
  22. "Thu, 13 Oct 0011 05:16:33 GMT");
  23. testDate(new Date("0111-10-13T05:16:33Z"), "Mon Oct 12 0111 22:16:33 GMT-0700 (Pacific Daylight Time)",
  24. "Tue, 13 Oct 0111 05:16:33 GMT");
  25. testDate(new Date("1111-10-13T05:16:33Z"), "Thu Oct 12 1111 22:16:33 GMT-0700 (Pacific Daylight Time)",
  26. "Fri, 13 Oct 1111 05:16:33 GMT");
  27. }
  28. },
  29. {
  30. name: "test if Date.toString() and Date.toUTCString() pad negative years to four digits",
  31. body: function () {
  32. testDate(new Date("-000001-10-13T05:16:33Z"), "Tue Oct 12 -0001 22:16:33 GMT-0700 (Pacific Daylight Time)",
  33. "Wed, 13 Oct -0001 05:16:33 GMT");
  34. testDate(new Date("-000011-10-13T05:16:33Z"), "Thu Oct 12 -0011 22:16:33 GMT-0700 (Pacific Daylight Time)",
  35. "Fri, 13 Oct -0011 05:16:33 GMT");
  36. testDate(new Date("-000111-10-13T05:16:33Z"), "Sat Oct 12 -0111 22:16:33 GMT-0700 (Pacific Daylight Time)",
  37. "Sun, 13 Oct -0111 05:16:33 GMT");
  38. testDate(new Date("-001111-10-13T05:16:33Z"), "Wed Oct 12 -1111 22:16:33 GMT-0700 (Pacific Daylight Time)",
  39. "Thu, 13 Oct -1111 05:16:33 GMT");
  40. }
  41. }];
  42. testRunner.run(tests, { verbose: WScript.Arguments[0] != "summary" });