Saturday, November 28, 2009

ActionScript: Dates

I had to parse an RFC 2822 date today in ActionScript. I couldn't find any equivalent of strptime in order to allow me to parse dates in a variety of formats. It turns out that ActionScript can do it automatically:
new Date("Sat, 28 Nov 2009 10:15:02 +0000");
The Date constructor will attempt to guess at the correct format, and it'll raise an exception if it can't.

The next problem I had was that I was using Date.getTime() to return the number of milliseconds since midnight January 1, 1970, universal time. Even though getUTCYear() said that my Date object was for 2009, I kept getting a value somewhere around January 12, 1970. The value returned by getTime() was just too small!

I finally figured out that getTime() returns a Number, not an int. If you store the value in an int, you'll be completely hosed, and there aren't even any warnings to help you out. It's obvious in retrospect--a 32-bit int can't possibly hold the number of milliseconds since 1970 given a date in the year 2009.

No comments: