Inside the AS3 Date class: Timezones and Daylight Saving Time

Posted on April 8, 02008
Filed Under Development, Flash, Latham

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)
I
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.

 var tzo:Number = d.timezoneOffset

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.

public static function getTimezone():Number
{
  // Create two dates: one summer and one winter
  var d1:Date = new Date( 0, 0, 1 )
  var d2:Date = new Date( 0, 6, 1 )

  // largest value has no DST modifier
  var tzd:Number = Math.max( d1.timezoneOffset, d2.timezoneOffset )

  // convert to milliseconds
  return tzd * 60000
}

public static function getDST( d:Date ):Number
{
  var tzd:Number = getTimezone()
  var dst:Number = (d.timezoneOffset * 60000) - tzd
  return dst
}
Share/Bookmark

Related Journal Entries

  1. A review of Temporal Web Standards
  2. Inside the AS3 Date class: Introduction
  3. Inside the AS3 Date class: The Basics
  4. Inside the AS3 Date class: The constructor
  5. Inside the AS3 Date class: Properties and methods
  6. The project architecture
  7. Hacking the Date class for GeoLocation
  8. Projects update March 2009
  9. Big History
  10. Project updates April 2009
  11. AS3 component: AnimatedButton
  12. Measuring Time: Datatypes
  13. Revelation
  14. Creating a main preloader in Flash CS3
  15. Minkowski Spacetime

Comments

Leave a Comment

If you would like to make a comment, please fill out the form below.

Name (required)

Email (required)

Website

Comments

10 Comments so far
  1. Immo January 29, 2009 9:30 pm

    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 wwp.britishsummertime.co.uk 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.

  2. John January 29, 2009 10:11 pm

    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 http://www.twinsun.com/tz/tz-link.htm . 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 :)
    jd

  3. Immo June 15, 2009 3:08 pm

    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.

  4. John June 19, 2009 1:18 pm

    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…

  5. Ward Ruth March 16, 2010 4:33 pm

    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.

  6. [...] final challenges emerged just as we were preparing to go live. On one hand, I delved into the depths of Flash’s Date object to ensure that everything would work in every possible timezone. On the other, we’d decided [...]

  7. [...] final challenges emerged just as we were preparing to go live. On one hand, I delved into the depths of Flash's Date object to ensure that everything would work in every possible timezone. On the other, we'd decided to [...]

  8. [...] final challenges emerged just as we were preparing to go live. On one hand, I delved into the depths of Flash’s Date object to ensure that everything would work in every possible timezone. On the other, we’d decided [...]

  9. [...] final challenges emerged just as we were preparing to go live. On one hand, I delved into the depths of Flash’s Date object to ensure that everything would work in every possible timezone. On the other, we’d decided [...]

  10. [...] final challenges emerged just as we were preparing to go live. On one hand, I delved into the depths of Flash’s Date object to ensure that everything would work in every possible timezone. On the other, we’d decided [...]