Highlighted

DateCompare returning incorrect result after moving to CF11 from CF9

Explorer ,
Jun 20, 2018

Copy link to clipboard

Copied

I have a page which has been the same since CF7. Recently we moved our application to CF11 to support TLS1.2 and I've since noticed that this one particular bit of code has stopped functioning correctly and it's lead me down a weird road.

This is the following code block. It's meant to create date/time ranges for conditional statements and compare the current time against those ranges, performing a certain task.

<!--- Set date --->

<cfset thisDate = getDate()>

<!--- Determine what flight they should see. --->

<!--- If it's after 9am and before 10pm --->

<cfif thisDate GTE CreateDateTime(DatePart("yyyy",thisDate),DatePart("m",thisDate),DatePart("d",thisDate),9,0,0)

AND

thisDate LT CreateDateTime(DatePart("yyyy",thisDate),DatePart("m",thisDate),DatePart("d",thisDate),21,0,0)>

<cfset d1 = "#DatePart('m',thisDate)#/#DatePart('d',thisDate)#/#DatePart('yyyy',thisDate)# 9:00:00">

<cfset d2 = "#DatePart('m',thisDate)#/#DatePart('d',thisDate)#/#DatePart('yyyy',thisDate)# 21:00:00">

<!--- If it's after 10pm --->

<cfelseif thisDate GTE CreateDateTime(DatePart("yyyy",thisDate),DatePart("m",thisDate),DatePart("d",thisDate),21,0,0)>

<cfset d1 = "#DatePart('m',thisDate)#/#DatePart('d',thisDate)#/#DatePart('yyyy',thisDate)# 21:00:00">

<cfset d2 = "#DatePart('m',thisDate)#/#DatePart('d',thisDate)#/#DatePart('yyyy',thisDate)# 9:00:00">

<cfset d2 = DateAdd("d",1,d2)>

<!--- If it's before 9am --->

<cfelse>

<cfset d1 = "#DatePart('m',thisDate)#/#DatePart('d',thisDate)#/#DatePart('yyyy',thisDate)# 21:00:00">

<cfset d1 = DateAdd("d",-1,d1)>

<cfset d2 = "#DatePart('m',thisDate)#/#DatePart('d',thisDate)#/#DatePart('yyyy',thisDate)# 9:00:00">

</cfif>

The above code has always functioned correct, however since the move I've noticed it's no longer evaluating the current date/time as falling within the correct ranges. This morning at 11:15am I checked and it was not firing the first if statement, but instead firing the else statement. Meaning it thought it was before 9:00am.

I assumed this meant that perhaps in CF11 I'm not able to just evaluate the times using LTE and GTE. So I thought instead that I would test using DateCompare. So I wrote the following:

<cfset thisDate = getDate()>

<cfset date1 = CreateODBCDateTime(thisDate)>

<cfset date2 = CreateODBCDateTime(CreateDateTime(DatePart("yyyy",thisDate),DatePart("m",thisDate),DatePart("d",thisDate),9,0,0))>

<cfoutput>

date1: #date1#<br />

date2: #date2#<br />

<br />

DateCompare: #DateCompare(date1,date2)#<br />

GTE Evaluation: #date1 GTE date2#<br />

</cfoutput>

Now this is where it gets weird. The output from the above is:

date1: {ts '2018-06-20 15:12:00'}

date2: {ts '2018-06-20 09:00:00'}

DateCompare: -1

GTE Evaluation: NO

The date compare should output "1" and the GTE evaluation should output "YES" since they are the same day and date1 is 3pm and date2 is 9am.

Can anyone help me out here? This is mind boggling for me.

Thanks so much,

Mike

It's probably a transcription error, but there is no GetDate() function in CF, you probably mean Now() right?

Also, your comments talk about 10PM, but 21:00 is 9PM.

It's also worth pointing out that the values you assign to d1 and d2 are strings until you do some date arithmetic on one or the other. This might be causing you problems later. Try to stick with date types throughout.

Other than that, the code works as expected for me on CF 11.

Add the following line at the end of your example code to see all your variables:

<cfdump var="#variables#" showudfs="false" >

Cheers

Eddie

Views

855

Likes

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

DateCompare returning incorrect result after moving to CF11 from CF9

Explorer ,
Jun 20, 2018

Copy link to clipboard

Copied

I have a page which has been the same since CF7. Recently we moved our application to CF11 to support TLS1.2 and I've since noticed that this one particular bit of code has stopped functioning correctly and it's lead me down a weird road.

This is the following code block. It's meant to create date/time ranges for conditional statements and compare the current time against those ranges, performing a certain task.

<!--- Set date --->

<cfset thisDate = getDate()>

<!--- Determine what flight they should see. --->

<!--- If it's after 9am and before 10pm --->

