3

我想知道 Flash/AS3 是否有任何好方法将 AS3 'Date' 对象转换为/从 rfc-850 时间戳格式(如 HTTP 日期和上次修改所使用)。

这个问题与关于 rfc 3339的问题非常相似,除了它特定于 AS3 和 rfc-850。

RFC-850 就像:Thu, 09 Oct 2008 01:09:43 GMT

4

2 回答 2

3

好的,这里有几个函数可以Date在 Flash 中进行 RFC-802/ 转换。

我了解到该Date对象实际上并没有任何时区概念,并假设它位于本地时区。如果您将 RFC-802 日期传递给Date()构造函数,它会解析除末尾的“GMT”时区标记之外的所有内容,从而生成正确的时间但可能位于错误的时区。

从解析的 Date 中减去当前时区可以弥补这一点,因此时间戳可以与这些函数进行往返,而不会完全出错。

(如果有人在设计课程时包含了一个属性,那不是很好吗?)timezoneDate

/**
 * 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)))));
于 2008-10-09T06:05:22.940 回答
2

as3corelib库具有DateUtil.toRFC822 () 和 DateUtil.parseRFC822() 方法(等等)。不知道这些是否正是您正在寻找的。

DateUtil 类的具体文档在这里:http ://as3corelib.googlecode.com/svn/trunk/docs/com/adobe/utils/DateUtil.html

于 2008-10-09T03:40:14.027 回答