|
|
||||
|
|
Ultrashock Tutorials > Flash5 > The Actionscript Date Object | |||
|
||||
|
|
The Actionscript Date Object |
|
||
|
What time is it? It's a simple enough question, but the answer is more complex than you might think. This tutorial serves as an introduction to the Flash 5 Actionscript Date object. It also contains some important information concerning the problem with daylight saving time. It is aimed at Flash users who are already proficient with Actionscript. The Date object now = new Date()
onClipEvent (enterFrame) { If you add a dynamic text box (displaying the variable 'now_display') inside this movie you'll find you have a clock that updates in real time. It may not be very pretty but it doesn't get any simpler. This principle forms the basis of any clock simulation you will ever build. With a bit of playing around you should be able to use these and other values to produce all manner of clock and calendar displays.
This is because the Earth is divided longitudinally (pole to pole) into 24 time zones spaced 15° apart. The lines dividing the time zones are not all straight but the areas within each time zone observe a local or 'civil time' one hour earlier than the zone immediately to the east. Other factors such as Daylight Saving time (see later) may also affect the value of local time. Consequently the same moment in time has a unique local interpretation depending on the time of year and your position on Earth. For the purposes of Actionscript, local time is the time returned by the operating system on which the Flash Player is running. We use the following methods to get local time values: now_Milliseconds = now.getMilliseconds() now_Seconds = now.getSeconds() now_Minutes = now.getMinutes() now_Hours = now.getHours() now_Day = now.getDay() now_Date = now.getDate() now_Month = now.getMonth() now_FullYear = now.getFullYear()
now_Year = now.getYear() Actionscript is based upon ECMA-262, the same standard used by Javascript. Consequently the Actionscript and Javascript Date objects and methods are almost identical. The .getyear method is a throwback to earlier incarnations of the Javascript Date object that existed before the introduction of .getFullYear. There is no UTC equivalent to .getyear . Universal Coordinated Time (UTC) If you've ever built a website that relies on the correct time (any e-commerce site for example) then you'll understand the clear need for a coordinated world-wide standard of time. The history behind this achievement is a long and fascinating story but sadly out with the realms of this tutorial. If you would like to know more I would strongly advise you to read, 'The Calendar' by David Ewing Duncan (ISBN 1-85702-979-8). Suffice to say, these days we have a Universal Coordinated Time (sometimes called 'Zulu' time but usually shortened to UTC). This is the modern equivalent of Greenwich Mean Time (GMT) and more or less equates to the same thing: It is the local unadjusted time at a single longitude on the planet: the prime meridian. When it is 4PM UTC in New York, it is 4 PM UTC in London and 4 PM UTC in Tokyo. It is often useful to know the difference between the current local time and the current UTC time. The following method returns the difference, measured in minutes. now_TimezoneOffset =
now.getTimezoneOffset() We use the following methods to get Universal Standard
Time values: now_UTCSeconds = now.getUTCSeconds() now_UTCMinutes = now.getUTCMinutes() now_UTCHours = now.getUTCHours() now_UTCDay = now.getUTCDay() now_UTCDate = now.getUTCDate() now_UTCMonth = now.getUTCMonth() now_UTCFullYear = now.getUTCFullYear() The Flash player measures UTC time in a similar way to Javascript. It maintains a counter that tracks the number of milliseconds that have elapsed since midnight on January 1, 1970. Dates prior to this will return a negative number. This value can be tracked using the .getTime method. now_Time = now.getTime() Thankfully the Flash player does not suffer from the old 32 bit Unix Date problem (Unix overflows after February 2106) and quite happily records dates up into the 33rd millennium.
In a perfect world daylight saving time wouldn't be an issue. You would think that, as local times are determined from the operating system and, as the OS adjusts automatically for DST, then it would always be correct. In reality though Flash calculates 'local' time rather than reads it. This would be fine if it was calculated correctly but due to the unfortunate assumption that dates for DST are a global standard we have a problem. Flash follows the US convention for DST: DST begins at 2am on the first Sunday of April. The rest of the world (and in fact some parts of the US) use there own dates for DST. In the European Union for example DST has now been standardized to begin at 1am on the last Sunday of March and end at 1am on the last Sunday of October. So what does this mean? It's official What can you do? In the next tutorial I'll look at ways of extending the functionality of the Date object. In theory it should be possible to code some form of lookup table vs location to do some local correction but in reality though the politics of DST are a very muddy affair. To give you some idea, Cuba always starts on April 1st (as does Syria and Iraq). China used to adopt DST but doesn't any more. Israel has a set number of DST days mandated in law but makes the starting dates up every year. Saskatchewn province doesn't but the rest of Canada does. In the US, DST is not observed in the Eastern Time Zone portion of the State of Indiana, or in the state of Arizona except for the Navajo Indian Reservation, which does observe DST! In short, it's a mess and we're stuck with it. Another possible solution involves using the Javascript Date object to validate the local times generated by the Flash player. I've written a tool to compare both objects if you want to look at the differences (click on TimeMachines 1 from the top menu). It should be pointed out though that Javascript Date object is subject to a number of issues itself so this is by no means a rock solid solution.
One of the most common uses for the Date object is to store specific dates. To do this we use the extended Date syntax: Date(year, month, date, hour, minute, second, millisecond ) Dates can be set relative to local time or UTC time. If specific dates are to be compared then it would be wise to use the slightly more verbose UTC syntax. This allows for comparisons irrespective of local modifiers such as time zones and DST. This example demonstrates the difference in the values of .getTime when the same date is set in local and UST time. This difference is the result of DST being in effect. local = new Date(2001,
5, 1, 0, 0, 0, 0 ) local.getTime() UTCTest = new Date(Date.UTC(2001
,5 ,1 ,0 ,0 ,0 ,0)) UTCTest.getTime()
now.setMilliseconds(new_value) now.setSeconds(new_value) now.setMinutes(new_value) now.setHours(new_value) now.setDate(new_value) now.setMonth(new_value) now.setFullYear(new_value) And a couple of whacky ones now.setYear(new_value) now.setTime(new_value)
now.setUTCMilliseconds(new_value) now.setUTCSeconds(new_value) now.setUTCMinutes(new_value) now.setUTCHours(new_value) now.setUTCDate(new_value) now.setUTCMonth(new_value) now.setUTCFullYear(new_value)
A common misconception with the Date object is that comparisons are black art. Prepare to be astounded. The beauty of the object is that you can simply add them and subtract them from each other. For example, here's how to find out how many shopping days to Christmas! now = new Date()
Like any other function it must be declared before it can be called so make sure you include all the extensions you wish to use early in your movie.
// Prototype to return AM or PM // Prototype to convert 24 hour to 12 hour clock
(integer 0-11)
Epilogue The correct time is the single most important variable in any dynamic site. Hopefully this tutorial has helped to demystify one of the most useful new objects in the Actionscripters armoury.
|
||||
|
|
©2001 Ultrashock.com Inc. - All rights reserved
|
|