ColdFusion 在同一代码中循环到看似不同的时间
ColdFusion Loops to Seemingly Different Times in Same Code
考虑以下循环:
<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 AM 到 7:30 PM 每 30 分钟增量输出一次。
当我的输入是从:10:00 AM
到:1:00 PM
时,代码每 30 分钟增量输出一次 10:00 AM 到 1:00 PM。
谁能解释发生了什么以及为什么第一个循环中缺少一个 30 分钟的片段而不是第二个循环?有人告诉我这是一种有效的时间循环方法,但我开始认为因为时间在基础 Java 方法中呈现为浮点数,所以发生了一些舍入并且它搞砸了。
编辑:我真的认为我不传递 Date/Time 个对象并不重要。 CF在幕后投射,否则整个事情根本无法进行。
这是因为在幕后,CF 所做的只是将日期转换为双精度(或者在您的情况下:将字符串转换为日期再转换为双精度),晚上 8 点(1 天的 20/24)将有将其留在预期上限内的舍入误差;而下午 1 点(1 天的 13/24)稍微超出了预期的上限。
当循环需要日期时传递字符串可能无济于事。
timeFormat returns 一个字符串。您可以按照文档中的示例使用 createTime 来代替使用适当的时间对象吗?
https://wikidocs.adobe.com/wiki/display/coldfusionen/cfloop%3A+looping+over+a+date+or+time+range
<cfset startTime = CreateTime(9,0,0)>
<cfset endTime = CreateTime(20,0,0)>
<cfloop from="#startTime#" to="#endTime#" index="time" step="#CreateTimeSpan(0,0,30,0)#">
<cfoutput>#TimeFormat(time, "short")#<br /></cfoutput>
</cfloop>
<cfset startTime = CreateTime(10,0,0)>
<cfset endTime = CreateTime(13,0,0)>
<cfloop from="#startTime#" to="#endTime#" index="time" step="#CreateTimeSpan(0,0,30,0)#">
<cfoutput>#TimeFormat(time, "short")#<br /></cfoutput>
</cfloop>
更新:以上代码没有解决你的问题。你可以这样做。循环到前面的第二个意味着你总是错过那个结束时间。然后,假设你想显示结束,在循环之后添加:
<cfset startTime = CreateTime(9,0,0)>
<cfset endTime = CreateTime(19,59,59)>
<cfloop from="#startTime#" to="#endTime#" index="time" step="#CreateTimeSpan(0,0,30,0)#">
<cfoutput>#TimeFormat(time, "short")#<br /></cfoutput>
</cfloop>
<cfoutput>#TimeFormat(dateAdd("s", 1, endTime), "short")#<br /></cfoutput>
<cfset startTime = CreateTime(10,0,0)>
<cfset endTime = CreateTime(12,59,59)>
<cfloop from="#startTime#" to="#endTime#" index="time" step="#CreateTimeSpan(0,0,30,0)#">
<cfoutput>#TimeFormat(time, "short")#<br /></cfoutput>
</cfloop>
<cfoutput>#TimeFormat(dateAdd("s", 1, endTime), "short")#<br /></cfoutput>
考虑以下循环:
<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 AM 到 7:30 PM 每 30 分钟增量输出一次。
当我的输入是从:10:00 AM
到:1:00 PM
时,代码每 30 分钟增量输出一次 10:00 AM 到 1:00 PM。
谁能解释发生了什么以及为什么第一个循环中缺少一个 30 分钟的片段而不是第二个循环?有人告诉我这是一种有效的时间循环方法,但我开始认为因为时间在基础 Java 方法中呈现为浮点数,所以发生了一些舍入并且它搞砸了。
编辑:我真的认为我不传递 Date/Time 个对象并不重要。 CF在幕后投射,否则整个事情根本无法进行。
这是因为在幕后,CF 所做的只是将日期转换为双精度(或者在您的情况下:将字符串转换为日期再转换为双精度),晚上 8 点(1 天的 20/24)将有将其留在预期上限内的舍入误差;而下午 1 点(1 天的 13/24)稍微超出了预期的上限。
当循环需要日期时传递字符串可能无济于事。
timeFormat returns 一个字符串。您可以按照文档中的示例使用 createTime 来代替使用适当的时间对象吗?
https://wikidocs.adobe.com/wiki/display/coldfusionen/cfloop%3A+looping+over+a+date+or+time+range
<cfset startTime = CreateTime(9,0,0)>
<cfset endTime = CreateTime(20,0,0)>
<cfloop from="#startTime#" to="#endTime#" index="time" step="#CreateTimeSpan(0,0,30,0)#">
<cfoutput>#TimeFormat(time, "short")#<br /></cfoutput>
</cfloop>
<cfset startTime = CreateTime(10,0,0)>
<cfset endTime = CreateTime(13,0,0)>
<cfloop from="#startTime#" to="#endTime#" index="time" step="#CreateTimeSpan(0,0,30,0)#">
<cfoutput>#TimeFormat(time, "short")#<br /></cfoutput>
</cfloop>
更新:以上代码没有解决你的问题。你可以这样做。循环到前面的第二个意味着你总是错过那个结束时间。然后,假设你想显示结束,在循环之后添加:
<cfset startTime = CreateTime(9,0,0)>
<cfset endTime = CreateTime(19,59,59)>
<cfloop from="#startTime#" to="#endTime#" index="time" step="#CreateTimeSpan(0,0,30,0)#">
<cfoutput>#TimeFormat(time, "short")#<br /></cfoutput>
</cfloop>
<cfoutput>#TimeFormat(dateAdd("s", 1, endTime), "short")#<br /></cfoutput>
<cfset startTime = CreateTime(10,0,0)>
<cfset endTime = CreateTime(12,59,59)>
<cfloop from="#startTime#" to="#endTime#" index="time" step="#CreateTimeSpan(0,0,30,0)#">
<cfoutput>#TimeFormat(time, "short")#<br /></cfoutput>
</cfloop>
<cfoutput>#TimeFormat(dateAdd("s", 1, endTime), "short")#<br /></cfoutput>