Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dateDiff returning wrong value

I have a variable called date1 which contains a ColdFusion date/time object generated using parseDateTime. When I dump the variable I get {ts '2014-12-20 15:46:57'}.

I have another variable called date2 which contains another ColdFusion date/time object generated by dateConvert("local2utc",now()). When I dump the variable I get {ts '2014-12-20 15:49:40'}.

But when I do dateDiff("s",date1,date2) I get -21436 which is far too many seconds. Can anybody explain why this happening? I think it might be a time zone issue but I can't get my head around it.

Repro code

<cfset dtString = "Saturday, December 20, 2014 05:07:30 PM">

<cfset dtObject = parseDateTime(dtString)>

<cfdump var="#dtObject#">

<cfset utcNow = dateConvert("local2utc",now())>

<br><br><cfdump var="#utcNow#">

<br><br><cfdump var="#dateDiff("s",dtObject,utcNow)#">
like image 648
Michael Avatar asked Dec 20 '14 15:12

Michael


1 Answers

The cause of this issue seems to be due to the bug described at https://bugbase.adobe.com/index.cfm?event=bug&id=3338974.

As described at the above URL 'the variable returned from DateConvert("local2Utc",now()) seems to carry around the offset from local to UTC. When, you use that variable for just about anything (including cfqueryparam), the value you get back is off by the amount of the offset (i.e. back to the value you passed in)'.

A workaround seems to be to convert the date to a string after conversion. I have created a simple wrapper function for this as follows:

<cffunction name="local2utc" output="no">
    <cfargument name="date">

    <cfreturn dateConvert("local2utc",arguments.date).toString()>

</cffunction>
like image 93
Michael Avatar answered Oct 08 '22 11:10

Michael