ColdFusion 在相同的代码中循环到看似不同的时间 [英] ColdFusion Loops to Seemingly Different Times in Same Code

查看:10
本文介绍了ColdFusion 在相同的代码中循环到看似不同的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下循环:

<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>

当我的输入从:9:00 AM 到:8:00 PM 时,代码从上午 9:00 到晚上 7:30 每 30 分钟增量输出一次.

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.

当我的输入从:10:00 AM 到:1:00 PM 时,代码从上午 10:00 到下午 1:00 每 30 分钟增量输出一次.

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.

谁能解释发生了什么以及为什么第一个循环中缺少一个 30 分钟的片段而不是第二个?有人告诉我这是一种有效的循环时间方法,但我开始认为,因为时间在底层 Java 方法中呈现为浮点数,所以会发生一些舍入并且搞砸了.

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.

我真的认为我不传递日期/时间对象并不重要.CF 是在幕后进行的,否则整个事情根本就行不通.

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.

推荐答案

这将是因为在幕后,所有 CF 所做的就是将日期转换为双精度(或者在您的情况下:字符串到日期到双精度),和晚上 8 点(1 天的 20/24 日)将出现舍入误差,使其在预期的上限范围内;而下午 1 点(1 天的 13/24 日)稍微超出预期的上限.

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.

这篇关于ColdFusion 在相同的代码中循环到看似不同的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