Skip to content

Instantly share code, notes, and snippets.

@mobz
Last active November 22, 2017 10:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mobz/53a3253884227b59d4ea66aaa2992bf5 to your computer and use it in GitHub Desktop.
Save mobz/53a3253884227b59d4ea66aaa2992bf5 to your computer and use it in GitHub Desktop.

Investigations show that ALL timezones that observe DST are in DST

  • Jan 1st (southern hemisphere) or
  • July 1st (northern hemisphere)

No binary search is required to simply know if a given moment is observing dst.

This allows us to imagine the following algorithm

var getTimezoneOffset = require('get-timezone-offset');

  module.exports = function( tz, date ) {
    var d = new Date(date);
    var offset = getTimezoneOffset( tz, d );
    var jan = getTimezoneOffset( tz, new Date(d.setMonth(0, 1) );
    var jul = getTimezoneOffset( tz, new Date(d.setMonth(5, 1) );
    var dst = Math.abs( jan - jul ) === 60 && Math.min( jan, jul );
    return {
      hasDst: !!dst,
      isDst: offset === dst,
      northern_hemisphere: dst === jul,
      southern_hemisphere: dst === jan
    };
  }

However this fails when the offset for a timezone changes permanently during the year, which happens a lot

A test which checks a single time each day for every timezone for a year takes about a minute to run.

The algorithm could be improved by testing jan/jul for a year each side and drawing conclusions from that

Also

  • Singapore had a DST of 20 minutes in 1933
  • Namibia observe Winter Time which effectivly puts the rest of the year in summary time
  • UK in 1940-45 shifted timezones and observed DST, which is known as double daylight savings
  • a bunch of other weird time oddities

In anycase you can get close with a 12 line program or closer with a larger program, but it won't be 100% accurate.

Hemisphere calculations will both be false if the timezone is not shown to be in DST

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment