Search notes:

VBA function: timer

The function timer returns a single that contains the number of seconds (to two decimal places) since midnight.

Trying to gauge timer's accuracy

The following example tries to gauge the accuracy of the timer.
option explicit

sub main()

  dim t0, t1       as single
  dim cnt          as long
  dim measurements as long
  
  cnt          = 0
  measurements = 0
  
  t0  = timer
  
  do while cnt < 10
  
     t1           = timer
     measurements = measurements + 1
  
     if t1 > t0 then
        debug.print "Diff: " & round(t1-t0, 3)
        t0 = t1
        cnt = cnt + 1
     end if
  
  loop
  
  debug.print "Total measurements: " & measurements

end sub
Github repository about-VBA, path: /functions/timer/accuracy.bas

Calculating hours, minutes, seconds and fractional part

option explicit

sub printHourMinuteSecond() ' {

    dim secsSinceMidnight as single ' timer returns also fractional part.
    dim hour_       as integer
    dim minute_     as integer
    dim second_     as integer
    dim secondPart_ as single

    secsSinceMidnight = timer

    hour_       = int(  secsSinceMidnight        /60/60                )
    minute_     = int( (secsSinceMidnight - hour_*60*60 )        /60   )
    second_     = int( (secsSinceMidnight - hour_*60*60 - minute_*60 ) )
    secondPart_ =       secsSinceMidnight - hour_*60*60 - minute_*60 - second_

    debug.print("Calculated hr:mi:ss is: " & _
       format(hour_      ,  "00"  ) & ":"  & _
       format(minute_    ,  "00"  ) & ":"  & _
       format(second_    ,  "00"  )        & _
       format(secondPart_, ".00") )

end sub ' }
Github repository about-VBA, path: /functions/timer/printHourMinuteSecond.bas

See also

The VBA class timeAccumulator allows to measure elapsed time for different portions within the source code using the WinAPI function QueryPerformanceFrequency.
VBA functions

Index