比较 ASP classic/VBscript 中的时间
Comparing time in ASP classic/VBscript
我正在为 asp 页面开发一个功能,该功能比较输入的时间是否大于具有更多余地的时间。我注意到某些时间在检查时会在时间相等时无法通过测试。包括我的功能片段来说明。不确定为什么相等的日期会失败,想知道这是否是比较时间的好方法。
<%
function TimeTest(testTime, checkTime, buffer, try)
checkingTime = FormatDateTime(cdate(DateAdd("n", buffer, cdate(checkTime))),4)
if try = 1 then
testTime = FormatDateTime(testTime, 4)
checktime = FormatDateTime(checkTime, 4)
end if
if cdate(testTime) > DateAdd("n", buffer, cdate(checkTime)) then
TimeTest = "<p class = 'redS'>Fails! testTime: "&testTime&" < checkTime:"&checkingTime&"</p>"
else
TimeTest = "<p class = 'greenS'>Works! testTime: "&testTime&" > checkTime:"&checkingTime&"</p>"
end if
end function
response.write("<br><br><h1>Test2</h1><br>")
for i=0 to 23
for j=0 to 59
response.write(TimeTest(i&":"&j&":00", i&":00:00", j, 1))
response.write("<BR>")
next
next
%>
这个问题引起了我的注意!我可以重现结果,但非常不清楚这些比较的幕后情况。不过,我为您准备了一个解决方法
这是我用来分析问题的代码的修改版本...
<%
Option Explicit
Function TimeTest(a, b, buffer)
Dim c : c = DateAdd("n", buffer, b)
Dim s : s = Join(Array("a=" & a, "b=" & b, "c=" & c, "buffer=" & buffer), ", ")
Dim passed : passed = a <= c
'Dim passed : passed = DateDiff("s", a, c) <= 0
If passed Then Exit Function
Dim color : color = "red" : If passed Then color = "green"
TimeTest = "<div style='color:" & color & "'>" & s & "</div>"
End Function
Dim i, j, a, b
For i = 0 To 23
For j = 0 To 59
a = CDate(i & ":" & j & ":00")
b = CDate(i & ":00:00")
'a = CDate(Date() & " " & i & ":" & j & ":00")
'b = CDate(Date() & " " & i & ":00:00")
Response.Write(TimeTest(a, b, j))
Next
Response.Write("<hr>")
Next
%>
请注意,注释掉第 13 行会显示通过的行。默认情况下,我只显示失败。
首先要注意的是,我在第 24-25 行有一些注释变体,我在其中将今天的日期添加到值中,然后再进行转换。有趣的是,这样做会改变测试失败的模式。失败次数仍然大致相同,但它们发生在不同的缓冲区值。
这让我相信,在 VBScript 的幕后,当您对这些日期时间使用本机 < <= > >= 比较运算符时,这些日期时间可能会被强制转换为浮点数,这会导致一些精度错误。如果将它们转换为长整数,那么它们应该是正确的。
我做了一个代码版本,我没有使用 VBDateTimes 的直接比较,而是使用 this function:
比较了它们的整数表示(unix 时间)
Function date2epoch(myDate)
date2epoch = DateDiff("s", "01/01/1970 00:00:00", myDate)
End Function
这样做时,所有测试都通过了。但是,这是一种不寻常的做事方式。我认为应该有更多的'normal'方式。
然后我返回并将简单的 <= 运算符替换为对 DateDiff
的调用(注释掉第 10 行,取消注释第 11 行)。无论我使用秒还是分钟,测试都通过了。因此,我认为这里的要点可能是 总是 在比较 VBDateTimes 时使用 DateDiff。作为使用 VBS 一段时间并且之前从未遇到过原生比较问题的人,这是一个启示,我可能也需要向我的同事提供这个建议。
我正在为 asp 页面开发一个功能,该功能比较输入的时间是否大于具有更多余地的时间。我注意到某些时间在检查时会在时间相等时无法通过测试。包括我的功能片段来说明。不确定为什么相等的日期会失败,想知道这是否是比较时间的好方法。
<%
function TimeTest(testTime, checkTime, buffer, try)
checkingTime = FormatDateTime(cdate(DateAdd("n", buffer, cdate(checkTime))),4)
if try = 1 then
testTime = FormatDateTime(testTime, 4)
checktime = FormatDateTime(checkTime, 4)
end if
if cdate(testTime) > DateAdd("n", buffer, cdate(checkTime)) then
TimeTest = "<p class = 'redS'>Fails! testTime: "&testTime&" < checkTime:"&checkingTime&"</p>"
else
TimeTest = "<p class = 'greenS'>Works! testTime: "&testTime&" > checkTime:"&checkingTime&"</p>"
end if
end function
response.write("<br><br><h1>Test2</h1><br>")
for i=0 to 23
for j=0 to 59
response.write(TimeTest(i&":"&j&":00", i&":00:00", j, 1))
response.write("<BR>")
next
next
%>
这个问题引起了我的注意!我可以重现结果,但非常不清楚这些比较的幕后情况。不过,我为您准备了一个解决方法
这是我用来分析问题的代码的修改版本...
<%
Option Explicit
Function TimeTest(a, b, buffer)
Dim c : c = DateAdd("n", buffer, b)
Dim s : s = Join(Array("a=" & a, "b=" & b, "c=" & c, "buffer=" & buffer), ", ")
Dim passed : passed = a <= c
'Dim passed : passed = DateDiff("s", a, c) <= 0
If passed Then Exit Function
Dim color : color = "red" : If passed Then color = "green"
TimeTest = "<div style='color:" & color & "'>" & s & "</div>"
End Function
Dim i, j, a, b
For i = 0 To 23
For j = 0 To 59
a = CDate(i & ":" & j & ":00")
b = CDate(i & ":00:00")
'a = CDate(Date() & " " & i & ":" & j & ":00")
'b = CDate(Date() & " " & i & ":00:00")
Response.Write(TimeTest(a, b, j))
Next
Response.Write("<hr>")
Next
%>
请注意,注释掉第 13 行会显示通过的行。默认情况下,我只显示失败。
首先要注意的是,我在第 24-25 行有一些注释变体,我在其中将今天的日期添加到值中,然后再进行转换。有趣的是,这样做会改变测试失败的模式。失败次数仍然大致相同,但它们发生在不同的缓冲区值。
这让我相信,在 VBScript 的幕后,当您对这些日期时间使用本机 < <= > >= 比较运算符时,这些日期时间可能会被强制转换为浮点数,这会导致一些精度错误。如果将它们转换为长整数,那么它们应该是正确的。
我做了一个代码版本,我没有使用 VBDateTimes 的直接比较,而是使用 this function:
比较了它们的整数表示(unix 时间)Function date2epoch(myDate)
date2epoch = DateDiff("s", "01/01/1970 00:00:00", myDate)
End Function
这样做时,所有测试都通过了。但是,这是一种不寻常的做事方式。我认为应该有更多的'normal'方式。
然后我返回并将简单的 <= 运算符替换为对 DateDiff
的调用(注释掉第 10 行,取消注释第 11 行)。无论我使用秒还是分钟,测试都通过了。因此,我认为这里的要点可能是 总是 在比较 VBDateTimes 时使用 DateDiff。作为使用 VBS 一段时间并且之前从未遇到过原生比较问题的人,这是一个启示,我可能也需要向我的同事提供这个建议。