Home ProjectsThe Computus Engine Inside the AS3 Date class: Timezones and Daylight Saving Time

# 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)
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
}
```

#### 19 comments

January 29, 02009 - 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.

February 9, 02011 - 10:28 pm

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.

January 29, 02009 - 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

June 15, 02009 - 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.

June 19, 02009 - 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…

March 16, 02010 - 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.

June 17, 02010 - 1:34 pm

[…] 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 […]

June 17, 02010 - 2:45 pm

[…] 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 […]

June 17, 02010 - 3:13 pm

[…] 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 […]

June 17, 02010 - 3:43 pm

[…] 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 […]

June 18, 02010 - 1:40 am

[…] 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 […]

February 9, 02011 - 6:19 am

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!

February 9, 02011 - 3:43 pm

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!

February 9, 02011 - 10:04 pm

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.

October 26, 02011 - 1:31 am

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.

January 9, 02013 - 1:31 pm

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

January 9, 02013 - 1:45 pm

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.

March 4, 02014 - 1:06 pm

“pole to pole” is not exactly my favorite description of longitudinally.

March 28, 02014 - 10:06 pm

True, but’s understandable without getting overly technical.

This site uses Akismet to reduce spam. Learn how your comment data is processed.