我想知道Flash / AS3是否有任何很好的方法将AS3'Date'对象转换为rfc-850时间戳格式(由HTTP日期和最后修改时使用)。
此问题与this question about rfc 3339非常相似,只是它特定于AS3和rfc-850。
RFC-850就像:Thu, 09 Oct 2008 01:09:43 GMT
答案 0 :(得分:3)
好的,所以这里有一些在Flash中进行RFC-802 / Date
转换的功能。
我了解到Date
对象实际上没有任何时区概念,并假设它位于本地时区。如果您将RFC-802日期传递给Date()
构造函数,它会在结尾处解析除“GMT”时区标记之外的所有内容,从而产生正确的时间,但可能在错误的时区。
从解析的日期中减去当前时区会对此进行补偿,因此时间戳可以使用这些函数进行往返,而不会完全错误。
(如果某人在设计timezone
类时已包含Date
属性,那么 是不是 <) / p>
/**
* 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)))));
答案 1 :(得分:2)
as3corelib库具有DateUtil.toRFC822()和DateUtil.parseRFC822()方法(以及其他方法)。不知道这些是否正是您正在寻找的。
DateUtil类的特定文档位于:http://as3corelib.googlecode.com/svn/trunk/docs/com/adobe/utils/DateUtil.html