开发者

Is there a function to convert a Twitter API created_at date into the correct format for ColdFusion 8+?

The Twitter API spits back dates (created_at) from api.twitter.com with JSON format in the following format:

"Fri Dec 10 17:12:00 开发者_Go百科+0000 2010" (<-- notice the year is at the end)

ColdFusion 9 doesn't seem to like this format and gives an error.

I tried various built-in ColdFusion date functions, to no avail. And I couldn't find anything useful on cflib.org. So, does anyone have a function for this already?


You could easily treat that string as a space-delimited list and compose a more friendly string. Since Arrays are faster than lists, I'll get it into an array as fast as possible and then work from that.

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');
}

This doesn't account for the time offset or include time information, but it would be easy to add.


try 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>


<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>

Credit goes to Matt Gifford's monkeyTweet library https://github.com/coldfumonkeh/monkehTweets

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