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.7.js
24 ECMA Section: 15.9.5.7 Date.prototype.toLocaleTimeString()
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()
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.
36 Author: pschwartau@netscape.com
37 Date: 14 november 2000
38 Revised: 07 january 2002 because of a change in JS Date format:
40 See http://bugzilla.mozilla.org/show_bug.cgi?id=118266 (SpiderMonkey)
41 See http://bugzilla.mozilla.org/show_bug.cgi?id=118636 (Rhino)
43 //-----------------------------------------------------------------------------
44 var SECTION
= "15.9.5.7";
45 var VERSION
= "ECMA_3";
46 var TITLE
= "Date.prototype.toLocaleTimeString()";
55 var reducedDateString
= '';
56 var hopeThisIsLocaleTimeString
= '';
57 var cnERR
='OOPS! FATAL ERROR: no regexp match in extractLocaleTimeString()';
61 writeHeaderToLog( SECTION
+ " "+ TITLE
);
64 //-----------------------------------------------------------------------------------------------------
65 var testcases
= new Array();
66 //-----------------------------------------------------------------------------------------------------
69 // first, a couple generic tests -
71 status
= "typeof (now.toLocaleTimeString())";
72 actual
= typeof (now
.toLocaleTimeString());
76 status
= "Date.prototype.toLocaleTimeString.length";
77 actual
= Date
.prototype.toLocaleTimeString
.length
;
86 addDateTestCase(TZ_ADJUST
);
90 addDateTestCase(TIME_1900
);
91 addDateTestCase(TIME_1900
- TZ_ADJUST
);
95 addDateTestCase(TIME_2000
);
96 addDateTestCase(TIME_2000
- TZ_ADJUST
);
100 addDateTestCase(UTC_29_FEB_2000
);
101 addDateTestCase(UTC_29_FEB_2000
- 1000);
102 addDateTestCase(UTC_29_FEB_2000
- TZ_ADJUST
);
106 addDateTestCase( TIME_NOW
);
107 addDateTestCase( TIME_NOW
- TZ_ADJUST
);
111 addDateTestCase(UTC_1_JAN_2005
);
112 addDateTestCase(UTC_1_JAN_2005
- 1000);
113 addDateTestCase(UTC_1_JAN_2005
- TZ_ADJUST
);
117 //-----------------------------------------------------------------------------------------------------
119 //-----------------------------------------------------------------------------------------------------
122 function addTestCase()
124 testcases
[tc
++] = new TestCase( SECTION
, status
, expect
, actual
);
128 function addDateTestCase(date_given_in_milliseconds
)
130 givenDate
= new Date(date_given_in_milliseconds
);
132 status
= '(' + givenDate
+ ').toLocaleTimeString()';
133 actual
= givenDate
.toLocaleTimeString();
134 expect
= extractLocaleTimeString(givenDate
);
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)
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)
147 * So first, use a regexp of the form /date.toDateString()(.*)$/
148 * to capture the TimeString into the first backreference.
150 * Then remove the GMT string from TimeString (see introduction above)
152 function extractLocaleTimeString(date
)
154 regexp
= new RegExp(date
.toDateString() + '(.*)' + '$');
157 TimeString
= date
.toString().match(regexp
)[1];
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)
169 regexp
= /([^G]*)GMT.*/;
172 hopeThisIsLocaleTimeString
= TimeString
.match(regexp
)[1];
179 // trim any leading or trailing spaces -
180 return trimL(trimR(hopeThisIsLocaleTimeString
));
186 if (!s
) {return cnEmptyString
;};
187 for (var i
= 0; i
!=s
.length
; i
++) {if (s
[i
] != ' ') {break;}}
188 return s
.substring(i
);
193 for (var i
= (s
.length
- 1); i
!=-1; i
--) {if (s
[i
] != ' ') {break;}}
194 return s
.substring(0, i
+1);
200 for ( tc
=0; tc
< testcases
.length
; tc
++ )
202 testcases
[tc
].passed
= writeTestCaseResult(
203 testcases
[tc
].expect
,
204 testcases
[tc
].actual
,
205 testcases
[tc
].description
+ " = " + testcases
[tc
].actual
);
207 testcases
[tc
].reason
+= ( testcases
[tc
].passed
) ? "" : "wrong value ";