Inside the AS3 Date class: Timezones and Daylight Saving Time

Local time
The same moment in time has a unique local interpretation depending upon the time of year and your position on Earth. The operating system provides the Flash Player with two local temporal modifiers: One for Time zone designation (TZD) and one for Daylight saving time (DST). Local time is derived by applying these modifiers to the value of UTC.

Local time = UTC + TZD + DST

Timezones: from ‘Keeping Track of Time with Flash MX’‘, New Riders 2002

Time zone (TZD)
f you ask two people at different longitudes “what time is it?” you will get two different answers. For example, when it is 11 am in New York, it is 4 pm in London and 1 am the following day in Tokyo. This is because the Earth is divided longitudinally (pole to pole) into 24 time zones spaced 15° apart. The areas within each time zone observe a local or ‘civil time’ one hour earlier than the zone immediately to the east.

Gotcha: Territories on a zone boundary decide which side of the zone they wish to be on. Consequently very few of the lines dividing the time zones are actually straight.

Daylight Saving Time (DST)
Thanks mainly to the efforts of a London builder named William Willett we have the controversial notion of daylight saving time (DST). This practice means that in some locations, all clocks are put one hour ahead of local (TZD) time during the summer months.

Gotcha: The Flash 5 Player incorrectly applies the DST rule for the US Pacific territory to all locations.

Gotcha: Although it’s easy to determine whether DST is in effect at the end user’s location, it’s a much bigger problem to determine it for any other location. The rules for DST are a muddy political affair that vary greatly from location to location, and year to year.

Determining the values of TZD and DST
It is often useful to know the difference between the current local time and the current UTC time. The following property returns the difference, measured in minutes.

Gotcha: The term ‘timezoneOffset’ is a bit of a red herring as this value contains the combined offset for timezone and Daylight Saving Time.

The following code can be used to determine the individual values of TZD and DST. Both functions return their offsets in milliseconds.


