0

I've recently come across a problem...

Lets say I have some Javascript like this, for example:

var timezone = -(new Date()).getTimezoneOffset() / 60; // Get the TZ offset

var local = [];
var gmt   = [];

for(var i = 0; i <= 23; i++) // Loop through each hour
{
    local.push(i);

    var time = (i + timezone);

    if(time == 24)
        time = 0;

    gmt.push(time);
}

This works perfectly while not in DST, but when DST comes around I have unexpected results such as this: (Local user is in GMT/DST)

> console.log(local);
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]

> console.log(gmt);
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 0]

Where the two arrays should be the same.

How would I detect when Daylight Saving is in effect?

Jordan Doyle
  • 2,976
  • 4
  • 22
  • 38
  • 1
    The results seem completely correct given the algorithm you are using. It should work for every day except when crossing daylight time boundaries. Maybe you can explain the problem you are having. – Tom Blodget Apr 07 '13 at 16:51
  • You mean that the local timezone is GMT/BST (same as WET/WEST), don't you? – Tom Blodget Apr 07 '13 at 16:51
  • The problem is the application should detect when BST is in effect and basically change the GMT to BST @TomBlodget – Jordan Doyle Apr 07 '13 at 17:24
  • @JordanDoyle - That appears to be exactly what it is doing. It detects BST is in effect, by getting the offset from `new Date()` which returns the current date/time. Why would you expect it to stay the same? Are you just looking for the "standard" offset without regard to DST? – Matt Johnson-Pint Apr 07 '13 at 18:37
  • I don't expect the two to be the same, because the user is in the (currently) BST timezone, meaning the two arrays should be the same - with regard to DST. Before DST took place the two arrays would've been the same. @MattJohnson – Jordan Doyle Apr 07 '13 at 18:44
  • huh? Is this a riddle? :) "When is a dog not a dog?" – Matt Johnson-Pint Apr 07 '13 at 18:52
  • Basically, you said "I don't expect them to be the same because they are the same". X != X ? does not compute... Please clarify. Thanks. – Matt Johnson-Pint Apr 07 '13 at 18:53

2 Answers2

0

This function will help you in calculations using DST.

function dstOffset() {
    var dailyOffsets = [];
    for (var i = 0; i <= 366; i++) { // sample timezone offset over at least a year since daylight saving changes are repeated each year.
        var sampleDay = new Date(); // today at whatever time
        sampleDay.setDate(i); // i days from the begining of this month (at the same time)
        dailyOffsets.push(sampleDay.getTimezoneOffset());
    }
    return Math.min.apply(null, dailyOffsets);
}
alert(dstOffset() + " minutes");
Tom Blodget
  • 20,260
  • 3
  • 39
  • 72
  • This will work, but it is certainly a brute force method. And it doesn't take into account that many time zones have changed their offsets in the past. Your best suggestion so far has been to use a time zone database. Preferably, one would do this on the server, but there are some available for javascript if required. – Matt Johnson-Pint Apr 08 '13 at 06:01
0

This is what I ended up getting, detects the last Sunday of March and then last Sunday of October and checks if we're currently within those days. Thanks for your comments and answers, everyone.

Feel free to edit and give me suggestions/improvements for it. I have a feeling this isn't the best way to do it.

function is_bst()
{
    var date    = new Date();
    var march   = null;
    var october = null;

    for(var i = 7; i != 0; i--)
    {
        var day = new Date(date.getFullYear(), 2, i);

        if(day.getDay() == 0)
        {
            march = day;
            break;
        }
    }

    for(var i = 7; i != 0; i--)
    {
        var day = new Date(date.getFullYear(), 9, i);

        if(day.getDay() == 0)
        {
            october = day;
            break;
        }
    }

    return !(date < march || date > october);
}
Jordan Doyle
  • 2,976
  • 4
  • 22
  • 38
  • 1
    Your code is too localized. At some point this code will be run by someone not in the UK. At some point the UK will change its DST law and later the JavaScript library will be updated to reflect that. – Tom Blodget Apr 07 '13 at 20:56
  • Is there anyway to detect when a defined country goes into DST? @TomBlodget – Jordan Doyle Apr 07 '13 at 21:46
  • See my earlier answer for a work-around. I don't think a time zone database is available in JavaScript. – Tom Blodget Apr 07 '13 at 22:01
  • @TomBlodget - There are several. Four that I know of I listed [here](http://stackoverflow.com/a/15171030/634824). – Matt Johnson-Pint Apr 08 '13 at 05:53
  • @JordanDoyle - If you are trying to determine specifically if BST or GMT is in effect, then you really need a timezone database, and you would specify the `Europe\London` time zone. If you're just trying to determine if the user is in some form of daylight savings time or not, you would have to sample several dates from their clock and try to make some guesses. One answer in the "possible duplicate" link shows a check in Jan and another in July. That is still fairly assumptive, but is on the right track. – Matt Johnson-Pint Apr 08 '13 at 05:59