;Put this line at start of your app.
AppStart = GetTickCount_()
Delay(4000) ;Just simuilating some run time here.
;Put the following calculations on a button or whatever to get uptime on your app.
c = (GetTickCount_() - AppStart)/1000
Debug FormatDate("%dd:%hh:%ii:%ss",c)
;FomatDate erromneously gave one day And 4 seconds so I did the following code:
d = c / 86400
r = c % 86400
aput$ = RSet(Str(d),2,"0") + ":"
h = r / 3600
r = r % 3600
aput$ + RSet(Str(h),2,"0") + ":"
m = r / 60
r = r % 60
aput$ +RSet(Str(m),2,"0") + ":"
aput$ +RSet(Str(r),2,"0")
Debug aput$
;That is correct
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
GetTickCount_() doesn't work after 49.7 days of uptime, so it's not usable for this sort of thing. Microsoft says to use GetTickCount64() instead, but that's not immediately available in PureBasic as a single API command. Someone else here will surely step in and show how to use it.
Last edited by BarryG on Fri Aug 08, 2025 5:24 am, edited 1 time in total.
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom
English is not my native language... (I often use DeepL.)
Procedure RunningSinceStart()
Static s_dtStart = 0
Protected dtDiff
If s_dtStart = 0
s_dtStart = DateUTC()
EndIf
If s_dtStart
dtDiff = DateUTC() - s_dtStart
EndIf
ProcedureReturn dtDiff
EndProcedure
Procedure.s DateDiffToStr(DtDiff) ; makes Day(s), Hour(s), Minute(s) and Seconds(s).
Protected result$, tmp
tmp = Day(DtDiff) - 1
result$ = Str(tmp) + " day" + StringField(".s.", Bool(tmp > 1)+1, ".") + ", "
tmp = Hour(DtDiff)
result$ + Str(tmp) + " hour" + StringField(".s.", Bool(tmp > 1)+1, ".") + ", "
tmp = Minute(DtDiff)
result$ + Str(tmp) + " minute" + StringField(".s.", Bool(tmp > 1)+1, ".") + ", "
tmp = Second(DtDiff)
result$ + Str(tmp) + " second" + StringField(".s.", Bool(tmp > 1)+1, ".") + ". "
ProcedureReturn result$
EndProcedure
CompilerIf 10 ; <== Test the procedures above
dt = RunningSinceStart() ; first start initialized the start time
Debug "Start " + DatediffToStr(dt)
Delay (2000) ; I'm not patient enough for a long wait.
dt = RunningSinceStart() ; next returns the difference time
Debug "App is running since " + DatediffToStr(dt)
CompilerEndIf
Just because it worked doesn't mean it works. PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
jacdelad wrote: Fri Aug 08, 2025 8:28 am
Also, shouldn't ElapsedMilliseconds() work too (and be cross platform)?
No, that's not uptime. It's just a timer from one call of it to the next.
That was only true years ago.
I can't remember in which PB version it changed, but today, the first call to ElapsedMilliseconds() returns 0, and starts counting from then on.
jacdelad wrote: Fri Aug 08, 2025 8:28 am
Also, shouldn't ElapsedMilliseconds() work too (and be cross platform)?
No, that's not uptime. It's just a timer from one call of it to the next.
No it isn't. It's the milliseconds elapsed from the first time it was called. Call it at the beginning of your program and you'll get the uptime. It also doesn't interfere with other usages of ElapsedMilliseconds().
It's got nothing to do with uptime at all. Fred has confirmed this in past comments. It's just a timer.
HeX0R wrote: Fri Aug 08, 2025 12:16 pmI can't remember in which PB version it changed, but today, the first call to ElapsedMilliseconds() returns 0, and starts counting from then on.
Exactly what I said: a timer from one call of it to the next, which is why the first call returns 0 (and not the PC's uptime). The manual even says: "This function should be used to calculate time differences between multiple ElapsedMilliseconds() calls."
Two things are important.
1. GetTickCount64() works differently than ElapsedMilliseconds().
2. Windows uptime result depends on the FastBoot setting.
Just because it worked doesn't mean it works. PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
It's got nothing to do with uptime at all. Fred has confirmed this in past comments. It's just a timer.
HeX0R wrote: Fri Aug 08, 2025 12:16 pmI can't remember in which PB version it changed, but today, the first call to ElapsedMilliseconds() returns 0, and starts counting from then on.
Exactly what I said: a timer from one call of it to the next, which is why the first call returns 0 (and not the PC's uptime). The manual even says: "This function should be used to calculate time differences between multiple ElapsedMilliseconds() calls."
ehm... no one asked for the PC uptime here
And to me it was pretty clear that jacdelad meant:
Call it at the beginning of your program and you'll get the uptime when calling it next time.