hexagon logo

Runtime script with inexplicable results...

I have a function in my scripts to calculate the runtime in seconds for a program, using the Timer value from windows, which returns the current time as seconds from midnight.

The blue code is my script run at the beginning of a part program. The red code is what is run at the end. (I've cut and pasted these sections from the scripts...they are too long.) In the event that a program runs through midnight, I have a section in the red code to correctly calculate runtime. I've tested this code in the excel editor while resetting my computer clock to 11:59 PM and running it through midnight, and it works. Unfortunately, this code does not work when running PC-DMIS through midnight. I get program times of more than 86400.

Any ideas why this is not calculating correctly only when run through PC-DMIS?

[COLOR="Blue"]Sub Main()

Dim BeforeTime As String
BeforeTime = Timer
BeforeTime = CLng(BeforeTime)

End Sub[/COLOR]

[COLOR="Red"]
Sub Main()

Dim Aftertime As String
Aftertime = Timer
Aftertime = CLng(Aftertime)
Dim RunTime As String

If Aftertime < BeforeTime Then
RunTime = (86400 - BeforeTime) + Aftertime
Else
RunTime = Aftertime - BeforeTime
End If

End Sub[/COLOR]
  • I have a function in my scripts to calculate the runtime in seconds for a program, using the Timer value from windows, which returns the current time as seconds from midnight.

    The blue code is my script run at the beginning of a part program. The red code is what is run at the end. (I've cut and pasted these sections from the scripts...they are too long.) In the event that a program runs through midnight, I have a section in the red code to correctly calculate runtime. I've tested this code in the excel editor while resetting my computer clock to 11:59 PM and running it through midnight, and it works. Unfortunately, this code does not work when running PC-DMIS through midnight. I get program times of more than 86400.

    Any ideas why this is not calculating correctly only when run through PC-DMIS?

    [COLOR="Blue"]Sub Main()
    
    Dim BeforeTime As String
    BeforeTime = Timer
    BeforeTime = CLng(BeforeTime)
    
    End Sub[/COLOR]
    
    
    [COLOR="Red"]
    Sub Main()
    
    Dim Aftertime As String
    Aftertime = Timer
    Aftertime = CLng(Aftertime)
    Dim RunTime As String
    
    If Aftertime < BeforeTime Then
    RunTime = (86400 - BeforeTime) + Aftertime
    Else
    RunTime = Aftertime - BeforeTime
    End If
    
    End Sub[/COLOR]


    There are lots of problems with your design here. I know because I've made this type of mistake before.

    You're assuming that you're off by 1 day (which is probably OK for this), but won't work when you reuse this (and you will, trust me).

    Better would be:

    while AfterTime < BeforeTIme
    BeforeTIme = BeforeTime - 86400


    which would correct for any number of days off. This is easier to express with:
    BeforeTime = BeforeTIme Mod 86400

    But that is really unnecessary as well. More to the point, you're getting into a lot of assumptions about the underlying structure of the data, and I suspect that's causing you the problem. So do this instead:

    BeforeTime = Now
    ...
    AfterTime = Now
    RunTime = DateDiff (s, AfterTIme, BeforeTIme)


    and let the language do the thinking for you

    If you're REALLY interested, read Eric Lippert's blog about how he fixed this particular function (he's a software engineer at Microsoft) to see just what you're assuming about:
    http://blogs.msdn.com/b/ericlippert/archive/2003/09/16/53013.aspx

    Hope this helps.
  • Thanks for the information. Eric's blog and the Spolsky article within made for interesting reading.

    When learning on your own, you just don't know what you don't know. I will update this thread when I get the code changed over.