michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: File Name: 15.9.5.4.js michael@0: ECMA Section: 15.9.5.4 Date.prototype.toTimeString() michael@0: Description: michael@0: This function returns a string value. The contents of the string are michael@0: implementation dependent, but are intended to represent the "time" michael@0: portion of the Date in the current time zone in a convenient, michael@0: human-readable form. We test the content of the string by checking michael@0: that d.toDateString() + d.toTimeString() == d.toString() michael@0: michael@0: Author: pschwartau@netscape.com michael@0: Date: 14 november 2000 michael@0: Revised: 07 january 2002 because of a change in JS Date format: michael@0: michael@0: See http://bugzilla.mozilla.org/show_bug.cgi?id=118266 (SpiderMonkey) michael@0: See http://bugzilla.mozilla.org/show_bug.cgi?id=118636 (Rhino) michael@0: */ michael@0: //----------------------------------------------------------------------------- michael@0: var SECTION = "15.9.5.4"; michael@0: var VERSION = "ECMA_3"; michael@0: var TITLE = "Date.prototype.toTimeString()"; michael@0: michael@0: var status = ''; michael@0: var actual = ''; michael@0: var expect = ''; michael@0: var givenDate; michael@0: var year = ''; michael@0: var regexp = ''; michael@0: var reducedDateString = ''; michael@0: var hopeThisIsTimeString = ''; michael@0: var cnEmptyString = ''; michael@0: var cnERR ='OOPS! FATAL ERROR: no regexp match in extractTimeString()'; michael@0: michael@0: startTest(); michael@0: writeHeaderToLog( SECTION + " "+ TITLE); michael@0: michael@0: // first, a couple of generic tests - michael@0: michael@0: status = "typeof (now.toTimeString())"; michael@0: actual = typeof (now.toTimeString()); michael@0: expect = "string"; michael@0: addTestCase(); michael@0: michael@0: status = "Date.prototype.toTimeString.length"; michael@0: actual = Date.prototype.toTimeString.length; michael@0: expect = 0; michael@0: addTestCase(); michael@0: michael@0: // 1970 michael@0: addDateTestCase(0); michael@0: addDateTestCase(TZ_ADJUST); michael@0: michael@0: michael@0: // 1900 michael@0: addDateTestCase(TIME_1900); michael@0: addDateTestCase(TIME_1900 - TZ_ADJUST); michael@0: michael@0: michael@0: // 2000 michael@0: addDateTestCase(TIME_2000); michael@0: addDateTestCase(TIME_2000 - TZ_ADJUST); michael@0: michael@0: michael@0: // 29 Feb 2000 michael@0: addDateTestCase(UTC_29_FEB_2000); michael@0: addDateTestCase(UTC_29_FEB_2000 - 1000); michael@0: addDateTestCase(UTC_29_FEB_2000 - TZ_ADJUST); michael@0: michael@0: michael@0: // Now michael@0: addDateTestCase( TIME_NOW); michael@0: addDateTestCase( TIME_NOW - TZ_ADJUST); michael@0: michael@0: michael@0: // 2005 michael@0: addDateTestCase(UTC_1_JAN_2005); michael@0: addDateTestCase(UTC_1_JAN_2005 - 1000); michael@0: addDateTestCase(UTC_1_JAN_2005 - TZ_ADJUST); michael@0: michael@0: //----------------------------------------------------------------------------------------------------- michael@0: test(); michael@0: //----------------------------------------------------------------------------------------------------- michael@0: michael@0: function addTestCase() michael@0: { michael@0: new TestCase( michael@0: SECTION, michael@0: status, michael@0: expect, michael@0: actual); michael@0: } michael@0: michael@0: function addDateTestCase(date_given_in_milliseconds) michael@0: { michael@0: givenDate = new Date(date_given_in_milliseconds); michael@0: michael@0: status = '(' + givenDate + ').toTimeString()'; michael@0: actual = givenDate.toTimeString(); michael@0: expect = extractTimeString(givenDate); michael@0: addTestCase(); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * As of 2002-01-07, the format for JavaScript dates changed. michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=118266 (SpiderMonkey) michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=118636 (Rhino) michael@0: * michael@0: * WAS: Mon Jan 07 13:40:34 GMT-0800 (Pacific Standard Time) 2002 michael@0: * NOW: Mon Jan 07 2002 13:40:34 GMT-0800 (Pacific Standard Time) michael@0: * michael@0: * Thus, use a regexp of the form /date.toDateString()(.*)$/ michael@0: * to capture the TimeString into the first backreference - michael@0: */ michael@0: function extractTimeString(date) michael@0: { michael@0: regexp = new RegExp(date.toDateString() + '(.*)' + '$'); michael@0: michael@0: try michael@0: { michael@0: hopeThisIsTimeString = date.toString().match(regexp)[1]; michael@0: } michael@0: catch(e) michael@0: { michael@0: return cnERR; michael@0: } michael@0: michael@0: // trim any leading or trailing spaces - michael@0: return trimL(trimR(hopeThisIsTimeString)); michael@0: } michael@0: michael@0: michael@0: function trimL(s) michael@0: { michael@0: if (!s) {return cnEmptyString;}; michael@0: for (var i = 0; i!=s.length; i++) {if (s[i] != ' ') {break;}} michael@0: return s.substring(i); michael@0: } michael@0: michael@0: michael@0: function trimR(s) michael@0: { michael@0: if (!s) {return cnEmptyString;}; michael@0: for (var i = (s.length - 1); i!=-1; i--) {if (s[i] != ' ') {break;}} michael@0: return s.substring(0, i+1); michael@0: }