Page 1 of 2

Uptime On Your App

Posted: Fri Aug 08, 2025 2:55 am
by Randy Walker
I wanted to be able to know how long my app was up and running. FormatDate failed me so I went through some hoops and came up with this:

Code: Select all

;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

Re: Uptime On Your App

Posted: Fri Aug 08, 2025 5:21 am
by BarryG
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.

Re: Uptime On Your App

Posted: Fri Aug 08, 2025 5:23 am
by Bisonte
FormatDate() is not the right function....

Use this to calculate the ontime into hours/days...

Code: Select all

Procedure.s FormatSeconds(lSeconds.q, Days = 0)
  ; from hroudtwolf
  Protected lHours.q, lMinutes, lDays.q, String.s, First.s
  
  lMinutes = lSeconds / 60
  lSeconds = lSeconds % 60
  lHours   = lMinutes / 60
  lMinutes = lMinutes % 60
  
  If Days
    If lHours =>24
      lDays = lHours / 24
      lHours = lHours % 24
    EndIf
    First.s = Str(lDays) + ":" + RSet (Str(lHours) , 2 , "0") + ":"
  Else
    If lHours > 0
      First.s = Str(lHours) + ":"
    Else
      First.s = ""
    EndIf
  EndIf
  
  String.s = First + RSet (Str(lMinutes) , 2 , "0") + ":" + RSet (Str(lSeconds) , 2 , "0")
  
  ProcedureReturn String
  
EndProcedure

Re: Uptime On Your App

Posted: Fri Aug 08, 2025 8:28 am
by jacdelad
Also, shouldn't ElapsedMilliseconds() work too (and be cross platform)?

Re: Uptime On Your App

Posted: Fri Aug 08, 2025 10:04 am
by Demivec
If you really don't need finer resolution than seconds you can also use DateUTC() instead of GetTickCount_().

Re: Uptime On Your App

Posted: Fri Aug 08, 2025 11:06 am
by Axolotl
I couldn't resist.
Taking into account all the comments so far.....

Code: Select all

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 


Re: Uptime On Your App

Posted: Fri Aug 08, 2025 11:35 am
by BarryG
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.

Re: Uptime On Your App

Posted: Fri Aug 08, 2025 11:42 am
by BarryG
Randy Walker wrote: Fri Aug 08, 2025 2:55 amI wanted to be able to know how long my app was up and running.
Just use the value of Date() when your app starts, and keep calculating its runtime from that. To get you started:

Code: Select all

start.q=Date()

Repeat
  Sleep_(1000)
  Debug "App runtime is: "+FormatDate("%hh:%ii:%ss",Date()-start)
ForEver

Re: Uptime On Your App

Posted: Fri Aug 08, 2025 12:16 pm
by HeX0R
BarryG wrote: Fri Aug 08, 2025 11:35 am
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.

Re: Uptime On Your App

Posted: Fri Aug 08, 2025 12:20 pm
by jacdelad
BarryG wrote: Fri Aug 08, 2025 11:35 am
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().

Re: Uptime On Your App

Posted: Fri Aug 08, 2025 1:11 pm
by BarryG
jacdelad wrote: Fri Aug 08, 2025 12:20 pmCall it at the beginning of your program and you'll get the uptime.
Wrong. Proof:

Code: Select all

Debug ElapsedMilliseconds() ; Returns 0
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."

Re: Uptime On Your App

Posted: Fri Aug 08, 2025 2:11 pm
by Axolotl
because it was mentioned earlier even if it is off topic (I guess the tip was about App uptime not OS uptime) ....

Code: Select all

Import "" ; kernel32.lib  
  GetTickCount64() 
EndImport 

Debug "Windows Uptime = " + FormatDate("%HH:%II:%SS", GetTickCount64() / 1000)  
Two things are important.
1. GetTickCount64() works differently than ElapsedMilliseconds().
2. Windows uptime result depends on the FastBoot setting.

Re: Uptime On Your App

Posted: Fri Aug 08, 2025 2:28 pm
by HeX0R
BarryG wrote: Fri Aug 08, 2025 1:11 pm
jacdelad wrote: Fri Aug 08, 2025 12:20 pmCall it at the beginning of your program and you'll get the uptime.
Wrong. Proof:

Code: Select all

Debug ElapsedMilliseconds() ; Returns 0
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.

Re: Uptime On Your App

Posted: Fri Aug 08, 2025 3:07 pm
by BarryG
Uptime = Time since PC started.
Runtime = How long the app has been running.

That's what my comments are based on.

Re: Uptime On Your App

Posted: Fri Aug 08, 2025 4:17 pm
by jacdelad
Just read the first sentence Randy wrote. The uptime if his program. Period.