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/
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.
11 * The Original Code is Mozilla Communicator client code, released March
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
23 File Name: 15.9.5.4.js
24 ECMA Section: 15.9.5.4 Date.prototype.toTimeString()
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.toTimeString() == d.toString()
32 Author: pschwartau@netscape.com
33 Date: 14 november 2000
34 Revised: 07 january 2002 because of a change in JS Date format:
36 See http://bugzilla.mozilla.org/show_bug.cgi?id=118266 (SpiderMonkey)
37 See http://bugzilla.mozilla.org/show_bug.cgi?id=118636 (Rhino)
39 //-----------------------------------------------------------------------------
40 var SECTION = "15.9.5.4";
41 var VERSION = "ECMA_3";
42 var TITLE = "Date.prototype.toTimeString()";
50 var reducedDateString = '';
51 var hopeThisIsTimeString = '';
52 var cnEmptyString = '';
53 var cnERR ='OOPS! FATAL ERROR: no regexp match in extractTimeString()';
58 writeHeaderToLog( SECTION + " "+ TITLE);
61 //-----------------------------------------------------------------------------------------------------
62 var testcases = new Array();
63 //-----------------------------------------------------------------------------------------------------
66 // first, a couple of generic tests -
68 status = "typeof (now.toTimeString())";
69 actual = typeof (now.toTimeString());
73 status = "Date.prototype.toTimeString.length";
74 actual = Date.prototype.toTimeString.length;
83 addDateTestCase(TZ_ADJUST);
87 addDateTestCase(TIME_1900);
88 addDateTestCase(TIME_1900 - TZ_ADJUST);
92 addDateTestCase(TIME_2000);
93 addDateTestCase(TIME_2000 - TZ_ADJUST);
97 addDateTestCase(UTC_29_FEB_2000);
98 addDateTestCase(UTC_29_FEB_2000 - 1000);
99 addDateTestCase(UTC_29_FEB_2000 - TZ_ADJUST);
103 addDateTestCase( TIME_NOW);
104 addDateTestCase( TIME_NOW - TZ_ADJUST);
108 addDateTestCase(UTC_1_JAN_2005);
109 addDateTestCase(UTC_1_JAN_2005 - 1000);
110 addDateTestCase(UTC_1_JAN_2005 - TZ_ADJUST);
114 //-----------------------------------------------------------------------------------------------------
116 //-----------------------------------------------------------------------------------------------------
119 function addTestCase()
121 testcases[tc++] = new TestCase( SECTION, status, expect, actual);
125 function addDateTestCase(date_given_in_milliseconds)
127 givenDate = new Date(date_given_in_milliseconds);
129 status = '(' + givenDate + ').toTimeString()';
130 actual = givenDate.toTimeString();
131 expect = extractTimeString(givenDate);
137 * As of 2002-01-07, the format for JavaScript dates changed.
138 * See http://bugzilla.mozilla.org/show_bug.cgi?id=118266 (SpiderMonkey)
139 * See http://bugzilla.mozilla.org/show_bug.cgi?id=118636 (Rhino)
141 * WAS: Mon Jan 07 13:40:34 GMT-0800 (Pacific Standard Time) 2002
142 * NOW: Mon Jan 07 2002 13:40:34 GMT-0800 (Pacific Standard Time)
144 * Thus, use a regexp of the form /date.toDateString()(.*)$/
145 * to capture the TimeString into the first backreference -
147 function extractTimeString(date)
149 regexp = new RegExp(date.toDateString() + '(.*)' + '$');
153 hopeThisIsTimeString = date.toString().match(regexp)[1];
160 // trim any leading or trailing spaces -
161 return trimL(trimR(hopeThisIsTimeString));
167 if (!s) {return cnEmptyString;};
168 for (var i = 0; i!=s.length; i++) {if (s[i] != ' ') {break;}}
169 return s.substring(i);
175 if (!s) {return cnEmptyString;};
176 for (var i = (s.length - 1); i!=-1; i--) {if (s[i] != ' ') {break;}}
177 return s.substring(0, i+1);
183 for ( tc=0; tc < testcases.length; tc++ )
185 testcases[tc].passed = writeTestCaseResult(
186 testcases[tc].expect,
187 testcases[tc].actual,
188 testcases[tc].description + " = " + testcases[tc].actual );
190 testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";