Debian Codegraph @

rhino/testsrc/tests/ecma_3/Date/15.9.5.4.js

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