You can obtain simple elapsed time by subtracting two date values. For example, CLng(Now - CDate(" 1/1/2000")) determines the number of days elapsed since January 1, 2000. OOo Basic supports returning elapsed time as the number of seconds (Timer) and as the number of system ticks (GetSystemTicks). See Table 6 . Internally, computers have a system timer that advances at a certain rate; the rate is hardware dependent. On Intel-based computers this value is 1/17 th of a second. Each time that the timer advances by 1, it's called a "system tick." The number returned by GetSystemTicks is always based on milliseconds , even if it relies on a less accurate clock.
Function | Description |
---|---|
GetSystemTicks | Return the number of system ticks as a Long. The reported time is always in milliseconds, even if the underlying timer is less accurate. |
Timer | Return the number of seconds since midnight as a Date. Cast this to a Long. |
Use GetSystemTicks to get the system-dependent number of system timer ticks. This value is typically used to time internal operations because it has higher precision than the internal time and date functions (see Listing 12 and Figure 3 ). The return value is a Long.
Sub ExampleElapsedTime Dim StartTicks As Long Dim EndTicks As Long Dim StartTime As Date Dim EndTime As Date StartTicks = GetSystemTicks() StartTime = Timer Wait(200) 'Pause execution for 0.2 seconds EndTicks = GetSystemTicks() EndTime = Timer MsgBox "After waiting 200 ms (0.2 seconds), " & CHR$(10) &_ "System ticks = " & CStr(EndTicks - StartTicks) & CHR$(10) &_ "Time elapsed = " & CStr((EndTime - StartTime) * 24 * 60 * 60) &_ CHR$(10), 0, "Elapsed Time"End Sub End Sub
Compatibility | GetSystemTicks is specific to OOo Basic and is not supported by Visual Basic. |
The Timer function returns the number of seconds since midnight as a Date object. The problem with this is that at 10 seconds past midnight, the return value is 10. The Date object, however, interprets 10 to mean "10 days". Cast the returned type directly to a numerical type using CLng or CDbl to obtain the number of elapsed seconds.
Dim nSeconds As Long nSeconds = Timer Print "Number of seconds = " & nSeconds Print "Number of seconds = " & Clng(Timer)
Tip | The Timer function returns the number of seconds since midnight. Using Timer to determine elapsed time for spans that start before midnight and end after midnight produces useless results. |