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.31-1.js
     24     ECMA Section:       15.9.5.31 Date.prototype.setUTCHours(hour [, min [, sec [, ms ]]] )
     25     Description:
     26     If min is not specified, this behaves as if min were specified with the value getUTCMinutes( ).
     27     If sec is not specified, this behaves as if sec were specified with the value getUTCSeconds ( ).
     28     If ms is not specified, this behaves as if ms were specified with the value getUTCMilliseconds( ).
     29 
     30    1.Let t be this time value.
     31    2.Call ToNumber(hour).
     32    3.If min is not specified, compute MinFromTime(t); otherwise, call ToNumber(min).
     33    4.If sec is not specified, compute SecFromTime(t); otherwise, call ToNumber(sec).
     34    5.If ms is not specified, compute msFromTime(t); otherwise, call ToNumber(ms).
     35    6.Compute MakeTime(Result(2), Result(3), Result(4), Result(5)).
     36    7.Compute MakeDate(Day(t), Result(6)).
     37    8.Set the [[Value]] property of the this value to TimeClip(Result(7)).
     38 
     39    1.Return the value of the [[Value]] property of the this value.
     40     Author:             christine (at) netscape.com
     41     Date:               12 november 1997
     42 */
     43     var SECTION = "15.9.5.31-1";
     44     var VERSION = "ECMA_1";
     45     startTest();
     46 
     47     writeHeaderToLog( SECTION + " Date.prototype.setUTCHours(hour [, min [, sec [, ms ]]] )");
     48 
     49     getTestCases();
     50     test();
     51 
     52 function test() {
     53     for ( tc=0; tc < testcases.length; tc++ ) {
     54         testcases[tc].passed = writeTestCaseResult(
     55                             testcases[tc].expect,
     56                             testcases[tc].actual,
     57                             testcases[tc].description +" = "+
     58                             testcases[tc].actual );
     59 
     60         testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
     61     }
     62     stopTest();
     63     return ( testcases );
     64 }
     65 
     66 function getTestCases() {
     67     addNewTestCase( 0, 0, void 0, void 0, void 0,
     68                     "TDATE = new Date(0);(TDATE).setUTCHours(0);TDATE",
     69                     UTCDateFromTime(SetUTCHours(0,0,0,0)),
     70                     LocalDateFromTime(SetUTCHours(0,0,0,0)) );
     71 
     72     addNewTestCase( 28800000, 23, 59, 999, void 0,
     73                     "TDATE = new Date(28800000);(TDATE).setUTCHours(23,59,999);TDATE",
     74                     UTCDateFromTime(SetUTCHours(28800000,23,59,999)),
     75                     LocalDateFromTime(SetUTCHours(28800000,23,59,999)) );
     76 
     77     addNewTestCase( 28800000,999,999, void 0, void 0,
     78                     "TDATE = new Date(28800000);(TDATE).setUTCHours(999,999);TDATE",
     79                     UTCDateFromTime(SetUTCHours(28800000,999,999)),
     80                     LocalDateFromTime(SetUTCHours(28800000,999,999)) );
     81 
     82     addNewTestCase( 28800000, 999, void 0, void 0, void 0,
     83                     "TDATE = new Date(28800000);(TDATE).setUTCHours(999);TDATE",
     84                     UTCDateFromTime(SetUTCHours(28800000,999,0)),
     85                     LocalDateFromTime(SetUTCHours(28800000,999,0)) );
     86 
     87     addNewTestCase( 28800000, -8670, void 0, void 0, void 0,
     88                     "TDATE = new Date(28800000);(TDATE).setUTCHours(-8670);TDATE",
     89                     UTCDateFromTime(SetUTCHours(28800000,-8670)),
     90                     LocalDateFromTime(SetUTCHours(28800000,-8670)) );
     91 
     92     addNewTestCase( 946684800000, 1234567, void 0, void 0, void 0,
     93                     "TDATE = new Date(946684800000);(TDATE).setUTCHours(1234567);TDATE",
     94                     UTCDateFromTime(SetUTCHours(946684800000,1234567)),
     95                     LocalDateFromTime(SetUTCHours(946684800000,1234567)) );
     96 
     97     addNewTestCase( -2208988800000, 59, 999, void 0, void 0,
     98                     "TDATE = new Date(-2208988800000);(TDATE).setUTCHours(59,999);TDATE",
     99                     UTCDateFromTime(SetUTCHours(-2208988800000,59,999)),
    100                     LocalDateFromTime(SetUTCHours(-2208988800000,59,999)) );
    101 /*
    102     addNewTestCase( "TDATE = new Date(-2208988800000);(TDATE).setUTCMilliseconds(123456789);TDATE",
    103                     UTCDateFromTime(SetUTCMilliseconds(-2208988800000,123456789)),
    104                     LocalDateFromTime(SetUTCMilliseconds(-2208988800000,123456789)) );
    105 
    106     addNewTestCase( "TDATE = new Date(-2208988800000);(TDATE).setUTCMilliseconds(123456);TDATE",
    107                     UTCDateFromTime(SetUTCMilliseconds(-2208988800000,123456)),
    108                     LocalDateFromTime(SetUTCMilliseconds(-2208988800000,123456)) );
    109 
    110     addNewTestCase( "TDATE = new Date(-2208988800000);(TDATE).setUTCMilliseconds(-123456);TDATE",
    111                     UTCDateFromTime(SetUTCMilliseconds(-2208988800000,-123456)),
    112                     LocalDateFromTime(SetUTCMilliseconds(-2208988800000,-123456)) );
    113 
    114     addNewTestCase( "TDATE = new Date(0);(TDATE).setUTCMilliseconds(-999);TDATE",
    115                     UTCDateFromTime(SetUTCMilliseconds(0,-999)),
    116                     LocalDateFromTime(SetUTCMilliseconds(0,-999)) );
    117 */
    118 
    119 }
    120 function addNewTestCase( time, hours, min, sec, ms, DateString, UTCDate, LocalDate) {
    121 
    122     DateCase = new Date(time);
    123     if ( min == void 0 ) {
    124         DateCase.setUTCHours( hours );
    125     } else {
    126         if ( sec == void 0 ) {
    127             DateCase.setUTCHours( hours, min );
    128         } else {
    129             if ( ms == void 0 ) {
    130                 DateCase.setUTCHours( hours, min, sec );
    131             } else {
    132                 DateCase.setUTCHours( hours, min, sec, ms );
    133             }
    134         }
    135     }
    136 
    137     var item = testcases.length;
    138 
    139 //    fixed_year = ( ExpectDate.year >=1900 || ExpectDate.year < 2000 ) ? ExpectDate.year - 1900 : ExpectDate.year;
    140 
    141     testcases[item++] = new TestCase( SECTION, DateString+".getTime()",             UTCDate.value,       DateCase.getTime() );
    142     testcases[item++] = new TestCase( SECTION, DateString+".valueOf()",             UTCDate.value,       DateCase.valueOf() );
    143 
    144     testcases[item++] = new TestCase( SECTION, DateString+".getUTCFullYear()",      UTCDate.year,    DateCase.getUTCFullYear() );
    145     testcases[item++] = new TestCase( SECTION, DateString+".getUTCMonth()",         UTCDate.month,  DateCase.getUTCMonth() );
    146     testcases[item++] = new TestCase( SECTION, DateString+".getUTCDate()",          UTCDate.date,   DateCase.getUTCDate() );
    147     testcases[item++] = new TestCase( SECTION, DateString+".getUTCDay()",           UTCDate.day,    DateCase.getUTCDay() );
    148     testcases[item++] = new TestCase( SECTION, DateString+".getUTCHours()",         UTCDate.hours,  DateCase.getUTCHours() );
    149     testcases[item++] = new TestCase( SECTION, DateString+".getUTCMinutes()",       UTCDate.minutes,DateCase.getUTCMinutes() );
    150     testcases[item++] = new TestCase( SECTION, DateString+".getUTCSeconds()",       UTCDate.seconds,DateCase.getUTCSeconds() );
    151     testcases[item++] = new TestCase( SECTION, DateString+".getUTCMilliseconds()",  UTCDate.ms,     DateCase.getUTCMilliseconds() );
    152 
    153     testcases[item++] = new TestCase( SECTION, DateString+".getFullYear()",         LocalDate.year,       DateCase.getFullYear() );
    154     testcases[item++] = new TestCase( SECTION, DateString+".getMonth()",            LocalDate.month,      DateCase.getMonth() );
    155     testcases[item++] = new TestCase( SECTION, DateString+".getDate()",             LocalDate.date,       DateCase.getDate() );
    156     testcases[item++] = new TestCase( SECTION, DateString+".getDay()",              LocalDate.day,        DateCase.getDay() );
    157     testcases[item++] = new TestCase( SECTION, DateString+".getHours()",            LocalDate.hours,      DateCase.getHours() );
    158     testcases[item++] = new TestCase( SECTION, DateString+".getMinutes()",          LocalDate.minutes,    DateCase.getMinutes() );
    159     testcases[item++] = new TestCase( SECTION, DateString+".getSeconds()",          LocalDate.seconds,    DateCase.getSeconds() );
    160     testcases[item++] = new TestCase( SECTION, DateString+".getMilliseconds()",     LocalDate.ms,         DateCase.getMilliseconds() );
    161 
    162     DateCase.toString = Object.prototype.toString;
    163 
    164     testcases[item++] = new TestCase( SECTION,
    165                                       DateString+".toString=Object.prototype.toString;"+DateString+".toString()",
    166                                       "[object Date]",
    167                                       DateCase.toString() );
    168 }
    169 function MyDate() {
    170     this.year = 0;
    171     this.month = 0;
    172     this.date = 0;
    173     this.hours = 0;
    174     this.minutes = 0;
    175     this.seconds = 0;
    176     this.ms = 0;
    177 }
    178 function LocalDateFromTime(t) {
    179     t = LocalTime(t);
    180     return ( MyDateFromTime(t) );
    181 }
    182 function UTCDateFromTime(t) {
    183  return ( MyDateFromTime(t) );
    184 }
    185 function MyDateFromTime( t ) {
    186     var d = new MyDate();
    187     d.year = YearFromTime(t);
    188     d.month = MonthFromTime(t);
    189     d.date = DateFromTime(t);
    190     d.hours = HourFromTime(t);
    191     d.minutes = MinFromTime(t);
    192     d.seconds = SecFromTime(t);
    193     d.ms = msFromTime(t);
    194 
    195     d.time = MakeTime( d.hours, d.minutes, d.seconds, d.ms );
    196     d.value = TimeClip( MakeDate( MakeDay( d.year, d.month, d.date ), d.time ) );
    197     d.day = WeekDay( d.value );
    198 
    199     return (d);
    200 }
    201 function SetUTCHours( t, hour, min, sec, ms ) {
    202     var TIME = t;
    203     var HOUR = Number(hour);
    204     var MIN =  ( min == void 0) ? MinFromTime(TIME) : Number(min);
    205     var SEC  = ( sec == void 0) ? SecFromTime(TIME) : Number(sec);
    206     var MS   = ( ms == void 0 ) ? msFromTime(TIME)  : Number(ms);
    207     var RESULT6 = MakeTime( HOUR,
    208                             MIN,
    209                             SEC,
    210                             MS );
    211     return ( TimeClip(MakeDate(Day(TIME), RESULT6)) );
    212 }
    213