7

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)#">
Michael
  • 1,643
  • 1
  • 15
  • 31
  • 1
    Can you post a repro case we can copy paste to recreate the issue? – Matt Busche Dec 20 '14 at 16:29
  • No, we cannot possibly comment one way or the other without seeing a repro case that demonstrates what you're seeing. Pls read this & the linked to docs: http://blog.adamcameron.me/2013/09/short-self-contained-correct-compilable.html – Adam Cameron Dec 20 '14 at 16:34
  • 4
    I suspect you are in a TZ that is UTC-6? If you google the CF bugbase (https://www.google.co.uk/search?q=bugbase.adobe.com+dateConvert) there's been a bunch of issues with dateConvert(), which seem to be along the same times of what you're seeing. Adobe claim to have fixed them, but I would never be so sure with them. I suspect your first date is in your local TZ, and the second one is in UTC, so you're getting that offset reflected in the dateAdd() results. As to why you're not seeing the TZ diff in ``? Dunno. Doesn't surprise me though. – Adam Cameron Dec 20 '14 at 17:03
  • I have added some code which when run on my server causes the issue – Michael Dec 20 '14 at 17:13
  • 1
    Thanks Adam. It seems to be related to the issue at https://bugbase.adobe.com/index.cfm?event=bug&id=3338974. If I do `` the problem goes away. – Michael Dec 20 '14 at 17:17
  • One of you guys should post the bug (and possible work around) as an answer :) – Leigh Dec 20 '14 at 20:24
  • @Michael why don't you post the answer: you were the one who sorted out the final details. Also then we can upvote it and get you some more rep points. – Adam Cameron Dec 20 '14 at 21:32

2 Answers2

6

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>
Michael
  • 1,643
  • 1
  • 15
  • 31
0

Another fix for this is to use parseDateTime.

<cfset localDate = now()>
<cfset utcDate = DateConvert("local2utc", localDate)> 
<cfset utcfix = parseDateTime(utcDate)> 

utc epoch: #DateDiff("s", "January 1 1970 00:00", utcfix)#<br>
loc epoch: #DateDiff("s", "January 1 1970 00:00", localDate)#
bob
  • 1