<cfif thisDate GTE CreateDateTime(DatePart("yyyy",thisDate),DatePart("m",thisDate),DatePart("d",thisDate),9,0,0)

AND

thisDate LT CreateDateTime(DatePart("yyyy",thisDate),DatePart("m",thisDate),DatePart("d",thisDate),21,0,0)>

<cfset d1 = "#DatePart('m',thisDate)#/#DatePart('d',thisDate)#/#DatePart('yyyy',thisDate)# 9:00:00">

<cfset d2 = "#DatePart('m',thisDate)#/#DatePart('d',thisDate)#/#DatePart('yyyy',thisDate)# 21:00:00">

<!--- If it's after 10pm --->

<cfelseif thisDate GTE CreateDateTime(DatePart("yyyy",thisDate),DatePart("m",thisDate),DatePart("d",thisDate),21,0,0)>

<cfset d1 = "#DatePart('m',thisDate)#/#DatePart('d',thisDate)#/#DatePart('yyyy',thisDate)# 21:00:00">

<cfset d2 = "#DatePart('m',thisDate)#/#DatePart('d',thisDate)#/#DatePart('yyyy',thisDate)# 9:00:00">

<cfset d2 = DateAdd("d",1,d2)>

<!--- If it's before 9am --->

<cfelse>

<cfset d1 = "#DatePart('m',thisDate)#/#DatePart('d',thisDate)#/#DatePart('yyyy',thisDate)# 21:00:00">

<cfset d1 = DateAdd("d",-1,d1)>

<cfset d2 = "#DatePart('m',thisDate)#/#DatePart('d',thisDate)#/#DatePart('yyyy',thisDate)# 9:00:00">

</cfif>

The above code has always functioned correct, however since the move I've noticed it's no longer evaluating the current date/time as falling within the correct ranges. This morning at 11:15am I checked and it was not firing the first if statement, but instead firing the else statement. Meaning it thought it was before 9:00am.

I assumed this meant that perhaps in CF11 I'm not able to just evaluate the times using LTE and GTE. So I thought instead that I would test using DateCompare. So I wrote the following:

<cfset thisDate = getDate()>

<cfset date1 = CreateODBCDateTime(thisDate)>

<cfset date2 = CreateODBCDateTime(CreateDateTime(DatePart("yyyy",thisDate),DatePart("m",thisDate),DatePart("d",thisDate),9,0,0))>

<cfoutput>

date1: #date1#<br />

date2: #date2#<br />

<br />

DateCompare: #DateCompare(date1,date2)#<br />

GTE Evaluation: #date1 GTE date2#<br />

</cfoutput>

Now this is where it gets weird. The output from the above is:

date1: {ts '2018-06-20 15:12:00'}

date2: {ts '2018-06-20 09:00:00'}

DateCompare: -1

GTE Evaluation: NO

The date compare should output "1" and the GTE evaluation should output "YES" since they are the same day and date1 is 3pm and date2 is 9am.

Can anyone help me out here? This is mind boggling for me.

Thanks so much,

Mike

It's probably a transcription error, but there is no GetDate() function in CF, you probably mean Now() right?

Also, your comments talk about 10PM, but 21:00 is 9PM.

It's also worth pointing out that the values you assign to d1 and d2 are strings until you do some date arithmetic on one or the other. This might be causing you problems later. Try to stick with date types throughout.

Other than that, the code works as expected for me on CF 11.

Add the following line at the end of your example code to see all your variables:

<cfdump var="#variables#" showudfs="false" >

Cheers

Eddie

Views

856

Likes

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
Jun 20, 2018 0
Advocate ,
Jun 20, 2018

Copy link to clipboard

Copied

It's probably a transcription error, but there is no GetDate() function in CF, you probably mean Now() right?

Also, your comments talk about 10PM, but 21:00 is 9PM.

It's also worth pointing out that the values you assign to d1 and d2 are strings until you do some date arithmetic on one or the other. This might be causing you problems later. Try to stick with date types throughout.

Other than that, the code works as expected for me on CF 11.

Add the following line at the end of your example code to see all your variables:

<cfdump var="#variables#" showudfs="false" >

Cheers

Eddie

Likes

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
Reply
Loading...
Jun 20, 2018 0
Explorer ,
Jun 21, 2018

Copy link to clipboard

Copied

Hi Eddie,

Thanks for the reply. The getDate() function is a function I created to adjust for the timezone difference between where the server is located and what the local time is, within the organization.

<cfscript>

function getDate() {

      return DateAdd('h',(application.GMTOffset*-1),DateConvert("local2utc", Now()));

}

</cfscript>

Yes, sorry, I do actually mean 9am and 9pm, not 10pm.

