VBA, MS Access MS Access в примерах

Дата - Разница во времени с точностью до миллисекунд

По материалам:
http://www.cyberforum.ru/visual-basic/thread1269743.html
https://stackoverflow.com/questions/45513727/vba-performance-test-using-elapsed-milliseconds-in-api-getsystemtime


Private Type SYSTEMTIME
    wYear As Integer
    wMonth As Integer
    wDayOfWeek As Integer
    wDay As Integer
    wHour As Integer
    wMinute As Integer
    wSecond As Integer
    wMilliseconds As Integer
End Type
 
Private Declare Sub GetLocalTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)
 
Private Sub TestSystemTimeDifference()
' Performance Test
Dim st1 As SYSTEMTIME
Dim st2  As SYSTEMTIME
    GetSystemTime st1     ' Start time

    ' Do something
    ' ............

    GetSystemTime st2     ' End time

'--------------------------------------------------------------------
  ' Show System time elapsed with milliseconds as work around
'--------------------------------------------------------------------
  MsgBox SystemTimeDiff(st1, st2), vbInformation, "Systemtime elapsed"

End Sub

 
Private Sub PrintTimeNow()
Dim t As SYSTEMTIME
    GetLocalTime t
    'Debug.Print t.wHour & ":" & t.wMinute & ":" & t.wSecond & "." & t.wMilliseconds
    DoEvents
End Sub

Public Function FormatSystemTime(st As SYSTEMTIME) As String
' TimeStamp hh:nn:ss:mmm
    With st
    FormatSystemTime = Format(.wHour, "00") & ":" & Format(.wMinute, "00") & ":" & _
        Format(.wSecond, "00") & "." & Format(.wMilliseconds, "000")
    End With
End Function

Function SystemTimeDiff(st1 As SYSTEMTIME, st2 As SYSTEMTIME)
  Dim msec1%, msec2 As Integer
  Dim timetaken As Date
  msec1 = Val(Left(Split(FormatSystemTime(st1) & ".", ".")(1) & "000", 3))
  msec2 = Val(Left(Split(FormatSystemTime(st2) & ".", ".")(1) & "000", 3))
  If msec2 < msec1 Then msec2 = msec2 + 1000
  timetaken = CDate(Split(FormatSystemTime(st2) & ".", ".")(0)) - CDate(Split(FormatSystemTime(st1), ".")(0))
  SystemTimeDiff = FormatSystemTime(st1) & vbNewLine & FormatSystemTime(st2) & vbNewLine & _
              (Format(Hour(timetaken), "00") & ":" & Format(Minute(timetaken), "00") & ":" & Format(Second(timetaken), "00")) & _
              "." & Format(msec2 - msec1, "000")

End Function


Private Sub PrintTimeNew()
Dim tOne As SYSTEMTIME
Dim tTwo As SYSTEMTIME
'Dim tDiff As SYSTEMTIME
Dim s$, vDiff As Variant
    
    GetLocalTime tOne
    'Debug.Print tOne.wHour & ":" & tOne.wMinute & ":" & tOne.wSecond & "." & tOne.wMilliseconds
    DoEvents

    GetLocalTime tTwo
    'Debug.Print tTwo.wHour & ":" & tTwo.wMinute & ":" & tTwo.wSecond & "." & tTwo.wMilliseconds
    DoEvents
    
    Debug.Print FormatSystemTime(tOne) & "-"
    vDiff = SystemTimeDiffNew(tOne, tTwo)
    
    Debug.Print vDiff 'tDiff.wHour & ":" & tDiff.wMinute & ":" & tDiff.wSecond & "." & tDiff.wMilliseconds
    DoEvents


End Sub


Назад ToTop
L.E. 30.08.2018
Рейтинг@Mail.ru