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