Home | History | Annotate | Download | only in Date
      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/
      5  *
      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.
     10  *
     11  * The Original Code is Mozilla Communicator client code, released March
     12  * 31, 1998.
     13  *
     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
     17  * Rights Reserved.
     18  *
     19  * Contributor(s):
     20  *
     21  */
     22 /**
     23     File Name:    15.9.5.4.js
     24     ECMA Section: 15.9.5.4 Date.prototype.toTimeString()
     25     Description:
     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.toTimeString()  ==  d.toString()
     31 
     32     Author:  pschwartau (at) netscape.com
     33     Date:    14 november 2000
     34     Revised: 07 january 2002  because of a change in JS Date format:
     35 
     36     See http://bugzilla.mozilla.org/show_bug.cgi?id=118266 (SpiderMonkey)
     37     See http://bugzilla.mozilla.org/show_bug.cgi?id=118636 (Rhino)
     38 */
     39 //-----------------------------------------------------------------------------
     40    var SECTION = "15.9.5.4";
     41    var VERSION = "ECMA_3";
     42    var TITLE   = "Date.prototype.toTimeString()";
     43 
     44    var status = '';
     45    var actual = '';
     46    var expect = '';
     47    var givenDate;
     48    var year = '';
     49    var regexp = '';
     50    var reducedDateString = '';
     51    var hopeThisIsTimeString = '';
     52    var cnEmptyString = '';
     53    var cnERR ='OOPS! FATAL ERROR: no regexp match in extractTimeString()';
     54 
     55 
     56 
     57    startTest();
     58    writeHeaderToLog( SECTION + " "+ TITLE);
     59 
     60 
     61 //-----------------------------------------------------------------------------------------------------
     62    var testcases = new Array();
     63 //-----------------------------------------------------------------------------------------------------
     64 
     65 
     66    // first, a couple of generic tests -
     67 
     68    status = "typeof (now.toTimeString())";
     69    actual =   typeof (now.toTimeString());
     70    expect = "string";
     71    addTestCase();
     72 
     73    status = "Date.prototype.toTimeString.length";
     74    actual =  Date.prototype.toTimeString.length;
     75    expect =  0;
     76    addTestCase();
     77 
     78 
     79 
     80 
     81    // 1970
     82    addDateTestCase(0);
     83    addDateTestCase(TZ_ADJUST);
     84 
     85 
     86    // 1900
     87    addDateTestCase(TIME_1900);
     88    addDateTestCase(TIME_1900 - TZ_ADJUST);
     89 
     90 
     91    // 2000
     92    addDateTestCase(TIME_2000);
     93    addDateTestCase(TIME_2000 - TZ_ADJUST);
     94 
     95 
     96    // 29 Feb 2000
     97    addDateTestCase(UTC_29_FEB_2000);
     98    addDateTestCase(UTC_29_FEB_2000 - 1000);
     99    addDateTestCase(UTC_29_FEB_2000 - TZ_ADJUST);
    100 
    101 
    102    // Now
    103    addDateTestCase( TIME_NOW);
    104    addDateTestCase( TIME_NOW - TZ_ADJUST);
    105 
    106 
    107    // 2005
    108    addDateTestCase(UTC_1_JAN_2005);
    109    addDateTestCase(UTC_1_JAN_2005 - 1000);
    110    addDateTestCase(UTC_1_JAN_2005 - TZ_ADJUST);
    111 
    112 
    113 
    114 //-----------------------------------------------------------------------------------------------------
    115    test();
    116 //-----------------------------------------------------------------------------------------------------
    117 
    118 
    119 function addTestCase()
    120 {
    121   testcases[tc++] = new TestCase( SECTION, status, expect, actual);
    122 }
    123 
    124 
    125 function addDateTestCase(date_given_in_milliseconds)
    126 {
    127    givenDate = new Date(date_given_in_milliseconds);
    128 
    129    status = '('  +  givenDate  +  ').toTimeString()';
    130    actual = givenDate.toTimeString();
    131    expect = extractTimeString(givenDate);
    132    addTestCase();
    133 }
    134 
    135 
    136 /*
    137  * As of 2002-01-07, the format for JavaScript dates changed.
    138  * See http://bugzilla.mozilla.org/show_bug.cgi?id=118266 (SpiderMonkey)
    139  * See http://bugzilla.mozilla.org/show_bug.cgi?id=118636 (Rhino)
    140  *
    141  * WAS: Mon Jan 07 13:40:34 GMT-0800 (Pacific Standard Time) 2002
    142  * NOW: Mon Jan 07 2002 13:40:34 GMT-0800 (Pacific Standard Time)
    143  *
    144  * Thus, use a regexp of the form /date.toDateString()(.*)$/
    145  * to capture the TimeString into the first backreference -
    146  */
    147 function extractTimeString(date)
    148 {
    149   regexp = new RegExp(date.toDateString() + '(.*)' + '$');
    150 
    151   try
    152   {
    153     hopeThisIsTimeString = date.toString().match(regexp)[1];
    154   }
    155   catch(e)
    156   {
    157     return cnERR;
    158   }
    159 
    160   // trim any leading or trailing spaces -
    161   return trimL(trimR(hopeThisIsTimeString));
    162  }
    163 
    164 
    165 function trimL(s)
    166 {
    167   if (!s) {return cnEmptyString;};
    168   for (var i = 0; i!=s.length; i++) {if (s[i] != ' ') {break;}}
    169   return s.substring(i);
    170 }
    171 
    172 
    173 function trimR(s)
    174 {
    175   if (!s) {return cnEmptyString;};
    176   for (var i = (s.length - 1); i!=-1; i--) {if (s[i] != ' ') {break;}}
    177   return s.substring(0, i+1);
    178 }
    179 
    180 
    181 function test()
    182 {
    183   for ( tc=0; tc < testcases.length; tc++ )
    184   {
    185     testcases[tc].passed = writeTestCaseResult(
    186                                                testcases[tc].expect,
    187                                                testcases[tc].actual,
    188                                                testcases[tc].description  +  " = "  +  testcases[tc].actual );
    189 
    190     testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
    191   }
    192   stopTest();
    193   return (testcases);
    194 }
    195