getvardate.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // NOTE: because vdates are timezone specific, this test will might only work in PST
  6. // not sure about DST. If you see a failure, that would be my first suspicion
  7. var date = new Date(0);
  8. var vdate = date.getVarDate();
  9. // Test string concat both ways:
  10. writeLine("Attempt string concat (string + vdate) and echo.");
  11. var appendMe = "test concat: " + vdate;
  12. writeLine(appendMe);
  13. writeLine("Attempt string concat (vdate + string) and echo.");
  14. var appendMe = vdate + ": test concat2";
  15. writeLine(appendMe);
  16. // Test typeof
  17. writeLine(typeof(date));
  18. writeLine(typeof(vdate));
  19. writeLine(typeof(new Object(vdate)));
  20. writeLine(typeof(Object(vdate)));
  21. // Test .toString (should fail)
  22. try {
  23. var myVar = vdate.toString();
  24. writeLine("FAIL: we should have errored on .toString();");
  25. } catch (e)
  26. {
  27. writeLine("SUCCESS: vdate.toString() failed with error #" + e.number);
  28. }
  29. // Test assigning to a member
  30. try {
  31. vdate.aMember = 3;
  32. writeLine("FAIL: we should have errored on vdate.aMember = 3;");
  33. } catch (e)
  34. {
  35. writeLine("SUCCESS: vdate.aMember = 3 failed with error #" + e.number);
  36. }
  37. // Test assigning to a member with []
  38. try {
  39. vdate["aMember"] = 3;
  40. writeLine("FAIL: we should have errored on vdate[\"aMember\"] = 3;");
  41. } catch (e)
  42. {
  43. writeLine("SUCCESS: vdate[\"aMember\"] = 3 failed with error #" + e.number);
  44. }
  45. // Test accessing a member
  46. try {
  47. var shouldNotWork = vdate.aMember;
  48. } catch (e)
  49. {
  50. writeLine("SUCCESS: var shouldNotWork = date.aMember failed with error #" + e.number);
  51. }
  52. // Try some more unusual or invalid uses of VarDate
  53. writeLine("");
  54. writeLine("Unusual cases:");
  55. vdate = new Date(1234567890123).getVarDate();
  56. safeCall(function() { writeLine(vdate ? true : false); });
  57. safeCall(function() { writeLine([1, 2].indexOf(2, vdate)); }); // valid only in version 3
  58. safeCall(function() { writeLine(parseInt("1", vdate)); });
  59. // normalize Old GetNumberFormatEx exception
  60. safeCall(function() {
  61. var result = [1, vdate, 2].toLocaleString();
  62. if (result == "1, [object Object], 2") {
  63. result = "1.00, [object Object], 2.00";
  64. }
  65. writeLine(result);
  66. });
  67. // Try some random dates to make sure we match the old engine
  68. writeLine("");
  69. writeLine("Pseudorandom cases:");
  70. for (var i = 0; i < 1000; i++)
  71. {
  72. var testDate = new Date(i*10373);
  73. var testVDate = testDate.getVarDate();
  74. writeLine("VT_DATE: '" + testVDate + "'");
  75. }
  76. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  77. // Helpers
  78. function writeLine(str)
  79. {
  80. if (typeof str === "string") {
  81. str = str.replace(/\(PDT\)/g, "(Pacific Daylight Time)")
  82. .replace(/\(PST\)/g, "(Pacific Standard Time)");
  83. }
  84. WScript.Echo("" + str);
  85. }
  86. function safeCall(func)
  87. {
  88. try
  89. {
  90. return func();
  91. }
  92. catch (ex)
  93. {
  94. writeLine(ex.name + " (" + ex.number + "): " + ex.message);
  95. }
  96. }