I wrapped the times ParseDateTime() and now it's working as expected. That's a bit of a bummer that it functions differently.

Thanks for much for the help.

Likes

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
Reply
Loading...
Jun 21, 2018 0
Advocate ,
Jun 21, 2018

Copy link to clipboard

Copied

I'm glad you got it sorted out, but I'm not clear which function you say is working differently. If you have the time, would you mind elaborating?

Cheers

Eddie

Likes

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
Reply
Loading...
Jun 21, 2018 0
Explorer ,
Jun 21, 2018

Copy link to clipboard

Copied

Absolutely. On CF9 the following code piece would evaluate as true at 9:30 EST.

<!--- Set date --->

<cfset thisDate = getDate()>

<!--- Determine what flight they should see. --->

<!--- If it's after 9am and before 9pm --->

<cfif thisDate GTE CreateDateTime(DatePart("yyyy",thisDate),DatePart("m",thisDate),DatePart("d",thisDate),9, 0,0)

AND

thisDate LT CreateDateTime(DatePart("yyyy",thisDate),DatePart("m",thisDate),DatePart("d",thisDate),21 ,0,0)>

Now, on CF11 that code evaluates as false. I changed the code to below and it now evaluates as true at 9:30 EST:

<!--- Set date object --->

<cfset thisDate = getDate()>

<!--- Determine what flight they should see. --->

<!--- If it's after 9am and before 9pm --->

<cfif ParseDateTime(thisDate) GTE ParseDateTime(CreateDateTime(DatePart("yyyy",thisDate),DatePart("m",thisDate),DatePart("d",thisDate),9,0,0))

AND

ParseDateTime(thisDate) LT ParseDateTime(CreateDateTime(DatePart("yyyy",thisDate),DatePart("m",thisDate),DatePart("d",thisDate),21,0,0))>

On CF9 it evaluated the string date correctly as a date/time object, whereas it now seems like I need to parse it into a date/time object to get the evaluation to work correctly.

The code was actually built in 2005, on either CF6 or CF7, I can't remember it was so long ago. However it's worked reliably for 13 straight years, and is a function that our pilots use on a daily basis without complaint. And for whatever reason since moving to CF11 a few days ago all the pilots have complained that it was no longer functioning as expected.

I hope that helps. I'm happy to answer any other questions you might have.

Warm Regards,

Mike

Likes

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
Reply
Loading...
Jun 21, 2018 0
Advocate ,
Jun 21, 2018

Copy link to clipboard

Copied

The reason I am persisting in the investigation is because I don't think the problem is where you think it is. Your getDate() function is returning a date object, not a date string, so the ParseDateTime() function should not be necessary.

Please add (temporarily) the line that I suggested in my original reply and post the results for thisDate, d1 and d2.

That way we can see visually whether or not d1 and d2 have the correct values based on what is actually in thisDate.

Cheers

Eddie

Likes

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
Reply
Loading...
Jun 21, 2018 0
Explorer ,
Jun 21, 2018

Copy link to clipboard

Copied

No problem at all. So it's not really about the d1 and d2 values, it's about the if statements evaluating differently. Since I've already updated the code I've created a new test page with the following code:

<!--- Set date --->

<cfset thisDate = DateAdd('h',(application.GMTOffset*-1),DateConvert("local2utc", Now()))>

<!--- Determine what flight they should see. --->

<!--- If it's after 9am and before 9pm --->

<cfif thisDate GTE CreateDateTime(DatePart("yyyy",thisDate),DatePart("m",thisDate),DatePart("d",thisDate),9, 0,0)

AND

thisDate LT CreateDateTime(DatePart("yyyy",thisDate),DatePart("m",thisDate),DatePart("d",thisDate),21 ,0,0)>

<cfset d1 = "#DatePart('m',thisDate)#/#DatePart('d',thisDate)#/#DatePart('yyyy',thisDate)# 9:00:00">

<cfset d2 = "#DatePart('m',thisDate)#/#DatePart('d',thisDate)#/#DatePart('yyyy',thisDate)# 21:00:00">

<!--- If it's after 9pm --->

<cfelseif thisDate GTE CreateDateTime(DatePart("yyyy",thisDate),DatePart("m",thisDate),DatePart("d",thisDate),21 ,0,0)>

<cfset d1 = "#DatePart('m',thisDate)#/#DatePart('d',thisDate)#/#DatePart('yyyy',thisDate)# 21:00:00">

<cfset d2 = "#DatePart('m',thisDate)#/#DatePart('d',thisDate)#/#DatePart('yyyy',thisDate)# 9:00:00">

<cfset d2 = DateAdd("d",1,d2)>

<!--- If it's before 9am --->

<cfelse>

<cfset d1 = "#DatePart('m',thisDate)#/#DatePart('d',thisDate)#/#DatePart('yyyy',thisDate)# 21:00:00">