19 thoughts on “Inside the AS3 Date class: Timezones and Daylight Saving Time

  1. Very interesting.- I am just building a world clock as a projector app in as3 where the user can set various clocks to their preferred cities. At the moment I am struggling to get my head around the DST. Let’s say a user in Japan sets one of the clocks to ‘London UK’. Sounds pretty easy in theory: UTC + TZD + DST would mean e.g. ‘London UK’ time in January : UTC + 0 + 0. But according to the dates change to what the last Sunday of March or October is. Then again you get slightly different rules for the USA and every other territory.- Do you think there is some kind of clever proof way to find out whether a certain city in the world is actually in summertime or not (or whether it is using it at all) or do you think I will need to hardcode all the eventualities and check with if statements e.g. for ‘London UK’ if (UTC + TZD > 29 March) then DST = false? And next year update my code to Sunday the 28th of March? Is there a way to find out what the date for the last Sunday of a month would be in a certain year? I am kind of hoping there might be a clear logic solution to this. Any thoughts welcome. And thanks for the tips on this page. Already very helpful.

    • The zero numbering of months is a quirk of the ECMA262 spec I think. ISO8601 specifies months beginning with 01 for January.

      There have been some brave attempts to codify those calendars. Search Google for the excellent books ‘Calendrical Calculations’ and ‘Standard C DateTime Library’ for versions in Lisp/Mathematica/Java and C respectively.

  2. Hi there Immo – I share your pain. Your spot on so far. It’s simple enough to determine “standard time” in any territory using a mixture of UTC (from the Date class) and some sort of TZD lookup table.

    It’s also easy enough to get DST for the users current location using the functions in the article. The showstopper, as you’ve correctly spotted, is determining if DST is in effect in another territory.

    Unlike TZDs, DST is not a global standard. The dates for adoption vary from territory to territory, and even within territories. Check out the DST rules for the Navajo and Hopi indian reservations in Arizona for some idea of how convoluted this can get!

    The upshot of all this is that implementing a DST lookup in pure Actionscript would be a huge undertaking. Your best bet would be to use a lookup of the excellent TZ Database at . You could either build yourself a web service (PHP uses the TZ DB) or you could build your app using AIR and use the internal SQLLite database.

    Hope that helps 🙂

  3. Hi John, only noticed your reply now.- The project is done and dusted and working just fine. I ended up writing a class that picks up the DST from a data list that can be updated whenever a territory decides to change their rules. I think I’ve got about 100 different entries there. Was quite a pain initially. I wrote a little script that detects the date for a specific day in the week, so to get e.g. the last Sunday in a month to switch DST. Works just fine but was very nervous when it came to the ‘live’ switch for the first time in spring ; ) Once again, thanks a lot for all the info on this page which helped very much.

    • Just wanted to say a quick thanks to all the folks who have IM’d, Twittered and emailed me. I feel the love! I’ve got a great new gig in Covent Garden and I’m really excited about it. Normal service will be resumed asap…

  4. Ward Ruth says:

    Thanks, very useful! Did note that for the getTimezone() method, I needed to use a more recent reference date (I develop on a Mac, so that may make a difference). I used 2000 instead of 0 (1900). Using the 0 date there was no difference in the timezoneOffset values for d1 and d2.

  5. Pingback: World Cup instant Twitter replay: how we did it | BB SPORT

  6. Pingback: World Cup instant Twitter replay: how we did it #the guardian #world cup 2010 | soccer ts

  7. Pingback: World Cup instant Twitter replay: how we did it | World News

  8. Pingback: World Cup instant Twitter replay | World News

  9. Pingback: World Cup instant Twitter replay | Top Mobile Accessories

  10. Robert says:

    With a bit of cleverness, it is not so difficult to get, for example, the date of the last Sunday of March. What you do is, you create a Date object for March 31, use getDay() to get the weekday number, and use setDate() to move the date back that many days.

    About the hardest part of dealing with calendar stuff in ActionScript is from the un-human month numbering: 0 through 11 instead of 1 through 12. I suppose that this is especially true for Chinese, Japanese, Korean, and Vietnamese programmers: in those languages, the “names” of the months are really just numbers, one to twelve!

    • Yes, the month numbering thing is definitely a gotcha! I tend to think of the month numbers in terms of referencing a zero-based Array and that way I remember.

      I think the toughest calendar systems to work with are the ones that require human intervention. The Islamic calendar for example requires a human sighting of the new crescent moon before the month can begin. Try programming that!

  11. Robert Lozyniak says:

    In my mind, the month numbers *are* the names, and that is why I get confused. I recently designed a (paper) calendar which for this reason has both the 1-to-12 system and the January-to-December system.
    What boggles my mind is: why are the months zero-based, but not the days of the month?

    The calendars that require human intervention are not difficult to code: they are impossible to code. The only way to “code” them is to make them contact a server for the number of days since the most recent crescent moon sighting or whatever.

    The most difficult to code are those requiring calculations regarding actual positions of astronomical bodies. This is so difficult that it seems programmers (including myself!) just give up and hardcode a table.

  12. Blake says:

    Thank you John. What a brilliant hack to determine the non-DST timezone offset. In retrospect, it’s obvious but I’ve been scratching my head over how to deal with timezone-aware dates in Flex and your solution fills the void that AS3 created.

    Combined with a timezone database return from the server (.NET has a wonderful TimeZoneInfo class to enumerate all the timezones and gather their offsets, etc.), I can now guesstimate what timezone the user is in based on the regular UTC offset and not worry about being off during summer.

  13. Nishanth says:

    Is there any way to get timeZone ID like IST,EST… and also can any body help me in gettiing the system region either in flash or javascript

  14. Hi Nishanth, I think the only way to get timeZone ID like IST,EST would be to use the algorithms at the bottom of the page to get the ‘real’ timezone offset (i.e. adjusted to remove DST) then have a lookup table to turn those offsets into strings.

Leave a Reply