Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | |
michael@0 | 7 | /* |
michael@0 | 8 | * Date functions used by tests in Date suite |
michael@0 | 9 | * |
michael@0 | 10 | */ |
michael@0 | 11 | var msPerDay = 86400000; |
michael@0 | 12 | var HoursPerDay = 24; |
michael@0 | 13 | var MinutesPerHour = 60; |
michael@0 | 14 | var SecondsPerMinute = 60; |
michael@0 | 15 | var msPerSecond = 1000; |
michael@0 | 16 | var msPerMinute = 60000; // msPerSecond * SecondsPerMinute |
michael@0 | 17 | var msPerHour = 3600000; // msPerMinute * MinutesPerHour |
michael@0 | 18 | var TZ_DIFF = getTimeZoneDiff(); // offset of tester's timezone from UTC |
michael@0 | 19 | var TZ_ADJUST = TZ_DIFF * msPerHour; |
michael@0 | 20 | var TZ_PST = -8; // offset of Pacific Standard Time from UTC |
michael@0 | 21 | var PST_DIFF = TZ_DIFF - TZ_PST; // offset of tester's timezone from PST |
michael@0 | 22 | var TIME_1970 = 0; |
michael@0 | 23 | var TIME_2000 = 946684800000; |
michael@0 | 24 | var TIME_1900 = -2208988800000; |
michael@0 | 25 | var UTC_29_FEB_2000 = TIME_2000 + 31*msPerDay + 28*msPerDay; |
michael@0 | 26 | var UTC_1_JAN_2005 = TIME_2000 + TimeInYear(2000) + TimeInYear(2001) + |
michael@0 | 27 | TimeInYear(2002) + TimeInYear(2003) + TimeInYear(2004); |
michael@0 | 28 | var now = new Date(); |
michael@0 | 29 | var TIME_NOW = now.valueOf(); //valueOf() is to accurate to the millisecond |
michael@0 | 30 | //Date.parse() is accurate only to the second |
michael@0 | 31 | |
michael@0 | 32 | /* |
michael@0 | 33 | * Originally, the test suite used a hard-coded value TZ_DIFF = -8. |
michael@0 | 34 | * But that was only valid for testers in the Pacific Standard Time Zone! |
michael@0 | 35 | * We calculate the proper number dynamically for any tester. We just |
michael@0 | 36 | * have to be careful not to use a date subject to Daylight Savings Time... |
michael@0 | 37 | */ |
michael@0 | 38 | function getTimeZoneDiff() |
michael@0 | 39 | { |
michael@0 | 40 | return -((new Date(2000, 1, 1)).getTimezoneOffset())/60; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | |
michael@0 | 44 | /* |
michael@0 | 45 | * Date test "ResultArrays" are hard-coded for Pacific Standard Time. |
michael@0 | 46 | * We must adjust them for the tester's own timezone - |
michael@0 | 47 | */ |
michael@0 | 48 | function adjustResultArray(ResultArray, msMode) |
michael@0 | 49 | { |
michael@0 | 50 | // If the tester's system clock is in PST, no need to continue - |
michael@0 | 51 | if (!PST_DIFF) {return;} |
michael@0 | 52 | |
michael@0 | 53 | /* The date testcases instantiate Date objects in two different ways: |
michael@0 | 54 | * |
michael@0 | 55 | * millisecond mode: e.g. dt = new Date(10000000); |
michael@0 | 56 | * year-month-day mode: dt = new Date(2000, 5, 1, ...); |
michael@0 | 57 | * |
michael@0 | 58 | * In the first case, the date is measured from Time 0 in Greenwich (i.e. UTC). |
michael@0 | 59 | * In the second case, it is measured with reference to the tester's local timezone. |
michael@0 | 60 | * |
michael@0 | 61 | * In the first case we must correct those values expected for local measurements, |
michael@0 | 62 | * like dt.getHours() etc. No correction is necessary for dt.getUTCHours() etc. |
michael@0 | 63 | * |
michael@0 | 64 | * In the second case, it is exactly the other way around - |
michael@0 | 65 | */ |
michael@0 | 66 | if (msMode) |
michael@0 | 67 | { |
michael@0 | 68 | // The hard-coded UTC milliseconds from Time 0 derives from a UTC date. |
michael@0 | 69 | // Shift to the right by the offset between UTC and the tester. |
michael@0 | 70 | var t = ResultArray[TIME] + TZ_DIFF*msPerHour; |
michael@0 | 71 | |
michael@0 | 72 | // Use our date arithmetic functions to determine the local hour, day, etc. |
michael@0 | 73 | ResultArray[HOURS] = HourFromTime(t); |
michael@0 | 74 | ResultArray[DAY] = WeekDay(t); |
michael@0 | 75 | ResultArray[DATE] = DateFromTime(t); |
michael@0 | 76 | ResultArray[MONTH] = MonthFromTime(t); |
michael@0 | 77 | ResultArray[YEAR] = YearFromTime(t); |
michael@0 | 78 | } |
michael@0 | 79 | else |
michael@0 | 80 | { |
michael@0 | 81 | // The hard-coded UTC milliseconds from Time 0 derives from a PST date. |
michael@0 | 82 | // Shift to the left by the offset between PST and the tester. |
michael@0 | 83 | var t = ResultArray[TIME] - PST_DIFF*msPerHour; |
michael@0 | 84 | |
michael@0 | 85 | // Use our date arithmetic functions to determine the UTC hour, day, etc. |
michael@0 | 86 | ResultArray[TIME] = t; |
michael@0 | 87 | ResultArray[UTC_HOURS] = HourFromTime(t); |
michael@0 | 88 | ResultArray[UTC_DAY] = WeekDay(t); |
michael@0 | 89 | ResultArray[UTC_DATE] = DateFromTime(t); |
michael@0 | 90 | ResultArray[UTC_MONTH] = MonthFromTime(t); |
michael@0 | 91 | ResultArray[UTC_YEAR] = YearFromTime(t); |
michael@0 | 92 | } |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | |
michael@0 | 96 | function Day( t ) { |
michael@0 | 97 | return ( Math.floor(t/msPerDay ) ); |
michael@0 | 98 | } |
michael@0 | 99 | function DaysInYear( y ) { |
michael@0 | 100 | if ( y % 4 != 0 ) { |
michael@0 | 101 | return 365; |
michael@0 | 102 | } |
michael@0 | 103 | if ( (y % 4 == 0) && (y % 100 != 0) ) { |
michael@0 | 104 | return 366; |
michael@0 | 105 | } |
michael@0 | 106 | if ( (y % 100 == 0) && (y % 400 != 0) ) { |
michael@0 | 107 | return 365; |
michael@0 | 108 | } |
michael@0 | 109 | if ( (y % 400 == 0) ){ |
michael@0 | 110 | return 366; |
michael@0 | 111 | } else { |
michael@0 | 112 | return "ERROR: DaysInYear(" + y + ") case not covered"; |
michael@0 | 113 | } |
michael@0 | 114 | } |
michael@0 | 115 | function TimeInYear( y ) { |
michael@0 | 116 | return ( DaysInYear(y) * msPerDay ); |
michael@0 | 117 | } |
michael@0 | 118 | function DayNumber( t ) { |
michael@0 | 119 | return ( Math.floor( t / msPerDay ) ); |
michael@0 | 120 | } |
michael@0 | 121 | function TimeWithinDay( t ) { |
michael@0 | 122 | |
michael@0 | 123 | var r = t % msPerDay; |
michael@0 | 124 | |
michael@0 | 125 | if (r < 0) |
michael@0 | 126 | { |
michael@0 | 127 | r += msPerDay; |
michael@0 | 128 | } |
michael@0 | 129 | return r; |
michael@0 | 130 | |
michael@0 | 131 | } |
michael@0 | 132 | function YearNumber( t ) { |
michael@0 | 133 | } |
michael@0 | 134 | function TimeFromYear( y ) { |
michael@0 | 135 | return ( msPerDay * DayFromYear(y) ); |
michael@0 | 136 | } |
michael@0 | 137 | function DayFromYear( y ) { |
michael@0 | 138 | return ( 365*(y-1970) + |
michael@0 | 139 | Math.floor((y-1969)/4) - |
michael@0 | 140 | Math.floor((y-1901)/100) + |
michael@0 | 141 | Math.floor((y-1601)/400) ); |
michael@0 | 142 | } |
michael@0 | 143 | function InLeapYear( t ) { |
michael@0 | 144 | if ( DaysInYear(YearFromTime(t)) == 365 ) { |
michael@0 | 145 | return 0; |
michael@0 | 146 | } |
michael@0 | 147 | if ( DaysInYear(YearFromTime(t)) == 366 ) { |
michael@0 | 148 | return 1; |
michael@0 | 149 | } else { |
michael@0 | 150 | return "ERROR: InLeapYear("+ t + ") case not covered"; |
michael@0 | 151 | } |
michael@0 | 152 | } |
michael@0 | 153 | function YearFromTime( t ) { |
michael@0 | 154 | t = Number( t ); |
michael@0 | 155 | var sign = ( t < 0 ) ? -1 : 1; |
michael@0 | 156 | var year = ( sign < 0 ) ? 1969 : 1970; |
michael@0 | 157 | for ( var timeToTimeZero = t; ; ) { |
michael@0 | 158 | // subtract the current year's time from the time that's left. |
michael@0 | 159 | timeToTimeZero -= sign * TimeInYear(year) |
michael@0 | 160 | |
michael@0 | 161 | // if there's less than the current year's worth of time left, then break. |
michael@0 | 162 | if ( sign < 0 ) { |
michael@0 | 163 | if ( sign * timeToTimeZero <= 0 ) { |
michael@0 | 164 | break; |
michael@0 | 165 | } else { |
michael@0 | 166 | year += sign; |
michael@0 | 167 | } |
michael@0 | 168 | } else { |
michael@0 | 169 | if ( sign * timeToTimeZero < 0 ) { |
michael@0 | 170 | break; |
michael@0 | 171 | } else { |
michael@0 | 172 | year += sign; |
michael@0 | 173 | } |
michael@0 | 174 | } |
michael@0 | 175 | } |
michael@0 | 176 | return ( year ); |
michael@0 | 177 | } |
michael@0 | 178 | function MonthFromTime( t ) { |
michael@0 | 179 | // i know i could use switch but i'd rather not until it's part of ECMA |
michael@0 | 180 | var day = DayWithinYear( t ); |
michael@0 | 181 | var leap = InLeapYear(t); |
michael@0 | 182 | |
michael@0 | 183 | if ( (0 <= day) && (day < 31) ) { |
michael@0 | 184 | return 0; |
michael@0 | 185 | } |
michael@0 | 186 | if ( (31 <= day) && (day < (59+leap)) ) { |
michael@0 | 187 | return 1; |
michael@0 | 188 | } |
michael@0 | 189 | if ( ((59+leap) <= day) && (day < (90+leap)) ) { |
michael@0 | 190 | return 2; |
michael@0 | 191 | } |
michael@0 | 192 | if ( ((90+leap) <= day) && (day < (120+leap)) ) { |
michael@0 | 193 | return 3; |
michael@0 | 194 | } |
michael@0 | 195 | if ( ((120+leap) <= day) && (day < (151+leap)) ) { |
michael@0 | 196 | return 4; |
michael@0 | 197 | } |
michael@0 | 198 | if ( ((151+leap) <= day) && (day < (181+leap)) ) { |
michael@0 | 199 | return 5; |
michael@0 | 200 | } |
michael@0 | 201 | if ( ((181+leap) <= day) && (day < (212+leap)) ) { |
michael@0 | 202 | return 6; |
michael@0 | 203 | } |
michael@0 | 204 | if ( ((212+leap) <= day) && (day < (243+leap)) ) { |
michael@0 | 205 | return 7; |
michael@0 | 206 | } |
michael@0 | 207 | if ( ((243+leap) <= day) && (day < (273+leap)) ) { |
michael@0 | 208 | return 8; |
michael@0 | 209 | } |
michael@0 | 210 | if ( ((273+leap) <= day) && (day < (304+leap)) ) { |
michael@0 | 211 | return 9; |
michael@0 | 212 | } |
michael@0 | 213 | if ( ((304+leap) <= day) && (day < (334+leap)) ) { |
michael@0 | 214 | return 10; |
michael@0 | 215 | } |
michael@0 | 216 | if ( ((334+leap) <= day) && (day < (365+leap)) ) { |
michael@0 | 217 | return 11; |
michael@0 | 218 | } else { |
michael@0 | 219 | return "ERROR: MonthFromTime("+t+") not known"; |
michael@0 | 220 | } |
michael@0 | 221 | } |
michael@0 | 222 | function DayWithinYear( t ) { |
michael@0 | 223 | return( Day(t) - DayFromYear(YearFromTime(t))); |
michael@0 | 224 | } |
michael@0 | 225 | function DateFromTime( t ) { |
michael@0 | 226 | var day = DayWithinYear(t); |
michael@0 | 227 | var month = MonthFromTime(t); |
michael@0 | 228 | |
michael@0 | 229 | if ( month == 0 ) { |
michael@0 | 230 | return ( day + 1 ); |
michael@0 | 231 | } |
michael@0 | 232 | if ( month == 1 ) { |
michael@0 | 233 | return ( day - 30 ); |
michael@0 | 234 | } |
michael@0 | 235 | if ( month == 2 ) { |
michael@0 | 236 | return ( day - 58 - InLeapYear(t) ); |
michael@0 | 237 | } |
michael@0 | 238 | if ( month == 3 ) { |
michael@0 | 239 | return ( day - 89 - InLeapYear(t)); |
michael@0 | 240 | } |
michael@0 | 241 | if ( month == 4 ) { |
michael@0 | 242 | return ( day - 119 - InLeapYear(t)); |
michael@0 | 243 | } |
michael@0 | 244 | if ( month == 5 ) { |
michael@0 | 245 | return ( day - 150- InLeapYear(t)); |
michael@0 | 246 | } |
michael@0 | 247 | if ( month == 6 ) { |
michael@0 | 248 | return ( day - 180- InLeapYear(t)); |
michael@0 | 249 | } |
michael@0 | 250 | if ( month == 7 ) { |
michael@0 | 251 | return ( day - 211- InLeapYear(t)); |
michael@0 | 252 | } |
michael@0 | 253 | if ( month == 8 ) { |
michael@0 | 254 | return ( day - 242- InLeapYear(t)); |
michael@0 | 255 | } |
michael@0 | 256 | if ( month == 9 ) { |
michael@0 | 257 | return ( day - 272- InLeapYear(t)); |
michael@0 | 258 | } |
michael@0 | 259 | if ( month == 10 ) { |
michael@0 | 260 | return ( day - 303- InLeapYear(t)); |
michael@0 | 261 | } |
michael@0 | 262 | if ( month == 11 ) { |
michael@0 | 263 | return ( day - 333- InLeapYear(t)); |
michael@0 | 264 | } |
michael@0 | 265 | |
michael@0 | 266 | return ("ERROR: DateFromTime("+t+") not known" ); |
michael@0 | 267 | } |
michael@0 | 268 | function WeekDay( t ) { |
michael@0 | 269 | var weekday = (Day(t)+4) % 7; |
michael@0 | 270 | return( weekday < 0 ? 7 + weekday : weekday ); |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | // missing daylight savings time adjustment |
michael@0 | 274 | |
michael@0 | 275 | function HourFromTime( t ) { |
michael@0 | 276 | var h = Math.floor( t / msPerHour ) % HoursPerDay; |
michael@0 | 277 | return ( (h<0) ? HoursPerDay + h : h ); |
michael@0 | 278 | } |
michael@0 | 279 | function MinFromTime( t ) { |
michael@0 | 280 | var min = Math.floor( t / msPerMinute ) % MinutesPerHour; |
michael@0 | 281 | return( ( min < 0 ) ? MinutesPerHour + min : min ); |
michael@0 | 282 | } |
michael@0 | 283 | function SecFromTime( t ) { |
michael@0 | 284 | var sec = Math.floor( t / msPerSecond ) % SecondsPerMinute; |
michael@0 | 285 | return ( (sec < 0 ) ? SecondsPerMinute + sec : sec ); |
michael@0 | 286 | } |
michael@0 | 287 | function msFromTime( t ) { |
michael@0 | 288 | var ms = t % msPerSecond; |
michael@0 | 289 | return ( (ms < 0 ) ? msPerSecond + ms : ms ); |
michael@0 | 290 | } |
michael@0 | 291 | function LocalTZA() { |
michael@0 | 292 | return ( TZ_DIFF * msPerHour ); |
michael@0 | 293 | } |
michael@0 | 294 | function UTC( t ) { |
michael@0 | 295 | return ( t - LocalTZA() - DaylightSavingTA(t - LocalTZA()) ); |
michael@0 | 296 | } |
michael@0 | 297 | |
michael@0 | 298 | function DaylightSavingTA( t ) { |
michael@0 | 299 | t = t - LocalTZA(); |
michael@0 | 300 | |
michael@0 | 301 | var dst_start = GetDSTStart(t); |
michael@0 | 302 | var dst_end = GetDSTEnd(t); |
michael@0 | 303 | |
michael@0 | 304 | if ( t >= dst_start && t < dst_end ) |
michael@0 | 305 | return msPerHour; |
michael@0 | 306 | |
michael@0 | 307 | return 0; |
michael@0 | 308 | } |
michael@0 | 309 | |
michael@0 | 310 | function GetFirstSundayInMonth( t, m ) { |
michael@0 | 311 | var year = YearFromTime(t); |
michael@0 | 312 | var leap = InLeapYear(t); |
michael@0 | 313 | |
michael@0 | 314 | // month m 0..11 |
michael@0 | 315 | // april == 3 |
michael@0 | 316 | // march == 2 |
michael@0 | 317 | |
michael@0 | 318 | // set time to first day of month m |
michael@0 | 319 | var time = TimeFromYear(year); |
michael@0 | 320 | for (var i = 0; i < m; ++i) |
michael@0 | 321 | { |
michael@0 | 322 | time += TimeInMonth(i, leap); |
michael@0 | 323 | } |
michael@0 | 324 | |
michael@0 | 325 | for ( var first_sunday = time; WeekDay(first_sunday) > 0; |
michael@0 | 326 | first_sunday += msPerDay ) |
michael@0 | 327 | { |
michael@0 | 328 | ; |
michael@0 | 329 | } |
michael@0 | 330 | |
michael@0 | 331 | return first_sunday; |
michael@0 | 332 | } |
michael@0 | 333 | |
michael@0 | 334 | function GetLastSundayInMonth( t, m ) { |
michael@0 | 335 | var year = YearFromTime(t); |
michael@0 | 336 | var leap = InLeapYear(t); |
michael@0 | 337 | |
michael@0 | 338 | // month m 0..11 |
michael@0 | 339 | // april == 3 |
michael@0 | 340 | // march == 2 |
michael@0 | 341 | |
michael@0 | 342 | // first day of following month |
michael@0 | 343 | var time = TimeFromYear(year); |
michael@0 | 344 | for (var i = 0; i <= m; ++i) |
michael@0 | 345 | { |
michael@0 | 346 | time += TimeInMonth(i, leap); |
michael@0 | 347 | } |
michael@0 | 348 | // prev day == last day of month |
michael@0 | 349 | time -= msPerDay; |
michael@0 | 350 | |
michael@0 | 351 | for ( var last_sunday = time; WeekDay(last_sunday) > 0; |
michael@0 | 352 | last_sunday -= msPerDay ) |
michael@0 | 353 | { |
michael@0 | 354 | ; |
michael@0 | 355 | } |
michael@0 | 356 | return last_sunday; |
michael@0 | 357 | } |
michael@0 | 358 | |
michael@0 | 359 | /* |
michael@0 | 360 | 15.9.1.9 Daylight Saving Time Adjustment |
michael@0 | 361 | |
michael@0 | 362 | The implementation of ECMAScript should not try to determine whether |
michael@0 | 363 | the exact time was subject to daylight saving time, but just whether |
michael@0 | 364 | daylight saving time would have been in effect if the current |
michael@0 | 365 | daylight saving time algorithm had been used at the time. This avoids |
michael@0 | 366 | complications such as taking into account the years that the locale |
michael@0 | 367 | observed daylight saving time year round. |
michael@0 | 368 | */ |
michael@0 | 369 | |
michael@0 | 370 | /* |
michael@0 | 371 | US DST algorithm |
michael@0 | 372 | |
michael@0 | 373 | Before 2007, DST starts first Sunday in April at 2 AM and ends last |
michael@0 | 374 | Sunday in October at 2 AM |
michael@0 | 375 | |
michael@0 | 376 | Starting in 2007, DST starts second Sunday in March at 2 AM and ends |
michael@0 | 377 | first Sunday in November at 2 AM |
michael@0 | 378 | |
michael@0 | 379 | Note that different operating systems behave differently. |
michael@0 | 380 | |
michael@0 | 381 | Fully patched Windows XP uses the 2007 algorithm for all dates while |
michael@0 | 382 | fully patched Fedora Core 6 and RHEL 4 Linux use the algorithm in |
michael@0 | 383 | effect at the time. |
michael@0 | 384 | |
michael@0 | 385 | Since pre-2007 DST is a subset of 2007 DST rules, this only affects |
michael@0 | 386 | tests that occur in the period Mar-Apr and Oct-Nov where the two |
michael@0 | 387 | algorithms do not agree. |
michael@0 | 388 | |
michael@0 | 389 | */ |
michael@0 | 390 | |
michael@0 | 391 | function GetDSTStart( t ) |
michael@0 | 392 | { |
michael@0 | 393 | return (GetFirstSundayInMonth(t, 2) + 7*msPerDay + 2*msPerHour - LocalTZA()); |
michael@0 | 394 | } |
michael@0 | 395 | |
michael@0 | 396 | function GetDSTEnd( t ) |
michael@0 | 397 | { |
michael@0 | 398 | return (GetFirstSundayInMonth(t, 10) + 2*msPerHour - LocalTZA()); |
michael@0 | 399 | } |
michael@0 | 400 | |
michael@0 | 401 | function GetOldDSTStart( t ) |
michael@0 | 402 | { |
michael@0 | 403 | return (GetFirstSundayInMonth(t, 3) + 2*msPerHour - LocalTZA()); |
michael@0 | 404 | } |
michael@0 | 405 | |
michael@0 | 406 | function GetOldDSTEnd( t ) |
michael@0 | 407 | { |
michael@0 | 408 | return (GetLastSundayInMonth(t, 9) + 2*msPerHour - LocalTZA()); |
michael@0 | 409 | } |
michael@0 | 410 | |
michael@0 | 411 | function LocalTime( t ) { |
michael@0 | 412 | return ( t + LocalTZA() + DaylightSavingTA(t) ); |
michael@0 | 413 | } |
michael@0 | 414 | function MakeTime( hour, min, sec, ms ) { |
michael@0 | 415 | if ( isNaN( hour ) || isNaN( min ) || isNaN( sec ) || isNaN( ms ) ) { |
michael@0 | 416 | return Number.NaN; |
michael@0 | 417 | } |
michael@0 | 418 | |
michael@0 | 419 | hour = ToInteger(hour); |
michael@0 | 420 | min = ToInteger( min); |
michael@0 | 421 | sec = ToInteger( sec); |
michael@0 | 422 | ms = ToInteger( ms ); |
michael@0 | 423 | |
michael@0 | 424 | return( (hour*msPerHour) + (min*msPerMinute) + |
michael@0 | 425 | (sec*msPerSecond) + ms ); |
michael@0 | 426 | } |
michael@0 | 427 | function MakeDay( year, month, date ) { |
michael@0 | 428 | if ( isNaN(year) || isNaN(month) || isNaN(date) ) { |
michael@0 | 429 | return Number.NaN; |
michael@0 | 430 | } |
michael@0 | 431 | year = ToInteger(year); |
michael@0 | 432 | month = ToInteger(month); |
michael@0 | 433 | date = ToInteger(date ); |
michael@0 | 434 | |
michael@0 | 435 | var sign = ( year < 1970 ) ? -1 : 1; |
michael@0 | 436 | var t = ( year < 1970 ) ? 1 : 0; |
michael@0 | 437 | var y = ( year < 1970 ) ? 1969 : 1970; |
michael@0 | 438 | |
michael@0 | 439 | var result5 = year + Math.floor( month/12 ); |
michael@0 | 440 | var result6 = month % 12; |
michael@0 | 441 | |
michael@0 | 442 | if ( year < 1970 ) { |
michael@0 | 443 | for ( y = 1969; y >= year; y += sign ) { |
michael@0 | 444 | t += sign * TimeInYear(y); |
michael@0 | 445 | } |
michael@0 | 446 | } else { |
michael@0 | 447 | for ( y = 1970 ; y < year; y += sign ) { |
michael@0 | 448 | t += sign * TimeInYear(y); |
michael@0 | 449 | } |
michael@0 | 450 | } |
michael@0 | 451 | |
michael@0 | 452 | var leap = InLeapYear( t ); |
michael@0 | 453 | |
michael@0 | 454 | for ( var m = 0; m < month; m++ ) { |
michael@0 | 455 | t += TimeInMonth( m, leap ); |
michael@0 | 456 | } |
michael@0 | 457 | |
michael@0 | 458 | if ( YearFromTime(t) != result5 ) { |
michael@0 | 459 | return Number.NaN; |
michael@0 | 460 | } |
michael@0 | 461 | if ( MonthFromTime(t) != result6 ) { |
michael@0 | 462 | return Number.NaN; |
michael@0 | 463 | } |
michael@0 | 464 | if ( DateFromTime(t) != 1 ) { |
michael@0 | 465 | return Number.NaN; |
michael@0 | 466 | } |
michael@0 | 467 | |
michael@0 | 468 | return ( (Day(t)) + date - 1 ); |
michael@0 | 469 | } |
michael@0 | 470 | function TimeInMonth( month, leap ) { |
michael@0 | 471 | // september april june november |
michael@0 | 472 | // jan 0 feb 1 mar 2 apr 3 may 4 june 5 jul 6 |
michael@0 | 473 | // aug 7 sep 8 oct 9 nov 10 dec 11 |
michael@0 | 474 | |
michael@0 | 475 | if ( month == 3 || month == 5 || month == 8 || month == 10 ) { |
michael@0 | 476 | return ( 30*msPerDay ); |
michael@0 | 477 | } |
michael@0 | 478 | |
michael@0 | 479 | // all the rest |
michael@0 | 480 | if ( month == 0 || month == 2 || month == 4 || month == 6 || |
michael@0 | 481 | month == 7 || month == 9 || month == 11 ) { |
michael@0 | 482 | return ( 31*msPerDay ); |
michael@0 | 483 | } |
michael@0 | 484 | |
michael@0 | 485 | // save february |
michael@0 | 486 | return ( (leap == 0) ? 28*msPerDay : 29*msPerDay ); |
michael@0 | 487 | } |
michael@0 | 488 | function MakeDate( day, time ) { |
michael@0 | 489 | if ( day == Number.POSITIVE_INFINITY || |
michael@0 | 490 | day == Number.NEGATIVE_INFINITY ) { |
michael@0 | 491 | return Number.NaN; |
michael@0 | 492 | } |
michael@0 | 493 | if ( time == Number.POSITIVE_INFINITY || |
michael@0 | 494 | time == Number.NEGATIVE_INFINITY ) { |
michael@0 | 495 | return Number.NaN; |
michael@0 | 496 | } |
michael@0 | 497 | return ( day * msPerDay ) + time; |
michael@0 | 498 | } |
michael@0 | 499 | function TimeClip( t ) { |
michael@0 | 500 | if ( isNaN( t ) ) { |
michael@0 | 501 | return ( Number.NaN ); |
michael@0 | 502 | } |
michael@0 | 503 | if ( Math.abs( t ) > 8.64e15 ) { |
michael@0 | 504 | return ( Number.NaN ); |
michael@0 | 505 | } |
michael@0 | 506 | |
michael@0 | 507 | return ( ToInteger( t ) ); |
michael@0 | 508 | } |
michael@0 | 509 | function ToInteger( t ) { |
michael@0 | 510 | t = Number( t ); |
michael@0 | 511 | |
michael@0 | 512 | if ( isNaN( t ) ){ |
michael@0 | 513 | return ( Number.NaN ); |
michael@0 | 514 | } |
michael@0 | 515 | if ( t == 0 || t == -0 || |
michael@0 | 516 | t == Number.POSITIVE_INFINITY || t == Number.NEGATIVE_INFINITY ) { |
michael@0 | 517 | return 0; |
michael@0 | 518 | } |
michael@0 | 519 | |
michael@0 | 520 | var sign = ( t < 0 ) ? -1 : 1; |
michael@0 | 521 | |
michael@0 | 522 | return ( sign * Math.floor( Math.abs( t ) ) ); |
michael@0 | 523 | } |
michael@0 | 524 | function Enumerate ( o ) { |
michael@0 | 525 | var p; |
michael@0 | 526 | for ( p in o ) { |
michael@0 | 527 | print( p +": " + o[p] ); |
michael@0 | 528 | } |
michael@0 | 529 | } |
michael@0 | 530 |