• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

DateConvert() vs DateAdd() inconsistency question

Community Beginner ,
Dec 30, 2015 Dec 30, 2015

Copy link to clipboard

Copied

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

TOPICS
Advanced techniques

Views

759

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 03, 2016 Jan 03, 2016

Copy link to clipboard

Copied

LATEST

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation