Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ColdFusion Loops to Seemingly Different Times in Same Code

Consider the following loop:

<cfloop from="#timeFormat(roomAvail.startTime,'short')#" to="#timeFormat(roomAvail.endTime,'short')#" step="#CreateTimeSpan(0,0,30,0)#" index="time">
    <cfoutput>
        #timeFormat(time,'short')#<br/>
    </cfoutput>
</cfloop>

When my input is from:9:00 AM to:8:00 PM the code outputs every 30 min increment from 9:00 AM to 7:30 PM.

When my input is from:10:00 AM to:1:00 PM the code outputs every 30 min increment form 10:00 AM to 1:00 PM.

Can anyone explain what is happening and why there is one 30 min segment missing from the first loop and not the second? I was told this was a valid method of looping over times, but I'm starting to think that because the time is rendered as a float in the underlying Java method, some rounding is occurring and it's screwing up.

Edit: I really don't think it matters that I'm not passing in Date/Time objects. CF is casting that behind the scenes, otherwise the entire thing wouldn't work at all.

like image 490
Leeish Avatar asked Dec 07 '25 21:12

Leeish


1 Answers

It'll be because under the hood, all CF is doing is converting the Dates to Doubles (or in your case: the Strings to Dates to Doubles), and 8pm (20/24ths of 1 day) will have a rounding error which leaves it within the intended upper boundary; whereas 1pm (13/24ths of 1 day) is slighty outside the intended upper boundary.

It probably doesn't help that you're passing a string when the loop is expecting dates.

like image 184
Adam Cameron Avatar answered Dec 09 '25 20:12

Adam Cameron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!