From 17c67df3525953da7b85b19916f0483d28cd8ff0 Mon Sep 17 00:00:00 2001 From: David Fugate Date: Wed, 21 Sep 2011 15:40:29 -0700 Subject: [PATCH] Found more of Sputnik's date helper funcs. --- test/harness/Date_library.js | 80 ++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 16 deletions(-) diff --git a/test/harness/Date_library.js b/test/harness/Date_library.js index 84fae35aff..e792b6604f 100644 --- a/test/harness/Date_library.js +++ b/test/harness/Date_library.js @@ -1,6 +1,69 @@ // Copyright 2009 the Sputnik authors. All rights reserved. // This code is governed by the BSD license found in the LICENSE file. +//the following values are normally generated by the sputnik.py driver +var $LocalTZ, + $DST_start_month, + $DST_start_sunday, + $DST_start_hour, + $DST_start_minutes, + $DST_end_month, + $DST_end_sunday, + $DST_end_hour, + $DST_end_minutes; + +(function () { + /** + * Finds the first date, starting from |start|, where |predicate| + * holds. + */ + var findNearestDateBefore = function(start, predicate) { + var current = start; + var month = 1000 * 60 * 60 * 24 * 30; + for (var step = month; step > 0; step = Math.floor(step / 3)) { + if (!predicate(current)) { + while (!predicate(current)) + current = new Date(current.getTime() + step); + current = new Date(current.getTime() - step); + } + } + while (!predicate(current)) { + current = new Date(current.getTime() + 1); + } + return current; + } + + var juneDate = new Date(2000, 6, 20, 0, 0, 0, 0); + var decemberDate = new Date(2000, 12, 20, 0, 0, 0, 0); + var juneOffset = juneDate.getTimezoneOffset(); + var decemberOffset = decemberDate.getTimezoneOffset(); + var isSouthernHemisphere = (juneOffset > decemberOffset); + var winterTime = isSouthernHemisphere ? juneDate : decemberDate; + var summerTime = isSouthernHemisphere ? decemberDate : juneDate; + + $LocalTZ = new Date().getTimezoneOffset() / -60; + + + var dstStart = findNearestDateBefore(winterTime, function (date) { + return date.getTimezoneOffset() == summerTime.getTimezoneOffset(); + }); + $DST_start_month = dstStart.getMonth(); + $DST_start_sunday = dstStart.getDate() > 15 ? '"last"' : '"first"'; + $DST_start_hour = dstStart.getHours(); + $DST_start_minutes = dstStart.getMinutes(); + + var dstEnd = findNearestDateBefore(summerTime, function (date) { + return date.getTimezoneOffset() == winterTime.getTimezoneOffset(); + }); + $DST_end_month = dstEnd.getMonth(); + $DST_end_sunday = dstEnd.getDate() > 15 ? '"last"' : '"first"'; + $DST_end_hour = dstEnd.getHours(); + $DST_end_minutes = dstEnd.getMinutes(); + + return; +})(); + + //15.9.1.2 Day Number and Time within Day function Day(t) { return Math.floor(t/msPerDay); @@ -323,19 +386,4 @@ function ConstructDate(year, month, date, hours, minutes, seconds, ms){ var r11 = MakeDate(r9, r10); return TimeClip(UTC(r11)); -} - - -//the following values are normally generated by the sputnik.py driver -// for now, we'll just use 0 for everything -/* -var $LocalTZ=-8; -var $DST_start_month=2; -var $DST_start_sunday='first'; -var $DST_start_hour=2; -var $DST_start_minutes=0; -var $DST_end_month=10; -var $DST_end_sunday='first'; -var $DST_end_hour=2; -var $DST_end_minutes=0; -*/ +} \ No newline at end of file