简体   繁体   English

在ASP Classic / VBscript中比较时间

[英]Comparing time in ASP classic/VBscript

I am working on a function for an asp page that compares if a time entered is greater than a time with added leeway. 我正在为一个ASP页面编写函数,该函数将比较输入的时间是否大于增加的回旋时间。 I noticed certain times when checked would fail the test when the times are equal. 我注意到,当时间相等时,某些时间在检查时会失败。 Included is a snip of my function to illustrate. 其中包括我的功能片段以进行说明。 Not sure why equal dates would fail, and would like to know if this is a good way to go about comparing time. 不知道为什么相等的日期会失败,并且想知道这是否是比较时间的好方法。

<% 


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

%> 

This problem has earned my attention! 这个问题引起了我的注意! I can reproduce the results and it's very unclear what's going on behind the scenes in these comparisons. 我可以重现结果,目前还不清楚这些比较的幕后发生了什么。 However, I have a workaround for you 但是,我有一个解决方法

Here is a modified version of the code that I've been using to analyse the issue... 这是我用来分析问题的代码的修改版本...

<% 

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

%> 

Note that commenting out line 13 will reveal lines that pass. 请注意,注释掉第13行将显示通过的行。 By default, I'm showing only failures. 默认情况下,我只显示失败。

The first thing to note is that I have some commented variants on lines 24-25 where I add today's date to the value before casting it. 首先要注意的是,我在第24-25行有一些注释变体,其中我将今天的日期添加到该值,然后进行强制转换。 Interestingly, doing this changes the pattern of which times fail the test. 有趣的是,这样做会改变测试失败的时间模式。 There are still roughly the same number of failures but they occur at different buffer values. 故障的数量仍然大致相同,但是它们发生在不同的缓冲区值处。

This leads me to believe that behind the scenes in VBScript, these datetimes might be cast to floating-point numbers when you use the native < <= > >= comparison operators on them and that's resulting in some precision errors. 这使我相信,在VBScript的幕后,当对它们使用本机<<=>> =比较运算符时,这些日期时间可能会转换为浮点数,这会导致一些精度错误。 If they were converted to long integers, then they should surely be correct. 如果将它们转换为长整数,那么它们肯定是正确的。

I did a version of the code where instead of using a direct comparison on the VBDateTimes, I compared the integer representation of them (unix time) using this function : 我做了一段代码,而不是在VBDateTimes上使用直接比较,而是使用以下函数比较了它们的整数表示(unix时间):

Function date2epoch(myDate)
    date2epoch = DateDiff("s", "01/01/1970 00:00:00", myDate)
End Function

When doing that, all tests passed. 这样做时,所有测试都通过了。 However, it is an unusual way to do things. 但是,这是一种不寻常的处理方式。 I thought there should be a more 'normal' way. 我认为应该有一种更“正常”的方式。

I then went back and replaced the straightforward <= operator with a call to DateDiff instead (comment out line 10, uncomment line 11). 然后,我返回并用直接调用<=运算符代替了对DateDiff的调用(在第10行注释掉,在第11行注释掉)。 Whether I used seconds or minutes, the tests passed. 无论我用秒还是数分钟,测试都通过了。 So, I think the takeaway lesson here might be to always use DateDiff when comparing VBDateTimes. 因此,我认为这里的主要收获是比较VBDateTimes时始终使用DateDiff。 As someone who's used VBS for a while and never encountered issues with native comparisons before, this is a revelation and I may need to offer this advice to my colleagues too. 作为使用VBS一段时间并且从未遇到过本机比较问题的人,这是一个启示,我可能也需要向我的同事提供此建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM