【问题标题】:Is there a function to convert a Twitter API created_at date into the correct format for ColdFusion 8+?是否有将 Twitter API created_at 日期转换为 ColdFusion 8+ 的正确格式的功能?
【发布时间】:2010-12-10 17:27:26
【问题描述】:

Twitter API 使用以下格式的 JSON 格式从 api.twitter.com 吐回日期(created_at):

“Fri Dec 10 17:12:00 +0000 2010”(

ColdFusion 9 似乎不喜欢这种格式并给出错误。

我尝试了各种内置的 ColdFusion 日期功能,但无济于事。而且我在 cflib.org 上找不到任何有用的东西。那么,有人已经有这个功能了吗?

【问题讨论】:

    标签: date coldfusion twitter


    【解决方案1】:

    您可以轻松地将该字符串视为以空格分隔的列表并组成更友好的字符串。由于数组比列表快,所以我会尽可能快地将它放入数组中,然后从那里开始工作。

    public string function getSaneTwitterDate(strDateIn) output="false"{
      var arrOrigDate = listToArray(strDateIn, ' ');
      var strNewDate = arrOrigDate[2] & ' ' & arrOrigDate[3] & ' ' & arrOrigDate[6];
      return dateFormat(strNewDate, 'yyyy-mm-dd');
    }
    

    这不考虑时间偏移或包含时间信息,但很容易添加。

    【讨论】:

    • 这么简单:D 我稍微修改了一下,在返回值的末尾附加了“& ' ' & arrOrigDate[4]”,这样它也包含了时间。 . 谢谢亚当!
    • 更新它以正确解析来自搜索 API public string function getSaneTwitterDate(strDateIn) output="false"{ var arrOrigDate = listToArray(arguments.strDateIn, ' '); var strNewDate = arrOrigDate[2] & ' ' & arrOrigDate[3] & ' '; // from api.twitter.com if ( IsNumeric(arrOrigDate[3]) ) { strNewDate &= arrOrigDate[6]; return dateFormat(strNewDate, 'yyyy-mm-dd') & ' ' & arrOrigDate[4]; // from search.twitter.com } else { strNewDate &= arrOrigDate[4]; return dateFormat(strNewDate, 'yyyy-mm-dd') & ' ' & arrOrigDate[5]; } }的 Twitter 日期
    【解决方案2】:

    试试http://pastebin.com/GuXu8Dy1

    <cfscript>
    function twitterDate(date,offset) {
        var retDate = listtoarray(date, " ");
        var thisDay = retDate[1];
        var thisMonth = retDate[2];
        var thisDate = retDate[3];
        var thisTime = timeformat(retDate[4], "h:mm tt");
        var thisYear = retDate[6];
        var thisReturn = "";
        var thisFormat = "#thisMonth#, #thisDate# #thisYear#";
    
        thisFormat = dateformat(thisFormat, "m/d/yy") & " " & thisTime;
        thisFormat = dateadd("s", offset, thisFormat);
        thisFormat = dateadd("h", 1, thisFormat);
    
        longFormat = dateformat(thisFormat, "yyyy-mm-dd") & " " & timeformat(thisFormat, "HH:mm:ss");
    
        thisReturn = longFormat;
        return thisReturn;
    }
    </cfscript>
    

    【讨论】:

    • 认真的吗?您不必费心将代码复制并粘贴到您的答案中吗?
    • 自从我在推特上回答他之后,它就已经在 pastebin 中了。很抱歉试图帮助其他可能在未来寻找并来到这里的人。
    • 关键是,linkrot 发生了。如果由于某种原因该链接更改或消失,则此答案绝对具有 no 值。最好在目的地复制(或摘录)信息并通过链接获取信息。
    • 为什么函数中有偏移量参数?
    【解决方案3】:
    <cffunction name="parseTwitterDateFormat" output="false" returntype="String" hint="I return a date in a useable date format.">
        <cfargument name="twitterDate" required="true" type="string" hint="The Twitter date." />
    
        <cfset var formatter = CreateObject("java", "java.text.SimpleDateFormat").init("EEE MMM d kk:mm:ss Z yyyy") />
        <cfset formatter.setLenient(true) />
    
        <cfreturn formatter.parse(arguments.twitterDate) />
    </cffunction>
    

    感谢 Matt Gifford 的 monkeyTweet 库 https://github.com/coldfumonkeh/monkehTweets

    【讨论】:

    • setLenient(true) 最酷的地方在于,它会导致格式化程序自动将无效日期(如“2010 年 2 月 30 日”)调整为有效日期,即“2010 年 3 月 2 日”。如果您想要更严格的解析,只需将其设置为false
    猜你喜欢
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    相关资源
    最近更新 更多