|
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 File Name: 15.9.5.4.js |
|
8 ECMA Section: 15.9.5.4 Date.prototype.toTimeString() |
|
9 Description: |
|
10 This function returns a string value. The contents of the string are |
|
11 implementation dependent, but are intended to represent the "time" |
|
12 portion of the Date in the current time zone in a convenient, |
|
13 human-readable form. We test the content of the string by checking |
|
14 that d.toDateString() + d.toTimeString() == d.toString() |
|
15 |
|
16 Author: pschwartau@netscape.com |
|
17 Date: 14 november 2000 |
|
18 Revised: 07 january 2002 because of a change in JS Date format: |
|
19 |
|
20 See http://bugzilla.mozilla.org/show_bug.cgi?id=118266 (SpiderMonkey) |
|
21 See http://bugzilla.mozilla.org/show_bug.cgi?id=118636 (Rhino) |
|
22 */ |
|
23 //----------------------------------------------------------------------------- |
|
24 var SECTION = "15.9.5.4"; |
|
25 var VERSION = "ECMA_3"; |
|
26 var TITLE = "Date.prototype.toTimeString()"; |
|
27 |
|
28 var status = ''; |
|
29 var actual = ''; |
|
30 var expect = ''; |
|
31 var givenDate; |
|
32 var year = ''; |
|
33 var regexp = ''; |
|
34 var reducedDateString = ''; |
|
35 var hopeThisIsTimeString = ''; |
|
36 var cnEmptyString = ''; |
|
37 var cnERR ='OOPS! FATAL ERROR: no regexp match in extractTimeString()'; |
|
38 |
|
39 startTest(); |
|
40 writeHeaderToLog( SECTION + " "+ TITLE); |
|
41 |
|
42 // first, a couple of generic tests - |
|
43 |
|
44 status = "typeof (now.toTimeString())"; |
|
45 actual = typeof (now.toTimeString()); |
|
46 expect = "string"; |
|
47 addTestCase(); |
|
48 |
|
49 status = "Date.prototype.toTimeString.length"; |
|
50 actual = Date.prototype.toTimeString.length; |
|
51 expect = 0; |
|
52 addTestCase(); |
|
53 |
|
54 // 1970 |
|
55 addDateTestCase(0); |
|
56 addDateTestCase(TZ_ADJUST); |
|
57 |
|
58 |
|
59 // 1900 |
|
60 addDateTestCase(TIME_1900); |
|
61 addDateTestCase(TIME_1900 - TZ_ADJUST); |
|
62 |
|
63 |
|
64 // 2000 |
|
65 addDateTestCase(TIME_2000); |
|
66 addDateTestCase(TIME_2000 - TZ_ADJUST); |
|
67 |
|
68 |
|
69 // 29 Feb 2000 |
|
70 addDateTestCase(UTC_29_FEB_2000); |
|
71 addDateTestCase(UTC_29_FEB_2000 - 1000); |
|
72 addDateTestCase(UTC_29_FEB_2000 - TZ_ADJUST); |
|
73 |
|
74 |
|
75 // Now |
|
76 addDateTestCase( TIME_NOW); |
|
77 addDateTestCase( TIME_NOW - TZ_ADJUST); |
|
78 |
|
79 |
|
80 // 2005 |
|
81 addDateTestCase(UTC_1_JAN_2005); |
|
82 addDateTestCase(UTC_1_JAN_2005 - 1000); |
|
83 addDateTestCase(UTC_1_JAN_2005 - TZ_ADJUST); |
|
84 |
|
85 //----------------------------------------------------------------------------------------------------- |
|
86 test(); |
|
87 //----------------------------------------------------------------------------------------------------- |
|
88 |
|
89 function addTestCase() |
|
90 { |
|
91 new TestCase( |
|
92 SECTION, |
|
93 status, |
|
94 expect, |
|
95 actual); |
|
96 } |
|
97 |
|
98 function addDateTestCase(date_given_in_milliseconds) |
|
99 { |
|
100 givenDate = new Date(date_given_in_milliseconds); |
|
101 |
|
102 status = '(' + givenDate + ').toTimeString()'; |
|
103 actual = givenDate.toTimeString(); |
|
104 expect = extractTimeString(givenDate); |
|
105 addTestCase(); |
|
106 } |
|
107 |
|
108 |
|
109 /* |
|
110 * As of 2002-01-07, the format for JavaScript dates changed. |
|
111 * See http://bugzilla.mozilla.org/show_bug.cgi?id=118266 (SpiderMonkey) |
|
112 * See http://bugzilla.mozilla.org/show_bug.cgi?id=118636 (Rhino) |
|
113 * |
|
114 * WAS: Mon Jan 07 13:40:34 GMT-0800 (Pacific Standard Time) 2002 |
|
115 * NOW: Mon Jan 07 2002 13:40:34 GMT-0800 (Pacific Standard Time) |
|
116 * |
|
117 * Thus, use a regexp of the form /date.toDateString()(.*)$/ |
|
118 * to capture the TimeString into the first backreference - |
|
119 */ |
|
120 function extractTimeString(date) |
|
121 { |
|
122 regexp = new RegExp(date.toDateString() + '(.*)' + '$'); |
|
123 |
|
124 try |
|
125 { |
|
126 hopeThisIsTimeString = date.toString().match(regexp)[1]; |
|
127 } |
|
128 catch(e) |
|
129 { |
|
130 return cnERR; |
|
131 } |
|
132 |
|
133 // trim any leading or trailing spaces - |
|
134 return trimL(trimR(hopeThisIsTimeString)); |
|
135 } |
|
136 |
|
137 |
|
138 function trimL(s) |
|
139 { |
|
140 if (!s) {return cnEmptyString;}; |
|
141 for (var i = 0; i!=s.length; i++) {if (s[i] != ' ') {break;}} |
|
142 return s.substring(i); |
|
143 } |
|
144 |
|
145 |
|
146 function trimR(s) |
|
147 { |
|
148 if (!s) {return cnEmptyString;}; |
|
149 for (var i = (s.length - 1); i!=-1; i--) {if (s[i] != ' ') {break;}} |
|
150 return s.substring(0, i+1); |
|
151 } |