Flex: All your dates are belong to us
- January 12th, 2010
- Posted in Announcements
- Write comment
I feel like I’m constantly ranting about Flex’s implementation of Dates. This week I just discovered a new “feature” that is even more annoying to me.
Without going over it all again, Flex dates are timezone aware and the UTC offset is automatically calculated for you. The problem is that you are locked into displaying the timezone of the local system clock unless you hack the date values. But when sending over the wire to the server the date is automatically converted into the server’s timezone without telling you what timezone the Date is supposed to be in. In other words, you get “8:00 PM UTC” but you have no ideas what timezone that pertains to, so you don’t know whether the Date is actually supposed to read 2:00 PM EST, 1:00 PM CST, or 11:00 AM PST. Technically those are all the same “time,” but in some cases you need to work in timezones other than your own.
Anyway, I implemented my own solution for this issue last year, but was going on the assumption that the timezone offset is based only on the local system clock. What I found out last week is that the timezone offset will actually change if you create a Date in the past or future where DST has kicked in/out. In other words, every Date has it’s own offset. I’ll admit that could probably be handy however if you are trying to neutralize the timezone changes that Flex does, it only makes your job that much harder. I had not been expecting this and so we had to do some modification to our client to correctly handle DST changes at the server.
Below is some code to demonstrate how the timezone offset changes with DST, even though your system clock is still the same. Note that you need to set your system clock to a timezone in the US that observes daylight savings time on March 15. (Chicago for example). If you’re in the UK the swtich is March 28, so you can change the dates to March 27 & March 28 to see the effect. Notice that the timeZoneOffset property is different between the two dates. I suppose this makes sense but it definitely creates hassles on the server side.
<?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" height="700" width="1000" creationComplete="onCreationComplete()"> <mx:Script> <![CDATA[ public function onCreationComplete():void { var d1:Date=new Date(2010, 2, 14); // march 14 result1.text="MARCH 14: " + d1.toDateString() + " OFFSET = " + d1.timezoneOffset.toString(); var d2:Date=new Date(2010, 2, 15); // march 15 result2.text="MARCH 15: " + d2.toDateString() + " OFFSET = " + d2.timezoneOffset.toString(); } ]]> </mx:Script> <mx:Label id="result1"/> <mx:Label id="result2"/> </mx:WindowedApplication>
I’m interested in anybody else’s ideas or solutions as to how you work with dates across timezones in Flex. Please post a comment.
No comments yet.