1/* The contents of this file are subject to the Netscape Public 2 * License Version 1.1 (the "License"); you may not use this file 3 * except in compliance with the License. You may obtain a copy of 4 * the License at http://www.mozilla.org/NPL/ 5 * 6 * Software distributed under the License is distributed on an "AS 7 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 8 * implied. See the License for the specific language governing 9 * rights and limitations under the License. 10 * 11 * The Original Code is Mozilla Communicator client code, released March 12 * 31, 1998. 13 * 14 * The Initial Developer of the Original Code is Netscape Communications 15 * Corporation. Portions created by Netscape are 16 * Copyright (C) 1998 Netscape Communications Corporation. All 17 * Rights Reserved. 18 * 19 * Contributor(s): 20 * 21 */ 22/** 23 File Name: 15.9.5.7.js 24 ECMA Section: 15.9.5.7 Date.prototype.toLocaleTimeString() 25 Description: 26 This function returns a string value. The contents of the string are 27 implementation dependent, but are intended to represent the "time" 28 portion of the Date in the current time zone in a convenient, 29 human-readable form. We test the content of the string by checking 30 that d.toDateString() + d.toLocaleTimeString() == d.toString() 31 32 The only headache is that as of this writing the "GMT ..." portion of 33 d.toString() is NOT included in d.toLocaleTimeString() as it is in 34 d.toTimeString(). So we have to take that into account. 35 36 Author: pschwartau@netscape.com 37 Date: 14 november 2000 38 Revised: 07 january 2002 because of a change in JS Date format: 39 40 See http://bugzilla.mozilla.org/show_bug.cgi?id=118266 (SpiderMonkey) 41 See http://bugzilla.mozilla.org/show_bug.cgi?id=118636 (Rhino) 42*/ 43//----------------------------------------------------------------------------- 44 var SECTION = "15.9.5.7"; 45 var VERSION = "ECMA_3"; 46 var TITLE = "Date.prototype.toLocaleTimeString()"; 47 48 var status = ''; 49 var actual = ''; 50 var expect = ''; 51 var givenDate; 52 var year = ''; 53 var regexp = ''; 54 var TimeString = ''; 55 var reducedDateString = ''; 56 var hopeThisIsLocaleTimeString = ''; 57 var cnERR ='OOPS! FATAL ERROR: no regexp match in extractLocaleTimeString()'; 58 59 60 startTest(); 61 writeHeaderToLog( SECTION + " "+ TITLE); 62 63 64//----------------------------------------------------------------------------------------------------- 65 var testcases = new Array(); 66//----------------------------------------------------------------------------------------------------- 67 68 69 // first, a couple generic tests - 70 71 status = "typeof (now.toLocaleTimeString())"; 72 actual = typeof (now.toLocaleTimeString()); 73 expect = "string"; 74 addTestCase(); 75 76 status = "Date.prototype.toLocaleTimeString.length"; 77 actual = Date.prototype.toLocaleTimeString.length; 78 expect = 0; 79 addTestCase(); 80 81 82 83 84 // 1970 85 addDateTestCase(0); 86 addDateTestCase(TZ_ADJUST); 87 88 89 // 1900 90 addDateTestCase(TIME_1900); 91 addDateTestCase(TIME_1900 - TZ_ADJUST); 92 93 94 // 2000 95 addDateTestCase(TIME_2000); 96 addDateTestCase(TIME_2000 - TZ_ADJUST); 97 98 99 // 29 Feb 2000 100 addDateTestCase(UTC_29_FEB_2000); 101 addDateTestCase(UTC_29_FEB_2000 - 1000); 102 addDateTestCase(UTC_29_FEB_2000 - TZ_ADJUST); 103 104 105 // Now 106 addDateTestCase( TIME_NOW); 107 addDateTestCase( TIME_NOW - TZ_ADJUST); 108 109 110 // 2005 111 addDateTestCase(UTC_1_JAN_2005); 112 addDateTestCase(UTC_1_JAN_2005 - 1000); 113 addDateTestCase(UTC_1_JAN_2005 - TZ_ADJUST); 114 115 116 117//----------------------------------------------------------------------------------------------------- 118 test(); 119//----------------------------------------------------------------------------------------------------- 120 121 122function addTestCase() 123{ 124 testcases[tc++] = new TestCase( SECTION, status, expect, actual); 125} 126 127 128function addDateTestCase(date_given_in_milliseconds) 129{ 130 givenDate = new Date(date_given_in_milliseconds); 131 132 status = '(' + givenDate + ').toLocaleTimeString()'; 133 actual = givenDate.toLocaleTimeString(); 134 expect = extractLocaleTimeString(givenDate); 135 addTestCase(); 136} 137 138 139/* 140 * As of 2002-01-07, the format for JavaScript dates changed. 141 * See http://bugzilla.mozilla.org/show_bug.cgi?id=118266 (SpiderMonkey) 142 * See http://bugzilla.mozilla.org/show_bug.cgi?id=118636 (Rhino) 143 * 144 * WAS: Mon Jan 07 13:40:34 GMT-0800 (Pacific Standard Time) 2002 145 * NOW: Mon Jan 07 2002 13:40:34 GMT-0800 (Pacific Standard Time) 146 * 147 * So first, use a regexp of the form /date.toDateString()(.*)$/ 148 * to capture the TimeString into the first backreference. 149 * 150 * Then remove the GMT string from TimeString (see introduction above) 151 */ 152function extractLocaleTimeString(date) 153{ 154 regexp = new RegExp(date.toDateString() + '(.*)' + '$'); 155 try 156 { 157 TimeString = date.toString().match(regexp)[1]; 158 } 159 catch(e) 160 { 161 return cnERR; 162 } 163 164 /* 165 * Now remove the GMT part of the TimeString. 166 * Guard against dates with two "GMT"s, like: 167 * Jan 01 00:00:00 GMT+0000 (GMT Standard Time) 168 */ 169 regexp= /([^G]*)GMT.*/; 170 try 171 { 172 hopeThisIsLocaleTimeString = TimeString.match(regexp)[1]; 173 } 174 catch(e) 175 { 176 return TimeString; 177 } 178 179 // trim any leading or trailing spaces - 180 return trimL(trimR(hopeThisIsLocaleTimeString)); 181} 182 183 184function trimL(s) 185{ 186 if (!s) {return cnEmptyString;}; 187 for (var i = 0; i!=s.length; i++) {if (s[i] != ' ') {break;}} 188 return s.substring(i); 189} 190 191function trimR(s) 192{ 193 for (var i = (s.length - 1); i!=-1; i--) {if (s[i] != ' ') {break;}} 194 return s.substring(0, i+1); 195} 196 197 198function test() 199{ 200 for ( tc=0; tc < testcases.length; tc++ ) 201 { 202 testcases[tc].passed = writeTestCaseResult( 203 testcases[tc].expect, 204 testcases[tc].actual, 205 testcases[tc].description + " = " + testcases[tc].actual ); 206 207 testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value "; 208 } 209 stopTest(); 210 return (testcases); 211} 212