Search code examples
actionscript-3datetimedateutclocaltime

UTC Time to GMT+2 in AS3


I'm having a problem. Using the Date() functions AS3 provides natively, I'm converting UTC time to local machine time.

The problem I have is this, look at month and hour.

2011-10-07 18:45:00 -> 2011-10-07 21:45:00
2011-11-07 18:45:00 -> 2011-11-07 20:45:00

This is probably because of the daylight saving, but I thought the native functions for date manipulations take this into consideration.

Am I right, that's the problem? I'm supposed to account for daylight savings myself? Or is it something different?


Solution

  • From the Date Documentation:

    The Date class handles daylight saving time differently, depending on the operating system and runtime version... The Date object detects whether daylight saving time is employed in the current locale, and if so, it detects the standard-to-daylight saving time transition date and times.

    From this, I believe this means that the Date object will detect DST for present time. If you're looking at a date in the future, I think you'll need to handle the offset on your own.

    I have used this code in the past to account for DST found on computus.org:

    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
    }