Reputation: 7336
I'd like to know if Flash/AS3 has any nice way to convert an AS3 'Date' object to/from rfc-850 timestamp format (as used by HTTP date and last-modified).
This question is very similar to this question about rfc 3339, except it's specific to AS3 and rfc-850.
RFC-850 is like: Thu, 09 Oct 2008 01:09:43 GMT
Upvotes: 3
Views: 6316
Reputation: 7336
Alright, so here's a couple of functions to do RFC-802/Date
conversion in Flash.
I learned that the Date
object doesn't really have any notion of a timezone, and assumes that it is in the local timezone. If you pass an RFC-802 date to the Date()
constructor, it parses it everything except the "GMT" timezone token at the end, resulting in the correct time but possibly in the wrong timezone.
Subtracting the current timezone from the parsed Date compensates for this, so a timestamp can do a round-trip with these functions without becoming completely wrong.
(Wouldn't it have been great if somebody had included a timezone
property when they were designing the Date
class?)
/**
* Converts an RFC string to a Date object.
*/
function fromRFC802(date:String):Date {
// Passing in an RFC802 date to the Date constructor causes flash
// to conveniently ignore the "GMT" timezone at the end, and assumes
// that it's in the Local timezone.
// If we additionally convert it back to GMT, then we're sweet.
var outputDate:Date = new Date(date);
outputDate = new Date(outputDate.time - outputDate.getTimezoneOffset()*1000*60);
return outputDate;
}
/**
* Converts a Date object to an RFC802-formatted string (GMT/UTC).
*/
function toRFC802 (date:Date):String {
// example: Thu, 09 Oct 2008 01:09:43 GMT
// Convert to GMT
var output:String = "";
// Day
switch (date.dayUTC) {
case 0: output += "Sun"; break;
case 1: output += "Mon"; break;
case 2: output += "Tue"; break;
case 3: output += "Wed"; break;
case 4: output += "Thu"; break;
case 5: output += "Fri"; break;
case 6: output += "Sat"; break;
}
output += ", ";
// Date
if (date.dateUTC < 10) {
output += "0"; // leading zero
}
output += date.dateUTC + " ";
// Month
switch(date.month) {
case 0: output += "Jan"; break;
case 1: output += "Feb"; break;
case 2: output += "Mar"; break;
case 3: output += "Apr"; break;
case 4: output += "May"; break;
case 5: output += "Jun"; break;
case 6: output += "Jul"; break;
case 7: output += "Aug"; break;
case 8: output += "Sep"; break;
case 9: output += "Oct"; break;
case 10: output += "Nov"; break;
case 11: output += "Dec"; break;
}
output += " ";
// Year
output += date.fullYearUTC + " ";
// Hours
if (date.hoursUTC < 10) {
output += "0"; // leading zero
}
output += date.hoursUTC + ":";
// Minutes
if (date.minutesUTC < 10) {
output += "0"; // leading zero
}
output += date.minutesUTC + ":";
// Seconds
if (date.seconds < 10) {
output += "0"; // leading zero
}
output += date.secondsUTC + " GMT";
return output;
}
var dateString:String = "Thu, 09 Oct 2008 01:09:43 GMT";
trace("Round trip proof:");
trace(" RFC-802: " + dateString);
trace("Date obj: " + fromRFC802(dateString));
trace(" RFC-802: " + toRFC802(fromRFC802(dateString)));
trace("Date obj: " + fromRFC802(toRFC802(fromRFC802(dateString))));
trace(" RFC-802: " + toRFC802(fromRFC802(toRFC802(fromRFC802(dateString)))));
Upvotes: 3
Reputation: 8393
The as3corelib libraries have DateUtil.toRFC822() and DateUtil.parseRFC822() methods (among others). Don't know if these are exactly what you are looking for.
The specific docs for the DateUtil class is here: http://as3corelib.googlecode.com/svn/trunk/docs/com/adobe/utils/DateUtil.html
Upvotes: 2