2

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?

Francisc
  • 77,430
  • 63
  • 180
  • 276
  • 1
    UTC and GMT is the SAME... not sure what you mean ? – Yahia Oct 25 '11 at 12:49
  • I meant GMT+2. The local time is GMT+2, yet after the 10th month of the year all hours show as 1h sooner. I looked up the winter time and it happens on the 31st of October. That's probably it. But not sure howw to deal with this as I was thinking AS3's date does all the date mumbo-jumbo. – Francisc Oct 25 '11 at 14:06
  • Do you save the the Timezone information somewhere ? Where does the AS3 code run ? Does it run on a machine with correct TimeZone settings ? – Yahia Oct 25 '11 at 14:15
  • Hey. The timezone info is taken from the device. It's run on a mobile device as an AIR app. – Francisc Oct 25 '11 at 21:50

2 Answers2

1

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
}
Corey
  • 5,818
  • 2
  • 24
  • 37
  • Hm, that's interesting. I'll give that a test tomorrow and comeback. Thank you. – Francisc Oct 25 '11 at 21:51
  • That worked, thank you. I'm not sure if this works well for other timezones than my own, but what I did was: `myDate.hours+=1+getDST(converted)/MILLISECONDS_IN_A_DAY;` – Francisc Oct 27 '11 at 10:20
0

If you just wish to return a +2 date value from UTC 0 using select statement and given you don't have to cater for DLST, example would be South Africa Then the following worked for me:

SELECT [FieldA]
       ,DATEADD(hour,2([DateField])) AS [NewDate]
FROM [Table]