Thursday, April 25, 2013

Flex time weirdness

Consider the following:

testDate.mxml

If you put that into Flash Builder and run it right now and click the button (this post is written in 2013, late April), you'll probably get something like this:

Beginning of this month: Mon Apr 1 2013
End of last month: Sun Mar 31 2013

However, if your timezone is set to "(UTC) Dublin Edinburgh, Lisbon, London", (as opposed to "(UTC) Monrovia, Reykjavik" or "(UTC) Coordinated Universal Time"), then you'll get this weirdness:

Beginning of this month: Mon Apr 1 2013
End of last month: Sat Mar 30 2013

Why? Well, because during the execution of the code, beginningOfThisMonth = Mon Apr 1 00:00:00 GMT+0100 2013, while endOfLastMonth = Sat Mar 30 23:00:00 GMT+0000 2013. Wait, what? Why did the timezone change? And why did it go to GMT+1? I thought UTC = GMT, no?

Let's see what wikipedia says:
[UTC] is one of several closely related successors to Greenwich Mean Time (GMT). For most purposes, UTC is synonymous with GMT, but GMT is no longer precisely defined by the scientific community.

Well, that's neat. How do we fix that? Best solution: do away with timezones, all together, as a culture. They're way more trouble than they're worth, if you think about it (especially if you, like me, think about a future where we go to other planets). But that's not going to happen soon, and I need this fix soon.

Here's a hint. That leads me to this quasi-fixed better approach:


var beginOffset:Number = beginningOfThisMonth.getTimezoneOffset(); //if utc london=-60; if plain utc=0; if EST=240
var endOffset:Number = endOfLastMonth.getTimezoneOffset();//if utc london=0; if plain utc=0; if EST=240
if (beginOffset == endOffset){/* no problem */}
else {
//problem
var offsetMilliseconds:Number = beginningOfThisMonth.getTimezoneOffset() * 60 * 1000;
endOfLastMonth.setTime(beginningOfThisMonth.getTime() + offsetMilliseconds);
}


 It's better in that it now has the right dates, but only just barely- the endOfLastMonth now = Sun Mar 31 23:00:00 GMT+0100 2013. Good enough for my purpose, but still leaves a bad taste in my mouth. Anyone wanna help wash this out?