Search code examples
datetimecoldfusioncfmlcoldfusion-2016

ColdFusion UTC TIME LTE Checks vs NonUTC Time


I'm lost I can do a check of Non UTC Times and things work. But when converting to UTC Times the CFIF doesn't work

NON UTC {ts '2019-11-10 14:59:46'} LTE {ts '2019-11-10 14:00:00'}

UTC if {ts '2019-11-10 21:59:46'} LTE {ts '2019-11-10 21:00:00'}

I'm lost... Can't seem to figure it out.

<cfset timenow = #Now()#>

<cfset utimenow = dateConvert("Local2UTC", timenow)>

<cfset admintime = #DateAdd("h", -1, chk.stime)#>
<cfset uadmintime = #DateAdd("h", -1, chk.utcact)#>

The chk.stime and chk.utc times are correct. basically it is taking an hr off time for the cancel window.

These are the time stamps created.

NON UTC {ts '2019-11-10 14:59:46'} LTE {ts '2019-11-10 14:00:00'}

The NonUTC Stamps are time stamps without the UTC Conversions.

UTC if {ts '2019-11-10 21:59:46'} LTE {ts '2019-11-10 21:00:00'} 

NonUTC Stamps

<cfif timenow LTE admintime>
This one works fine...
 NON UTC {ts '2019-11-10 14:59:46'} LTE {ts '2019-11-10 14:00:00'}
 Then allow cancel
<cfelse>
This cfelse is activated properly and Can't Cancel.
 Can't Cancel
</cfif>

UTC Stamps
<cfif utimenow LTE uadmintime>
This one does not work
 UTC if {ts '2019-11-10 21:59:46'} LTE {ts '2019-11-10 21:00:00'}
 Then allow cancel
 This UTC Time does not activate properly and allows the cancel.
 Executes/Activates inside the cfif - it should not
 <cfelse>
 Can't Cancel
</cfif>

I have also tried to convert to be sure of ODBCTime

<cfset uadmintime = createODBCDateTime(uadmintime)>

I ended up having to recreate the times, and compare that format. It now works with both the <cfif timenow LTE admintime> and the DateCompare as below. This must have been a formatting issue of not liking the {ts '2019-11-10 14:59:46'} LTE {ts '2019-11-10 14:00:00'} formatting.

<cfset nctime = '#dateformat(uadmintime, "dd-MM-yyyy")# #timeformat(uadmintime, "hh:mm:ss")#'>
<cfset nutctime = '#dateformat(utimenow, "dd-MM-yyyy")# #timeformat(utimenow, "hh:mm:ss")#'>

Solution

  • ColdFusion is a loosely typed language and can keep date/time values in various data types. Standard comparisons like eq, lte, etc compare variables of different types based on unknown and changing rules so can have unexpected results if CF decides to convert to a different datatype. There are times where you would expect a variable to be a date/time object when really it is a string that passes validates for a date. Different versions of CF, Lucee, etc could act differently or depend on the actual values involved as well.

    I recommend always using dateCompare() when comparing dates...

    https://cfdocs.org/datecompare

    <cfif utimenow LTE uadmintime>
        ...
    </cfif>
    

    becomes

    <cfif dateCompare(utimenow, uadmintime) lte 0>
        ...
    </cfif>
    

    Example code:

    https://cffiddle.org/app/file?filepath=e3d24147-8a25-44ff-a98d-5b1e686cd619/d6115113-eead-4214-bc94-13eb7f0456f5/efad9206-b082-4940-bf5d-f4c5001ae2ec.cfm