Helpful Information
 
 
Category: Visual Basic Programming
Figuring out System Uptime via API

Hey, I'm trying to make a small formless app that will read the tick value from the API call 'GetTickCount' which is supposed to return the system uptime as long in miliseconds. What I want to do is create a function that will return a string based on time elapsed such as "45 Days 5 Hours 6 Minutes 23 Seconds" as seen on a lot of popular progs (mIRC, Samurize..etc etc)

I'm not very good at math, anyone have any ideas?

Think this through logically. What does it take to make up 1 unit of each time segment?

Your base unit is 1 millisecond.
1000 milliseconds = 1 second
60 second = 1 minute
60 minutes = 1 hour
24 hours = 1 day

So, if you take your total value in milliseconds and convert it into seconds, then you have a good starting value.

seconds = milliseconds / 1000

From this value, get the total number of days. 1 day is 86400 seconds (60 seconds * 60 minutes * 24 hours), with this value we calculate how many days is in the value, store that into a variable and subtract that total from the seconds. This leaves us with the remaining time.

if (seconds >= 864000) then
days = seconds / 86400
seconds = seconds - (days * 86400)
else
days = 0
end if

Now we get the total number of hours. 1 hour is 3600 seconds (60 seconds * 60 minutes), with this values we calculate how many hours are left in the seconds variable. Then we subtract it and this leaves us with the minutes and seconds remaining.

if (seconds >= 3600) then
hours = seconds / 3600
seconds = seconds - (hours * 3600)
else
hours = 0
end if

Now we perform the same operation to figure out the minutes.

if (seconds >= 60) then
minutes = seconds / 60
seconds = seconds - (minutes * 60)
else
minutes = 0
end if

Now we have all the values stored in thier individual variables so that you can create any type of string from them you need.

Does this help?

I noticed this wouldn't actually work in VB the way it is written. All of the division operators are the wrong way....they should be "\" and not "/"......also....I adapted it to fit in a module:

Option Explicit

Public Type TimeConv
Days As Long
Hours As Long
Minutes As Long
Seconds As Long
MSeconds As Long
End Type

Private Declare Function GetTickCount Lib "kernel32.dll" () As Long

Private Function ConvertTime(ByVal Tick As Long) As TimeConv
Dim TheDays As Long
Dim TheHours As Long
Dim TheMinutes As Long
Dim TheSeconds As Long
Dim TheMilliseconds As Long
Dim TheTicks As Long

TheTicks = Tick

TheMilliseconds = TheTicks Mod 1000

TheSeconds = (TheTicks \ 1000)

If (TheSeconds >= 86400) Then
TheDays = TheSeconds \ 86400
TheSeconds = TheSeconds - (TheDays * 86400)
Else
TheDays = 0
End If

If (TheSeconds >= 3600) Then
TheHours = TheSeconds \ 3600
TheSeconds = TheSeconds - (TheHours * 3600)
Else
TheHours = 0
End If

If (TheSeconds >= 60) Then
TheMinutes = TheSeconds \ 60
TheSeconds = TheSeconds - (TheMinutes * 60)
Else
TheMinutes = 0
End If

ConvertTime.Days = TheDays
ConvertTime.Hours = TheHours
ConvertTime.Minutes = TheMinutes
ConvertTime.Seconds = TheSeconds
ConvertTime.MSeconds = TheMilliseconds

End Function


Property Get SystemUptime() As TimeConv
SystemUptime = ConvertTime(GetTickCount)
End Property

All of the division operators are the wrong way....they should be "\" and not "/".

Thanks for bouncing this three year old thread to mention that but. No, / is correct.

Please note the difference:

3/4 = 0.75
3\4 = 0

7/2 = 3.5
7\2 = 3

All you did is make is drastically less accurate.

Actually, I take that back. I understand where you are coming from but please explain it for the peanut gallery so they understand your logic :-)

I actually have similar in one of my routines



intEstDays = lngEstimatedSeconds \ 86400
lngEstimatedSeconds = lngEstimatedSeconds - CLng(intEstDays) * 86400
intEstHours = lngEstimatedSeconds \ 3600
lngEstimatedSeconds = lngEstimatedSeconds - (CLng(intEstHours) * 3600)
intEstMinutes = lngEstimatedSeconds \ 60
intEstSeconds = lngEstimatedSeconds - (intEstMinutes * 60)

yes, medialint is correct. It is /, which is on your keypad portion of your keyboard. Makes it easy to not get it mixed up.
Also, if you look at my post, all it really does, is just try to explain to the original post how to figure out time. Thanks again for dredging up a post several years old...










privacy (GDPR)