1// Copyright JS Foundation and other contributors, http://js.foundation 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15var ms = 1; 16var sec = 1000 * ms; 17var min = 60 * sec; 18var hour = 60 * min; 19var day = 24 * hour; /* 86400000 */ 20var d = new Date(); 21 22/* 15.9.5.27 Date.prototype.setTime (time) */ 23assert (d.setTime(0) == 0); 24d.setTime(0); 25assert (d.setTime(day) == day); 26assert (d.getDate() == 2); 27 28/* 15.9.5.28 Date.prototype.setMilliseconds (ms) */ 29d.setTime(0); 30assert (d.setMilliseconds(1) == ms); 31assert (d.getMilliseconds() == 1); 32 33/* 15.9.5.29 Date.prototype.setUTCMilliseconds (ms) */ 34d.setTime(0); 35assert (d.setUTCMilliseconds(1) == ms); 36assert (d.getUTCMilliseconds() == 1); 37 38/* 15.9.5.30 Date.prototype.setSeconds (sec [, ms ] ) */ 39d.setTime(0); 40assert (d.setSeconds(1) == sec); 41assert (d.getSeconds() == 1); 42d.setTime(0); 43assert (d.setSeconds(1, 1) == sec + ms); 44assert (d.getSeconds() == 1); 45assert (d.getMilliseconds() == 1); 46 47/* 15.9.5.31 Date.prototype.setUTCSeconds (sec [, ms ] ) */ 48d.setTime(0); 49assert (d.setUTCSeconds(1) == sec); 50assert (d.getUTCSeconds() == 1); 51d.setTime(0); 52assert (d.setUTCSeconds(1, 1) == sec + ms); 53assert (d.getUTCSeconds() == 1); 54assert (d.getUTCMilliseconds() == 1); 55 56/* 15.9.5.32 Date.prototype.setMinutes (min [, sec [, ms ] ] ) */ 57d.setTime(0); 58assert (d.setMinutes(1) == min); 59assert (d.getMinutes() == 1); 60d.setTime(0); 61assert (d.setMinutes(1, 1) == min + sec); 62assert (d.getMinutes() == 1); 63assert (d.getSeconds() == 1); 64d.setTime(0); 65assert (d.setMinutes(1, 1, 1) == min + sec + ms); 66assert (d.getMinutes() == 1); 67assert (d.getSeconds() == 1); 68assert (d.getMilliseconds() == 1); 69 70/* 15.9.5.33 Date.prototype.setUTCMinutes (min [, sec [, ms ] ] ) */ 71d.setTime(0); 72assert (d.setUTCMinutes(1) == min); 73assert (d.getUTCMinutes() == 1); 74d.setTime(0); 75assert (d.setUTCMinutes(1, 1) == min + sec); 76assert (d.getUTCMinutes() == 1); 77assert (d.getUTCSeconds() == 1); 78d.setTime(0); 79assert (d.setUTCMinutes(1, 1, 1) == min + sec + ms); 80assert (d.getUTCMinutes() == 1); 81assert (d.getUTCSeconds() == 1); 82assert (d.getUTCMilliseconds() == 1); 83 84/* 15.9.5.34 Date.prototype.setHours (hour [, min [, sec [, ms ] ] ] ) */ 85d.setTime(0); 86assert (d.setHours(1) == hour + d.getTimezoneOffset() * 60000); 87assert (d.getHours() == 1); 88d.setTime(0); 89assert (d.setHours(1, 1) == hour + min + d.getTimezoneOffset() * 60000); 90assert (d.getHours() == 1); 91assert (d.getMinutes() == 1); 92d.setTime(0); 93assert (d.setHours(1, 1, 1) == hour + min + sec + d.getTimezoneOffset() * 60000); 94assert (d.getHours() == 1); 95assert (d.getMinutes() == 1); 96assert (d.getSeconds() == 1); 97d.setTime(0); 98assert (d.setHours(1, 1, 1, 1) == hour + min + sec + ms + d.getTimezoneOffset() * 60000); 99assert (d.getHours() == 1); 100assert (d.getMinutes() == 1); 101assert (d.getSeconds() == 1); 102assert (d.getMilliseconds() == 1); 103 104/* 15.9.5.35 Date.prototype.setUTCHours (hour [, min [, sec [, ms ] ] ] ) */ 105d.setTime(0); 106assert (d.setUTCHours(1) == hour); 107assert (d.getUTCHours() == 1); 108d.setTime(0); 109assert (d.setUTCHours(1, 1) == hour + min); 110assert (d.getUTCHours() == 1); 111assert (d.getUTCMinutes() == 1); 112d.setTime(0); 113assert (d.setUTCHours(1, 1, 1) == hour + min + sec); 114assert (d.getUTCHours() == 1); 115assert (d.getUTCMinutes() == 1); 116assert (d.getUTCSeconds() == 1); 117d.setTime(0); 118assert (d.setUTCHours(1, 1, 1, 1) == hour + min + sec + ms); 119assert (d.getUTCHours() == 1); 120assert (d.getUTCMinutes() == 1); 121assert (d.getUTCSeconds() == 1); 122assert (d.getUTCMilliseconds() == 1); 123 124/* 15.9.5.36 Date.prototype.setDate (date) */ 125d.setTime(0); 126assert (d.setDate(0) == -day); 127assert (d.getDate() == 31); 128d.setTime(0); 129assert (d.setDate(1) == 0); 130assert (d.getDate() == 1); 131 132/* 15.9.5.37 Date.prototype.setUTCDate (date) */ 133d.setTime(0); 134assert (d.setUTCDate(0) == -day); 135assert (d.getUTCDate() == 31); 136d.setTime(0); 137assert (d.setUTCDate(1) == 0); 138assert (d.getUTCDate() == 1); 139 140/* 15.9.5.38 Date.prototype.setMonth (month [, date ] ) */ 141d.setTime(0); 142assert (d.setMonth(0) == 0); 143assert (d.getMonth() == 0); 144d.setTime(0); 145assert (d.setMonth(0, 0) == -day); 146assert (d.getMonth() == 11); 147assert (d.getDate() == 31); 148d.setTime(0); 149assert (d.setMonth(1) == 31 * day); 150assert (d.getMonth() == 1); 151d.setTime(0); 152assert (d.setMonth(12) == 365 * day); 153assert (d.getMonth() == 0); 154d.setTime(0); 155assert (d.setMonth(13) == (365 + 31) * day); 156assert (d.getMonth() == 1); 157 158/* 15.9.5.39 Date.prototype.setUTCMonth (month [, date ] ) */ 159d.setTime(0); 160assert (d.setUTCMonth(0) == 0); 161assert (d.getUTCMonth() == 0); 162d.setTime(0); 163assert (d.setUTCMonth(0, 0) == -day); 164assert (d.getUTCMonth() == 11); 165assert (d.getUTCDate() == 31); 166d.setTime(0); 167assert (d.setUTCMonth(1) == 31 * day); 168assert (d.getUTCMonth() == 1); 169d.setTime(0); 170assert (d.setUTCMonth(12) == 365 * day); 171assert (d.getUTCMonth() == 0); 172d.setTime(0); 173assert (d.setUTCMonth(13) == (365 + 31) * day); 174assert (d.getUTCMonth() == 1); 175 176/* 15.9.5.40 Date.prototype.setFullYear (year [, month [, date ] ] ) */ 177d.setTime(0); 178assert (d.setFullYear(0) == -62167219200000); 179assert (d.getFullYear() == 0); 180d.setTime(0); 181assert (d.setFullYear(0, 0) == -62167219200000); 182assert (d.getFullYear() == 0); 183assert (d.getMonth() == 0); 184d.setTime(0); 185assert (d.setFullYear(0, 0, 0) == -62167219200000 - day); 186assert (d.getFullYear() == -1); 187assert (d.getMonth() == 11); 188assert (d.getDate() == 31); 189d.setTime(0); 190assert (d.setFullYear(1970) == 0); 191assert (d.getFullYear() == 1970); 192 193/* 15.9.5.41 Date.prototype.setUTCFullYear (year [, month [, date ] ] ) */ 194d.setTime(0); 195assert (d.setUTCFullYear(0) == -62167219200000); 196assert (d.getUTCFullYear() == 0); 197d.setTime(0); 198assert (d.setUTCFullYear(0, 0) == -62167219200000); 199assert (d.getUTCFullYear() == 0); 200assert (d.getUTCMonth() == 0); 201d.setTime(0); 202assert (d.setUTCFullYear(0, 0, 0) == -62167219200000 - day); 203assert (d.getUTCFullYear() == -1); 204assert (d.getUTCMonth() == 11); 205assert (d.getUTCDate() == 31); 206d.setTime(0); 207assert (d.setUTCFullYear(1970) == 0); 208assert (d.getUTCFullYear() == 1970); 209 210/* ECMA262 v11 20.4.1.2 Day Number and Time within Day 211 msPerDay = 86400000 212 TimeWithinDay(t) = t modulo msPerDay 213 214 ECMA262 v11 5.2.5 Mathematical Operations 215 The notation “x modulo y” (y must be finite and nonzero) computes a value k of the same sign as y (or zero). 216 217 Consequently TimeWithinDay(t) >= 0. It can be tested properly with dates close to 1970. 218*/ 219d = new Date("1969-12-01T01:00:00.000Z"); 220d.setFullYear(1968); 221assert (d.toISOString() == "1968-12-01T01:00:00.000Z"); 222 223d = new Date("1970-01-31T01:00:00.000Z"); 224d.setFullYear(1971); 225assert (d.toISOString() == "1971-01-31T01:00:00.000Z"); 226 227/* Without argument */ 228d = new Date(); 229assert (isNaN (d.setTime())); 230assert (isNaN (d.setMilliseconds())); 231assert (isNaN (d.setUTCMilliseconds())); 232assert (isNaN (d.setSeconds())); 233assert (isNaN (d.setUTCSeconds())); 234assert (isNaN (d.setMinutes())); 235assert (isNaN (d.setUTCMinutes())); 236assert (isNaN (d.setHours())); 237assert (isNaN (d.setUTCHours())); 238assert (isNaN (d.setDate())); 239assert (isNaN (d.getUTCDate())); 240assert (isNaN (d.setMonth())); 241assert (isNaN (d.setUTCMonth())); 242assert (isNaN (d.setFullYear())); 243assert (isNaN (d.setUTCFullYear())); 244