Skip to content

Instantly share code, notes, and snippets.

@troufster
Created March 10, 2011 12:11
Show Gist options
  • Save troufster/864008 to your computer and use it in GitHub Desktop.
Save troufster/864008 to your computer and use it in GitHub Desktop.
Detecting EU DST
function dstBounds(date, month) {
var d = new Date(date.getFullYear(),/*March*/(month-1),31),
day = d.getDay();
if (day != 0) d.setDate(d.getDate()-day);
//Dst starts @(UTC 01:00)
//this implementation is in UTC+1 (02:00)
d.setHours(2);
return d;
}
function isDst(date) {
var start = dstBounds(date, 3),
end = dstBounds(date,10);
if (date >= start && date <= end) return true;
return false;
}
function equals(a,b, desc) {
if (a!=b) throw new Error(a + ', ' + b + ' are not equal at test: ' + desc)
}
function test() {
//Test sundays dstBounds
var year = 1970;
while(year < 2100) {
var d = new Date(year,1,1);
var dststart = dstBounds(d, 3);
var dstend = dstBounds(d, 10);
if(dststart.getDay() != 0) console.log("Start not a sunday");
if(dstend.getDay() != 0) console.log("End not a sunday");
year++;
}
//Test 2011 DST starts
var d = new Date(2011, 2, 25);
var t = equals(isDst(d), false);
var d = new Date(2011, 2, 27, 1,00);
var t = equals(isDst(d), false);
var d = new Date(2011, 2, 27, 1,59);
var t = equals(isDst(d), false);
var d = new Date(2011, 2, 27, 2,0);
var t = equals(isDst(d), true);
var d = new Date(2011, 2, 27, 2,1);
var t = equals(isDst(d), true);
var d = new Date(2011, 2, 28);
var t = equals(isDst(d), true);
//DST ends
var d = new Date(2011, 9, 30);
var t = equals(isDst(d), true);
var d = new Date(2011, 9, 30, 1,00);
var t = equals(isDst(d), true);
var d = new Date(2011, 9, 30, 1,59);
var t = equals(isDst(d), true);
var d = new Date(2011, 9, 30, 2,00);
var t = equals(isDst(d), true);
var d = new Date(2011, 9, 30, 2,01);
var t = equals(isDst(d), false);
//2012 DST starts
var d = new Date(2012, 2, 23);
var t = equals(isDst(d), false, '2012 3 23');
var d = new Date(2012, 2, 25, 1,00);
var t = equals(isDst(d), false, '2012, 3 25 01.00');
var d = new Date(2012, 2, 25, 1,59);
var t = equals(isDst(d), false);
var d = new Date(2012, 2, 25, 2,0);
var t = equals(isDst(d), true);
var d = new Date(2012, 2, 26, 2,1);
var t = equals(isDst(d), true);
var d = new Date(2012, 2, 27);
var t = equals(isDst(d), true);
//2012 DST Ends
var d = new Date(2012, 9, 27);
var t = equals(isDst(d), true);
var d = new Date(2012, 9, 28, 1,00);
var t = equals(isDst(d), true);
var d = new Date(2012, 9, 28, 1,59);
var t = equals(isDst(d), true);
var d = new Date(2012, 9, 28, 2,00);
var t = equals(isDst(d), true);
var d = new Date(2012, 9, 28, 2,01);
var t = equals(isDst(d), false);
console.log("All OK");
};
test();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment