Wednesday, August 11, 2010

CFML Function - createDatetimeFromHttpTimeString()

Working with HTTP time strings should be easier to CFML / ColdFusion,
but there is no built-in function that will create a CFML date/time from
a UTC HTTP time string. So here is the UDF that can help you work with
HTTP time string in native CFML date/time functions. I didn't check
cflib.org first, but I really couldn't check it out of professional
courtesy because I was writing this function for open source project and
I didn't want to deal with attribution / license stuff (yes, I'm lazy).





<cffunction name="createDatetimeFromHttpTimeString" access="public" returntype="date" output="false"
    hint="Creates an UTC datetime from an HTTP time string.">
    <cfargument name="httpTimeString" type="string" required="true"
        hint="An HTTP time string in the format of '11 Aug 2010 17:58:48 GMT'." />

    <cfset var rawArray = ListToArray(ListLast(arguments.httpTimeString, ","), " ") />
    <cfset var rawTimePart = ListToArray(rawArray[4], ":") />
   
    <cfreturn CreateDatetime(rawArray[3], DateFormat("#rawArray[2]#/1/2000", "m"), rawArray[1], rawTimePart[1], rawTimePart[2], rawTimePart[3]) />
</cffunction>




Enjoy!

2 comments:

  1. parseDateTime('11 Aug 2010 17:58:48 GMT', "pop") works fine! Try it!

    ReplyDelete
  2. That doesn't work on all engines sorry (OpenBD, Railo and older version of Adobe CF).  Cross CFML engine compatibility is tricky. Comment to Posterous said the following on 08/11/2010 04:59 PM:

    ReplyDelete