15.9.5.5.js (4048B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 7 /** 8 File Name: 15.9.5.5.js 9 ECMA Section: 15.9.5.5 Date.prototype.toLocaleString() 10 Description: 11 This function returns a string value. The contents of the string are 12 implementation dependent, but are intended to represent the "date" 13 portion of the Date in the current time zone in a convenient, 14 human-readable form. We can't test the content of the string, 15 but can verify that the string is parsable by Date.parse 16 17 The toLocaleString function is not generic; it generates a runtime error 18 if its 'this' value is not a Date object. Therefore it cannot be transferred 19 to other kinds of objects for use as a method. 20 21 Note: This test isn't supposed to work with a non-English locale per spec. 22 23 Author: pschwartau@netscape.com 24 Date: 14 november 2000 25 */ 26 27 var SECTION = "15.9.5.5"; 28 var TITLE = "Date.prototype.toLocaleString()"; 29 30 var status = ''; 31 var actual = ''; 32 var expect = ''; 33 34 35 writeHeaderToLog( SECTION + " "+ TITLE); 36 37 var now = new Date(); 38 39 // first, some generic tests - 40 41 status = "typeof (now.toLocaleString())"; 42 actual = typeof (now.toLocaleString()); 43 expect = "string"; 44 addTestCase(); 45 46 status = "Date.prototype.toLocaleString.length"; 47 actual = Date.prototype.toLocaleString.length; 48 expect = 0; 49 addTestCase(); 50 51 // Date.parse is accurate to the second; valueOf() to the millisecond - 52 status = "Math.abs(Date.parse(now.toLocaleString('en-US')) - now.valueOf()) < 1000 + (!isRepeatedTime(now) ? 0 : msPerHour)"; 53 actual = Math.abs(Date.parse(now.toLocaleString('en-US')) - now.valueOf()) < 1000 + (!isRepeatedTime(now) ? 0 : msPerHour); 54 expect = true; 55 addTestCase(); 56 57 58 // Test with repeated and skipped time due to daylight saving time. 59 for (var date of [ 60 // Switch from standard time to daylight saving time for PST8PDT. 61 new Date(Date.parse("2025-11-02T08:00:00Z")), 62 new Date(Date.parse("2025-11-02T09:00:00Z")), 63 64 // Switch from daylight saving time to standard time for PST8PDT. 65 new Date(Date.parse("2025-03-09T09:00:00Z")), 66 new Date(Date.parse("2025-03-09T10:00:00Z")), 67 ]) { 68 status = "Math.abs(Date.parse(date.toLocaleString('en-US')) - date.valueOf()) < 1000 + (!isRepeatedTime(date) ? 0 : msPerHour)"; 69 actual = Math.abs(Date.parse(date.toLocaleString('en-US')) - date.valueOf()) < 1000 + (!isRepeatedTime(date) ? 0 : msPerHour); 70 expect = true; 71 addTestCase(); 72 } 73 74 75 // 1970 76 addDateTestCase(0); 77 addDateTestCase(TZ_ADJUST); 78 79 80 // 1900 81 addDateTestCase(UTC_01_JAN_1900); 82 addDateTestCase(UTC_01_JAN_1900 -TZ_ADJUST); 83 84 85 // 2000 86 addDateTestCase(UTC_01_JAN_2000); 87 addDateTestCase(UTC_01_JAN_2000 -TZ_ADJUST); 88 89 90 // 29 Feb 2000 91 addDateTestCase(UTC_29_FEB_2000); 92 addDateTestCase(UTC_29_FEB_2000 - 1000); 93 addDateTestCase(UTC_29_FEB_2000 - TZ_ADJUST); 94 95 96 // 2005 97 addDateTestCase(UTC_01_JAN_2005); 98 addDateTestCase(UTC_01_JAN_2005 - 1000); 99 addDateTestCase(UTC_01_JAN_2005-TZ_ADJUST); 100 101 102 103 //----------------------------------------------------------------------------------------------------- 104 test(); 105 //----------------------------------------------------------------------------------------------------- 106 107 108 function addTestCase() 109 { 110 AddTestCase( 111 status, 112 expect, 113 actual); 114 } 115 116 117 function addDateTestCase(date_given_in_milliseconds) 118 { 119 var givenDate = new Date(date_given_in_milliseconds); 120 121 status = 'Date.parse(' + givenDate + ').toLocaleString("en-US"))'; 122 actual = Date.parse(givenDate.toLocaleString("en-US")); 123 expect = date_given_in_milliseconds; 124 addTestCase(); 125 } 126 127 128 // Repeated local time when switching from daylight saving time to standard time. 129 function isRepeatedTime(date) 130 { 131 var offset1 = date.getTimezoneOffset(); 132 var offset2 = new Date(date.valueOf() - msPerHour).getTimezoneOffset(); 133 return offset1 > offset2; 134 }