UNPKG

dst

Version:

determine if a date is in daylight savings time

73 lines (53 loc) 2.02 kB
;(function() { // so, the only way we (reliably) get access to DST in javascript // is via `Date#getTimezoneOffset`. // // this value will switch for a given date based on the presence or absence // of DST at that date. function find_dst_threshold (near, far) { var near_date = new Date(near) , far_date = new Date(far) , near_offs = near_date.getTimezoneOffset() , far_offs = far_date.getTimezoneOffset() if(near_offs === far_offs) return 0 if(Math.abs(near_date - far_date) < 1000) return near_date return find_dst_threshold(near, near+(far-near)/2) || find_dst_threshold(near+(far-near)/2, far) } function find_dst_thresholds() { var d = new Date() , d = new Date(d.getFullYear(), 0, 1) , f = new Date(d.getFullYear(), 11, 31) , x , first , second x = (f - d) / -2 first = find_dst_threshold(+d, d - x) second = find_dst_threshold(d - x, +f) return { spring_forward : first ? (first.getTimezoneOffset() < second.getTimezoneOffset() ? second : first) - new Date(d.getFullYear(), 0, 1, 0, 0) : 0 , fall_back : first ? (first.getTimezoneOffset() < second.getTimezoneOffset() ? first : second) - new Date(d.getFullYear(), 0, 1, 0, 0) : 0 } } var THRESHOLDS = find_dst_thresholds() function is_dst(datetime, thresholds) { thresholds = thresholds || THRESHOLDS if(thresholds.spring_forward === thresholds.fall_back) return false var offset = datetime - new Date(datetime.getFullYear(), 0, 1, 0, 0) , dst_is_reversed = thresholds.spring_forward > thresholds.fall_back , max = Math.max(thresholds.fall_back, thresholds.spring_forward) , min = Math.min(thresholds.fall_back, thresholds.spring_forward) if(min < offset && offset < max) return !dst_is_reversed return dst_is_reversed } Date.prototype.isDST = function(thresholds) { return is_dst(this, thresholds) } is_dst.find_thresholds = find_dst_thresholds if(typeof module !== 'undefined') { module.exports = is_dst } else { window.is_dst = is_dst } })()