Last active
December 22, 2015 00:00
-
-
Save takeshy/6385997 to your computer and use it in GitHub Desktop.
check if Daylight Saving Time using JavaScript.
夏時間判定
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//3月の第2日曜日 | |
function summerTimeStart(y){ | |
var first = new Date(y,2,8); | |
var i = 9; | |
while(first.getDay() != 0){ | |
first = new Date(first.setDate(i++)); | |
} | |
return first; | |
} | |
//11月の第1日曜日 | |
function summerTimeEnd(y){ | |
var last = new Date(y,10,1); | |
i = 2; | |
while(last.getDay() != 0){ | |
last = new Date(last.setDate(i++)); | |
} | |
return last; | |
} | |
function isSummerTime(d){ | |
var y = d.getFullYear(); | |
var start = summerTimeStart(y); | |
var end = summerTimeEnd(y); | |
return d.getTime() >= start.getTime() && d.getTime() < end.getTime(); | |
} | |
if (typeof window === 'undefined') { | |
module.exports = { | |
summerTimeStart: summerTimeStart, | |
summerTimeEnd: summerTimeEnd, | |
isSummerTime: isSummerTime | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('../test_helper.js'); | |
var util = require('../lib/summer_time.js'); | |
QUnit.test('isSummerTime', function() { | |
//2007年 3月11日 11月4日 | |
//2008年 3月9日 11月2日 | |
//2009年 3月8日 11月1日 | |
//2010年 3月14日 11月7日 | |
//2011年 3月13日 11月6日 | |
//2012年 3月11日 11月4日 | |
//2013年 3月10日 11月3日 | |
//2014年 3月9日 11月2日 | |
//2015年 3月8日 11月1日 | |
var days = [ | |
{s: new Date(2007,2,11), e: new Date(2007,10,4)}, | |
{s: new Date(2008,2,9), e: new Date(2008,10,2)}, | |
{s: new Date(2009,2,8), e: new Date(2009,10,1)}, | |
{s: new Date(2010,2,14), e: new Date(2010,10,7)}, | |
{s: new Date(2011,2,13), e: new Date(2011,10,6)}, | |
{s: new Date(2012,2,11), e: new Date(2012,10,4)}, | |
{s: new Date(2013,2,10), e: new Date(2013,10,3)}, | |
{s: new Date(2014,2,9), e: new Date(2014,10,2)}, | |
{s: new Date(2015,2,8), e: new Date(2015,10,1)} | |
] | |
QUnit.expect(days.length * 4); | |
for(var i=0;i<days.length;i++){ | |
var d = days[i].s; | |
QUnit.equal(util.isSummerTime(d),true); | |
QUnit.equal(util.isSummerTime(new Date(d.getTime()-1)),false); | |
d = days[i].e; | |
QUnit.equal(util.isSummerTime(d),false); | |
QUnit.equal(util.isSummerTime(new Date(d.getTime()-1)),true); | |
} | |
}); | |
QUnit.start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment