js/src/tests/ecma_3/Date/15.9.5.4.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial