Valhalla Legends Forums Archive | Visual Basic Programming | Log time with milliseconds

AuthorMessageTime
Grok
I use a single procedure to write log files.
My old WriteLog used to produce:

10:28:09 DatabaseFile::ConnectToDatabase() entered.
10:28:09 DatabaseFile::ConnectToDatabase() successful, connected.
10:28:09 ERROR DatabaseFile::SetupForValidation() - -2147211402-The requested item is not in the collection.
10:28:15 DatabaseFile::ConnectToDatabase() entered.
10:28:15 DatabaseFile::ConnectToDatabase() successful, connected.
10:28:15 ERROR DatabaseFile::SetupForValidation() - -2147211402-The requested item is not in the collection.

When just HH:MM:SS was not sufficient, I had to
upgrade WriteLog(). Now it reports milliseconds.

12:13:59.0281 *********
12:13:59.0571
12:14:00.0152 *********
12:14:00.0312
12:14:00.0633 *********

Here's the VB code for whoever cares.

[code]
Private Declare Sub GetLocalTime Lib "Kernel32" (lpSystemTime As SYSTEMTIME)

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 st As SYSTEMTIME

Public Sub WriteLog(ByVal msg As String)

On Error GoTo WriteLogErr

Dim F As Integer
Dim fn As String
Dim sTime As String
GetLocalTime st
sTime = Format(st.wHour, "00") & ":" & Format(st.wMinute, "00") & ":" & _
Format(st.wSecond, "00") & "." & Format(st.wMilliseconds, "0000")
fn = "C:\TEMP\" & Format$(Now, "YYYYMMDD") & ".txt"
F = FreeFile
Open fn For Append As #F
Print #F, sTime & " " & msg
Close #F
' Timer1.Interval = CInt(Right(CStr(st.wMilliseconds), 3)) + 1
DoEvents

WriteLogExit:
Exit Sub

WriteLogErr:
Debug.Print Err.Description
Resume WriteLogExit

End Sub
[/code]
December 15, 2003, 5:18 PM
Spht
[quote author=Grok link=board=31;threadid=4255;start=0#msg35501 date=1071508712][code] sTime = Format(st.wHour, "0") & ":" & Format(st.wMinute, "0") & ":" & _
Format(st.wSecond, "0") & "." & Format(st.wMilliseconds, "00")[/code][/quote]

Removed unneeded zero's since st structure contains all numbers (so can never return an empty string).

Edit - Took off another zero for wMilliseconds since it'll never be >999.
December 15, 2003, 5:25 PM
Grok
[quote author=Spht link=board=31;threadid=4255;start=0#msg35503 date=1071509107]
[quote author=Grok link=board=31;threadid=4255;start=0#msg35501 date=1071508712][code] sTime = Format(st.wHour, "0") & ":" & Format(st.wMinute, "0") & ":" & _
Format(st.wSecond, "0") & "." & Format(st.wMilliseconds, "00")[/code][/quote]

Removed unneeded zero's since st structure contains all numbers (so can never return an empty string).

Edit - Took off another zero for wMilliseconds since it'll never be >999.
[/quote]

Before correcting my code, please test your corrections. I specifically used those format strings in order to pad the hour, minute, second, and milliseconds appropriately. I did have one extra 0 in milliseconds, but everything else was correct.
December 15, 2003, 7:01 PM
Spht
[quote author=Grok link=board=31;threadid=4255;start=0#msg35516 date=1071514906]
[quote author=Spht link=board=31;threadid=4255;start=0#msg35503 date=1071509107]
[quote author=Grok link=board=31;threadid=4255;start=0#msg35501 date=1071508712][code] sTime = Format(st.wHour, "0") & ":" & Format(st.wMinute, "0") & ":" & _
Format(st.wSecond, "0") & "." & Format(st.wMilliseconds, "00")[/code][/quote]

Removed unneeded zero's since st structure contains all numbers (so can never return an empty string).

Edit - Took off another zero for wMilliseconds since it'll never be >999.
[/quote]

Before correcting my code, please test your corrections. I specifically used those format strings in order to pad the hour, minute, second, and milliseconds appropriately. I did have one extra 0 in milliseconds, but everything else was correct.
[/quote]

Sorry about that, I thought you were using Right() for some reason...

[code]Dim a As Long
a = 0
Debug.Print Right("00" & a, 2)
Debug.Print Right("0" & a, 2)
a = 10
Debug.Print Right("00" & a, 2)
Debug.Print Right("0" & a, 2)[/code]

Extra zero unneeded.
December 15, 2003, 7:36 PM
UserLoser.
[quote author=Spht link=board=31;threadid=4255;start=0#msg35524 date=1071517019]
Sorry about that, I thought you were using Right() for some reason...
[/quote]

Sounds like what I did when you yelled at me (right & extra 0's) :P

Also, sometimes wHour returns 0 when it's 6:00 PM on my system, why is that?
December 16, 2003, 12:39 AM
Grok
Moved Andreas Strobl's code for VB.NET to the .NET forum.
December 17, 2003, 12:08 PM

Search