<cfset d1 = DateAdd("d",-1,d1)>

<cfset d2 = "#DatePart('m',thisDate)#/#DatePart('d',thisDate)#/#DatePart('yyyy',thisDate)# 9:00:00">

</cfif>

<cfdump var="#variables#" showudfs="false" >

<cfoutput>

Is it later than 9am? #thisDate GTE CreateDateTime(DatePart("yyyy",thisDate),DatePart("m",thisDate),DatePart("d",thisDate),9, 0,0)#<br />

Is it earlier than 9pm? #thisDate LT CreateDateTime(DatePart("yyyy",thisDate),DatePart("m",thisDate),DatePart("d",thisDate),21 ,0,0)#

</cfoutput>

And the output is:

struct
ADMINPATHNAME/
AUTHPATHD:\Inetpub\armis\auth
BASEPATHD:\Inetpub\
D1{ts '2018-06-20 21:00:00'}
D26/21/2018 9:00:00
DOGMTSETfalse
DOREFRESHfalse
HARDPATHD:\Inetpub\armis
THISDATE{ts '2018-06-21 10:52:51'}
THISPATHD:\Inetpub\armis

Is it later than 9am? NO

Is it earlier than 9pm? YES

The problem here is with the "Is it later than 9am" evaluation. I realize that D1 and D2 in the example don't both look like date/time objects, and I've added code to fix that. However, it's the evaluation bit that has caused the issue. In CF9 (and the versions before) it evaluated correctly, but for some reason in CF11 "thisDate" doesn't evaluate properly in the first IF statement.

Cheers,

Mike

Likes

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
Reply
Loading...
Jun 21, 2018 0
Advocate ,
Jun 21, 2018

Copy link to clipboard

Copied

Holy smokes! I was setting thisDate to various dates to test the logic and they all worked out. When I set thisDate using your example the logic fails!

I have narrowed the problem down to the DateConvert() function. Although the value displayed looks correct, internally the date object seems to be corrupt which is causing problems during comparisons.

This is a reproducible bug and I urge you to log it with Adobe.

As a work-around, change the line

<cfset thisDate = DateAdd('h',(application.GMTOffset*-1),DateConvert("local2utc", Now()))>

to

<cfset thisDate = dateTimeFormat(DateConvert("local2utc", Now()))>

<cfset thisDate = DateAdd('h',(application.GMTOffset*-1),thisDate)>

Basically, I'm using dateTimeFormat() to convert DateConvert() to a string and then DateAdd() converts it to a valid date object which compares correctly in your logic statements.

Cheers

Eddie

Likes

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
Reply
Loading...
Jun 21, 2018 0
Explorer ,
Jun 21, 2018

Copy link to clipboard

Copied

Interesting. I actually have no idea how to log a bug with Adobe. Any help would be appreciated there.

The way I tackled it was this:

ParseDateTime(DateAdd('h',(application.GMTOffset*-1),DateConvert("local2utc", Now())));

Likes

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
Reply
Loading...
Jun 21, 2018 0
Advocate ,
Jun 21, 2018

Copy link to clipboard

Copied

Go to Tracker  and click on "Add Bug" at the top of the page. You can include a link to this conversation in your description.

Thanks.

Cheers

Eddie

Likes

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
Reply
Loading...
Jun 21, 2018 0
Explorer ,
Jun 21, 2018

Copy link to clipboard

Copied

Great, thanks.

Bug ID: CF-4202965

Likes

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
Reply
Loading...
Jun 21, 2018 0
Most Valuable Participant ,
Jun 25, 2018

Copy link to clipboard

Copied

Hi Mike,

Could you please run the following in CF9 and report the output?

<cfscript>

  time1 = time2 = createDateTime(2018,1,2,3,4,5);

  time2 = dateConvert("local2utc", time2);

  writeOutput(SERVER.ColdFusion.productVersion & '<br>');

  writeOutput(time1+0 & '<br>' & time2+0 & '<br>');

  writeOutput(dateTimeFormat(time1, "yyyy-mm-dd HH:nn:ss z") & '<br>');

  writeOutput(dateTimeFormat(time2, "yyyy-mm-dd HH:nn:ss z") & '<br>');

  writeOutput(time1 is time2);

</cfscript>

Thanks!,

-Aaron

Likes

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
Reply
Loading...
Jun 25, 2018 0
Explorer ,
Jun 26, 2018

Copy link to clipboard

Copied

Unfortunately the codebase is so longer active on CF9. Only CF11.

Mike

On Mon, Jun 25, 2018 at 11:59 PM itisdesign <forums_noreply@adobe.com>

Likes

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
Reply
Loading...
Jun 26, 2018 0