Skip to main content
Known Participant
December 30, 2015
Question

DateConvert() vs DateAdd() inconsistency question

  • December 30, 2015
  • 1 reply
  • 831 views

I am working on some Date Helper functions where I need to convert UTC dates to unixtimestamps and vice versa. Interestingly I came across an inconsistency using the DateConvert function in cooperation with DateDiff in that the timestamps are different. DateConvert makes the timestamp using local time while DateAdd uses the appropriate UTC Date. I was wondering if this is a bug / anomaly or my misunderstanding how DateConvert works in ColdFusion.

The following is syntax to reproduce scenario

<cfset unixEpoch = CreateDate(1970,1,1) />

<cfoutput>

    <pre>

    Local Date:        #Now()# [CORRECT]

    UTC Date:          #DateConvert("local2Utc", now())# [CORRECT]

    =======================================================

    UTC to Unix Timestamp Conversion

    =======================================================

    UTX (DateConvert): #DateDiff("s", unixEpoch, DateConvert("local2Utc", now()))# [WRONG]

    UTX (DateAdd):     #DateDiff("s", unixEpoch, DateAdd( "s", GetTimeZoneInfo().UTCTotalOffset, now() ) )# [CORRECT]

    </pre>

</cfoutput>

The following is what the above unit of code output last time I ran it

Local Date: {ts '2015-12-30 10:44:25'} [CORRECT]

UTC Date: {ts '2015-12-30 18:44:25'} [CORRECT]

=======================================================

UTC to Unix Timestamp Conversion

=======================================================

UTX (DateConvert): 1451472265 [WRONG]

UTX (DateAdd): 1451501065 [CORRECT]

*** Use http://www.unixtimestamp.com/index.php to confirm unix timestamp discrepancies

Any thoughts would certainly be helpful -- Thank you

This topic has been closed for replies.

1 reply

BKBK
Community Expert
Community Expert
January 3, 2016

I think you have stumbled on a bug. I have isolated it as follows:

<cfset utcDate1 = dateConvert("local2Utc", now()) />

<cfset utcDate2 = dateAdd( "s", getTimeZoneInfo().UTCTotalOffset, now()) />

    <cfoutput>

        <pre>

        UTC Date:                #utcDate1#

       

        Alternative UTC Date:      #utcDate2#

       

        Difference in UTC dates: #dateDiff("s", utcDate1, utcDate2)# seconds

        </pre>

    </cfoutput>