|
| 1 | +// Copyright 2022 the V8 project authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// Test the new Date( date.toLocaleString("en-US")) is not invalid. |
| 6 | +// This is not guaranteed by the standard but many code use that to set the |
| 7 | +// timezone as suggested in |
| 8 | +// https://stackoverflow.com/questions/15141762/how-to-initialize-a-javascript-date-to-a-particular-time-zone |
| 9 | + |
| 10 | +let d = new Date(); |
| 11 | + |
| 12 | +// https://tc39.es/ecma262/#sec-todatestring |
| 13 | +// 21.4.4.41.4 ToDateString ( tv ) |
| 14 | +// 1. If tv is NaN, return "Invalid Date". |
| 15 | +let invalid = "Invalid Date"; |
| 16 | +let largestDiff = 25*60*60*1000; |
| 17 | + |
| 18 | +let garbage = new Date("garbage"); |
| 19 | +assertTrue(invalid == garbage); |
| 20 | +assertEquals(NaN, garbage.getTime()); |
| 21 | + |
| 22 | +let d1 = new Date(d.toLocaleString("en-US")); |
| 23 | +assertTrue(d1 != invalid); |
| 24 | +assertTrue(d1.getTime() != NaN); |
| 25 | +// The milliseconds are different between d1 and d. |
| 26 | +assertTrue(Math.abs(d1-d) < 1000); |
| 27 | + |
| 28 | +// Force a version of date string which have U+202f before AM |
| 29 | +let nnbsp_am = new Date("11/16/2022, 9:04:55\u202fAM"); |
| 30 | +assertTrue(nnbsp_am != invalid); |
| 31 | +assertTrue(nnbsp_am.getTime() != NaN); |
| 32 | +// Force a version of date string which have U+202f before PM |
| 33 | +let nnbsp_pm = new Date("11/16/2022, 9:04:55\u202fPM"); |
| 34 | +assertTrue(nnbsp_pm != invalid); |
| 35 | +assertTrue(nnbsp_pm.getTime() != NaN); |
| 36 | + |
| 37 | +let d2 = new Date(d.toLocaleString("en-US", {timeZone: "Asia/Taipei"})); |
| 38 | +assertTrue(d2 != invalid); |
| 39 | +assertTrue(d2.getTime() != NaN); |
| 40 | +// The differences should be within 25 hours. |
| 41 | +assertTrue(Math.abs(d2-d) < largestDiff); |
| 42 | + |
| 43 | +let d3 = new Date(d.toLocaleString("en-US", {timeZone: "Africa/Lusaka"})); |
| 44 | +assertTrue(d3 != invalid); |
| 45 | +assertTrue(d3.getTime() != NaN); |
| 46 | +// The differences should be within 25 hours. |
| 47 | +assertTrue(Math.abs(d3-d) < largestDiff); |
0 commit comments