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.3.8.js
     24     ECMA Section:       15.9.3.8 The Date Constructor
     25                         new Date( value )
     26     Description:        The [[Prototype]] property of the newly constructed
     27                         object is set to the original Date prototype object,
     28                         the one that is the initial valiue of Date.prototype.
     29 
     30                         The [[Class]] property of the newly constructed object is
     31                         set to "Date".
     32 
     33                         The [[Value]] property of the newly constructed object is
     34                         set as follows:
     35 
     36                         1. Call ToPrimitive(value)
     37                         2. If Type( Result(1) ) is String, then go to step 5.
     38                         3. Let V be  ToNumber( Result(1) ).
     39                         4. Set the [[Value]] property of the newly constructed
     40                             object to TimeClip(V) and return.
     41                         5. Parse Result(1) as a date, in exactly the same manner
     42                             as for the parse method.  Let V be the time value for
     43                             this date.
     44                         6. Go to step 4.
     45 
     46     Author:             christine (at) netscape.com
     47     Date:               28 october 1997
     48     Version:            9706
     49 
     50 */
     51 
     52     var VERSION = "ECMA_1";
     53     startTest();
     54     var SECTION = "15.9.3.8";
     55     var TYPEOF  = "object";
     56 
     57     var TIME        = 0;
     58     var UTC_YEAR    = 1;
     59     var UTC_MONTH   = 2;
     60     var UTC_DATE    = 3;
     61     var UTC_DAY     = 4;
     62     var UTC_HOURS   = 5;
     63     var UTC_MINUTES = 6;
     64     var UTC_SECONDS = 7;
     65     var UTC_MS      = 8;
     66 
     67     var YEAR        = 9;
     68     var MONTH       = 10;
     69     var DATE        = 11;
     70     var DAY         = 12;
     71     var HOURS       = 13;
     72     var MINUTES     = 14;
     73     var SECONDS     = 15;
     74     var MS          = 16;
     75 
     76 
     77 //  for TCMS, the testcases array must be global.
     78     var tc= 0;
     79     var TITLE = "Date constructor:  new Date( value )";
     80     var SECTION = "15.9.3.8";
     81     var VERSION = "ECMA_1";
     82     startTest();
     83 
     84     writeHeaderToLog( SECTION +" " + TITLE );
     85 
     86     testcases = new Array();
     87     getTestCases();
     88 
     89 //  all tests must call a function that returns a boolean value
     90     test();
     91 
     92 function getTestCases( ) {
     93     // all the "ResultArrays" below are hard-coded to Pacific Standard Time values -
     94     var TZ_ADJUST =  -TZ_PST * msPerHour;
     95 
     96 
     97     // Dates around 2000
     98 
     99     addNewTestCase( new Date(TIME_2000+TZ_ADJUST),
    100                     "new Date(" +(TIME_2000+TZ_ADJUST)+")",
    101                     [TIME_2000+TZ_ADJUST,2000,0,1,6,8,0,0,0,2000,0,1,6,0,0,0,0] );
    102 
    103     addNewTestCase( new Date(TIME_2000),
    104                     "new Date(" +TIME_2000+")",
    105                     [TIME_2000,2000,0,1,6,0,0,0,0,1999,11,31,5,16,0,0,0] );
    106 
    107     addNewTestCase( new Date( (new Date(TIME_2000+TZ_ADJUST)).toString()),
    108                     "new Date(\"" +(new Date(TIME_2000+TZ_ADJUST)).toString()+"\")",
    109                     [TIME_2000+TZ_ADJUST,2000,0,1,6,8,0,0,0,2000,0,1,6,0,0,0,0] );
    110 
    111     addNewTestCase( new Date((new Date(TIME_2000)).toString()),
    112                    "new Date(\"" +(new Date(TIME_2000)).toString()+"\")",
    113                    [TIME_2000,2000,0,1,6,0,0,0,0,1999,11,31,5,16,0,0,0] );
    114 
    115 
    116     addNewTestCase(  new Date( (new Date(TIME_2000+TZ_ADJUST)).toUTCString()),
    117                     "new Date(\"" +(new Date(TIME_2000+TZ_ADJUST)).toUTCString()+"\")",
    118                     [TIME_2000+TZ_ADJUST,2000,0,1,6,8,0,0,0,2000,0,1,6,0,0,0,0] );
    119 
    120     addNewTestCase( new Date( (new Date(TIME_2000)).toUTCString()),
    121                     "new Date(\"" +(new Date(TIME_2000)).toUTCString()+"\")",
    122                     [TIME_2000,2000,0,1,6,0,0,0,0,1999,11,31,5,16,0,0,0] );
    123 /*
    124     // Dates around Feb 29, 2000
    125 
    126     var UTC_FEB_29_2000 = TIME_2000 + 31*msPerDay + 28*msPerDay;
    127     var PST_FEB_29_2000 = UTC_FEB_29_2000 + TZ_ADJUST;
    128 
    129     addNewTestCase( new Date(UTC_FEB_29_2000),
    130                     "new Date("+UTC_FEB_29_2000+")",
    131                     [UTC_FEB_29_2000,2000,1,29,2,0,0,0,0,2000,1,28,1,16,0,0,0] );
    132 
    133     addNewTestCase( new Date(PST_FEB_29_2000),
    134                     "new Date("+PST_FEB_29_2000+")",
    135                     [PST_FEB_29_2000,2000,1,29,2,8,0,0,0,2000,1,29,2,0,0,0,0] );
    136 
    137     addNewTestCase( new Date( (new Date(UTC_FEB_29_2000)).toString() ),
    138                     "new Date(\""+(new Date(UTC_FEB_29_2000)).toString()+"\")",
    139                     [UTC_FEB_29_2000,2000,1,29,2,0,0,0,0,2000,1,28,1,16,0,0,0] );
    140 
    141     addNewTestCase( new Date( (new Date(PST_FEB_29_2000)).toString() ),
    142                     "new Date(\""+(new Date(PST_FEB_29_2000)).toString()+"\")",
    143                     [PST_FEB_29_2000,2000,1,29,2,8,0,0,0,2000,1,29,2,0,0,0,0] );
    144 
    145 //  Parsing toLocaleString() is not guaranteed by ECMA.
    146 //    addNewTestCase( "new Date(\""+(new Date(UTC_FEB_29_2000)).toLocaleString()+"\")",  [UTC_FEB_29_2000,2000,1,29,2,0,0,0,0,2000,1,28,1,16,0,0,0] );
    147 //    addNewTestCase( "new Date(\""+(new Date(PST_FEB_29_2000)).toLocaleString()+"\")",  [PST_FEB_29_2000,2000,1,29,2,8,0,0,0,2000,1,29,2,0,0,0,0] );
    148 
    149     addNewTestCase( new Date( (new Date(UTC_FEB_29_2000)).toGMTString() ),
    150                     "new Date(\""+(new Date(UTC_FEB_29_2000)).toGMTString()+"\")",
    151                     [UTC_FEB_29_2000,2000,1,29,2,0,0,0,0,2000,1,28,1,16,0,0,0] );
    152 
    153     addNewTestCase( new Date( (new Date(PST_FEB_29_2000)).toGMTString() ),
    154                     "new Date(\""+(new Date(PST_FEB_29_2000)).toGMTString()+"\")",
    155                      [PST_FEB_29_2000,2000,1,29,2,8,0,0,0,2000,1,29,2,0,0,0,0] );
    156 
    157     // Dates around 1900
    158 
    159     var PST_1900 = TIME_1900 + 8*msPerHour;
    160 
    161     addNewTestCase( new Date( TIME_1900 ),
    162                     "new Date("+TIME_1900+")",
    163                     [TIME_1900,1900,0,1,1,0,0,0,0,1899,11,31,0,16,0,0,0] );
    164 
    165     addNewTestCase( new Date(PST_1900),
    166                     "new Date("+PST_1900+")",
    167                     [ PST_1900,1900,0,1,1,8,0,0,0,1900,0,1,1,0,0,0,0] );
    168 
    169     addNewTestCase( new Date( (new Date(TIME_1900)).toString() ),
    170                     "new Date(\""+(new Date(TIME_1900)).toString()+"\")",
    171                     [TIME_1900,1900,0,1,1,0,0,0,0,1899,11,31,0,16,0,0,0] );
    172 
    173     addNewTestCase( new Date( (new Date(PST_1900)).toString() ),
    174                     "new Date(\""+(new Date(PST_1900 )).toString()+"\")",
    175                     [ PST_1900,1900,0,1,1,8,0,0,0,1900,0,1,1,0,0,0,0] );
    176 
    177     addNewTestCase( new Date( (new Date(TIME_1900)).toUTCString() ),
    178                     "new Date(\""+(new Date(TIME_1900)).toUTCString()+"\")",
    179                     [TIME_1900,1900,0,1,1,0,0,0,0,1899,11,31,0,16,0,0,0] );
    180 
    181     addNewTestCase( new Date( (new Date(PST_1900)).toUTCString() ),
    182                     "new Date(\""+(new Date(PST_1900 )).toUTCString()+"\")",
    183                     [ PST_1900,1900,0,1,1,8,0,0,0,1900,0,1,1,0,0,0,0] );
    184 
    185 //    addNewTestCase( "new Date(\""+(new Date(TIME_1900)).toLocaleString()+"\")",   [TIME_1900,1900,0,1,1,0,0,0,0,1899,11,31,0,16,0,0,0] );
    186 //    addNewTestCase( "new Date(\""+(new Date(PST_1900 )).toLocaleString()+"\")",   [ PST_1900,1900,0,1,1,8,0,0,0,1900,0,1,1,0,0,0,0] );
    187 */
    188 /*
    189    This test case is incorrect.  Need to fix the DaylightSavings functions in
    190    shell.js for this to work properly.
    191 
    192     var DST_START_1998 = UTC( GetSecondSundayInMarch(TimeFromYear(1998)) + 2*msPerHour )
    193 
    194     addNewTestCase( new Date(DST_START_1998-1),
    195                     "new Date("+(DST_START_1998-1)+")",
    196                     [DST_START_1998-1,1998,3,5,0,9,59,59,999,1998,3,5,0,1,59,59,999] );
    197 
    198     addNewTestCase( new Date(DST_START_1998),
    199                     "new Date("+DST_START_1998+")",
    200                     [DST_START_1998,1998,3,5,0,10,0,0,0,1998,3,5,0,3,0,0,0]);
    201 
    202     var DST_END_1998 = UTC( GetFirstSundayInNovember(TimeFromYear(1998)) + 2*msPerHour );
    203 
    204     addNewTestCase ( new Date(DST_END_1998-1),
    205                     "new Date("+(DST_END_1998-1)+")",
    206                     [DST_END_1998-1,1998,9,25,0,8,59,59,999,1998,9,25,0,1,59,59,999] );
    207 
    208     addNewTestCase ( new Date(DST_END_1998),
    209                     "new Date("+DST_END_1998+")",
    210                     [DST_END_1998,1998,9,25,0,9,0,0,0,1998,9,25,0,1,0,0,0] );
    211 */
    212 }
    213 
    214 function addNewTestCase( DateCase, DateString, ResultArray ) {
    215     //adjust hard-coded ResultArray for tester's timezone instead of PST
    216     adjustResultArray(ResultArray, 'msMode');
    217 
    218     item = testcases.length;
    219 
    220     testcases[item++] = new TestCase( SECTION, DateString+".getTime()", ResultArray[TIME],       DateCase.getTime() );
    221     testcases[item++] = new TestCase( SECTION, DateString+".valueOf()", ResultArray[TIME],       DateCase.valueOf() );
    222     testcases[item++] = new TestCase( SECTION, DateString+".getUTCFullYear()",      ResultArray[UTC_YEAR], DateCase.getUTCFullYear() );
    223     testcases[item++] = new TestCase( SECTION, DateString+".getUTCMonth()",         ResultArray[UTC_MONTH],  DateCase.getUTCMonth() );
    224     testcases[item++] = new TestCase( SECTION, DateString+".getUTCDate()",          ResultArray[UTC_DATE],   DateCase.getUTCDate() );
    225     testcases[item++] = new TestCase( SECTION, DateString+".getUTCDay()",           ResultArray[UTC_DAY],    DateCase.getUTCDay() );
    226     testcases[item++] = new TestCase( SECTION, DateString+".getUTCHours()",         ResultArray[UTC_HOURS],  DateCase.getUTCHours() );
    227     testcases[item++] = new TestCase( SECTION, DateString+".getUTCMinutes()",       ResultArray[UTC_MINUTES],DateCase.getUTCMinutes() );
    228     testcases[item++] = new TestCase( SECTION, DateString+".getUTCSeconds()",       ResultArray[UTC_SECONDS],DateCase.getUTCSeconds() );
    229     testcases[item++] = new TestCase( SECTION, DateString+".getUTCMilliseconds()",  ResultArray[UTC_MS],     DateCase.getUTCMilliseconds() );
    230     testcases[item++] = new TestCase( SECTION, DateString+".getFullYear()",         ResultArray[YEAR],       DateCase.getFullYear() );
    231     testcases[item++] = new TestCase( SECTION, DateString+".getMonth()",            ResultArray[MONTH],      DateCase.getMonth() );
    232     testcases[item++] = new TestCase( SECTION, DateString+".getDate()",             ResultArray[DATE],       DateCase.getDate() );
    233     testcases[item++] = new TestCase( SECTION, DateString+".getDay()",              ResultArray[DAY],        DateCase.getDay() );
    234     testcases[item++] = new TestCase( SECTION, DateString+".getHours()",            ResultArray[HOURS],      DateCase.getHours() );
    235     testcases[item++] = new TestCase( SECTION, DateString+".getMinutes()",          ResultArray[MINUTES],    DateCase.getMinutes() );
    236     testcases[item++] = new TestCase( SECTION, DateString+".getSeconds()",          ResultArray[SECONDS],    DateCase.getSeconds() );
    237     testcases[item++] = new TestCase( SECTION, DateString+".getMilliseconds()",     ResultArray[MS],         DateCase.getMilliseconds() );
    238 }
    239 
    240 function test() {
    241     for( tc = 0; tc < testcases.length; tc++ ) {
    242 
    243         testcases[tc].passed = writeTestCaseResult(
    244                                 testcases[tc].expect,
    245                                 testcases[tc].actual,
    246                                 testcases[tc].description +" = " +
    247                                 testcases[tc].actual );
    248     }
    249         stopTest();
    250 
    251     //  all tests must return a boolean value
    252         return ( testcases );
    253 }
    254