Tuesday, February 7, 2012

Timezones and Daylight savings

In XQuery, the dateTime is associated with a TimeZone - expressed as a difference from GMT, the +01:00 below:

fn:current-dateTime()
2012-02-07T11:33:37.829+01:00


to convert this dateTime to GMT, use this trick:

fn:adjust-dateTime-to-timezone(fn:current-dateTime(),xdt:dayTimeDuration('PT0H'))
2012-02-07T10:33:37.829Z

to discover which is your timezone:

fn:implicit-timezone()
PT1H

To convert a String to dateTime:
xs:dateTime("2012-02-07T10:39:27")

To convert a String to date:
xs:date("2012-02-07")

So instead of fn:current-dateTime() you can pass an explicit dateTime:

fn:adjust-dateTime-to-timezone(xs:dateTime("2012-02-07T11:33:37.829+01:00"),xdt:dayTimeDuration('PT0H'))
2012-02-07T10:33:37.829Z

(since I am in +1 timeZone, the hour goes back 1 hour after adjustment)

BEWARE: the difference with GMT changes with the DayLight savings period (23 march to 22 September approximately)

setting the clock to june the 14th, the difference becomes 2 hours:

fn:current-dateTime()
2012-06-14T11:50:36.124+02:00

but our system to adjust dateTime to GMT still works perfectly:

fn:adjust-dateTime-to-timezone(fn:current-dateTime(),xdt:dayTimeDuration('PT0H'))
2012-06-14T09:50:36.124Z

No comments: