Uptime On Your App

Share your advanced PureBasic knowledge/code with the community.
Randy Walker
Addict
Addict
Posts: 1008
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Uptime On Your App

Post 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
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
BarryG
Addict
Addict
Posts: 4155
Joined: Thu Apr 18, 2019 8:17 am

Re: Uptime On Your App

Post 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.
Last edited by BarryG on Fri Aug 08, 2025 5:24 am, edited 1 time in total.
User avatar
Bisonte
Addict
Addict
Posts: 1310
Joined: Tue Oct 09, 2007 2:15 am

Re: Uptime On Your App

Post 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
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.)
User avatar
jacdelad
Addict
Addict
Posts: 1999
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Uptime On Your App

Post by jacdelad »

Also, shouldn't ElapsedMilliseconds() work too (and be cross platform)?
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
Demivec
Addict
Addict
Posts: 4265
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Uptime On Your App

Post by Demivec »

If you really don't need finer resolution than seconds you can also use DateUTC() instead of GetTickCount_().
Axolotl
Addict
Addict
Posts: 815
Joined: Wed Dec 31, 2008 3:36 pm

Re: Uptime On Your App

Post 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 

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).
BarryG
Addict
Addict
Posts: 4155
Joined: Thu Apr 18, 2019 8:17 am

Re: Uptime On Your App

Post 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.
BarryG
Addict
Addict
Posts: 4155
Joined: Thu Apr 18, 2019 8:17 am

Re: Uptime On Your App

Post 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
User avatar
HeX0R
Addict
Addict
Posts: 1197
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Uptime On Your App

Post 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.
User avatar
jacdelad
Addict
Addict
Posts: 1999
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Uptime On Your App

Post 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().
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
BarryG
Addict
Addict
Posts: 4155
Joined: Thu Apr 18, 2019 8:17 am

Re: Uptime On Your App

Post 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."
Axolotl
Addict
Addict
Posts: 815
Joined: Wed Dec 31, 2008 3:36 pm

Re: Uptime On Your App

Post 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.
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).
User avatar
HeX0R
Addict
Addict
Posts: 1197
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Uptime On Your App

Post 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.
BarryG
Addict
Addict
Posts: 4155
Joined: Thu Apr 18, 2019 8:17 am

Re: Uptime On Your App

Post by BarryG »

Uptime = Time since PC started.
Runtime = How long the app has been running.

That's what my comments are based on.
User avatar
jacdelad
Addict
Addict
Posts: 1999
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Uptime On Your App

Post by jacdelad »

Just read the first sentence Randy wrote. The uptime if his program. Period.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Post Reply