Page 1 of 2

[REVISED] "uptime" for Windows

Posted: Thu Mar 13, 2025 1:14 am
by Randy Walker
found the uptime line command very handy on Linux but Windows doesn't have it so I created this:

Code: Select all

; Written by Randy Walker (8/16/2024)
;free To use Or improve As you like
Import "" ; --- from Kernel32.dll ---
  GetTickCount64()  ; Modified (3/13/2025) to include this Import suggested by Axolotl
  ;https://www.purebasic.fr/english/viewtopic.php?p=637424#p637424
EndImport 
trigger.q = (GetTickCount64() & $FFFFFFFFFFFFFFFF)/1000
hours.d = trigger / 3600  ;193.92388886213303
totalSeconds.d = hours * 3600
uptm$ = StrD(totalSeconds)  
days.d = Int(totalSeconds / 86400)
remainingSeconds.d = totalSeconds - (days * 86400)
hours.d = Int(remainingSeconds / 3600)
remainingSeconds.d = remainingSeconds - (hours * 3600)
minutes.d = Int(remainingSeconds / 60)
seconds.d = Round(remainingSeconds - (minutes * 60), #PB_Round_Nearest)
Date.q = Date()
Boot.q = date-trigger
Start$ = FormatDate("%hh:%ii:%ss   %mm:%dd:%yyyy",boot)
s$ = uptm$ + " Total seconds   "+Chr(10)+StrD(days) + " days   "+Chr(10) +StrD(hours) + " hours   "+Chr(10) +StrD(minutes) + " Minutes   "+Chr(10) +StrD(seconds)+ " seconds"+Chr(10)+Chr(10)+"Last boot at:"+Chr(10)+Start$
MessageRequester("  System  Uptime", s$)
REVISED code located here: viewtopic.php?p=644618#p644618
I added the ability to do reboot or shutdown with optional countdown. I have it on a hotkey so I can launch it and press spacebar twice for fast and convenient reboot.

Re: "uptime" for Windows

Posted: Thu Mar 13, 2025 8:30 am
by BarryG
Cool. Matches what Task Manager says, which I was hoping it would. :)

Re: "uptime" for Windows

Posted: Thu Mar 13, 2025 9:07 am
by idle
works well. :D

Re: "uptime" for Windows

Posted: Thu Mar 13, 2025 9:18 am
by Randy Walker
BarryG wrote: Thu Mar 13, 2025 8:30 am Cool. Matches what Task Manager says, which I was hoping it would. :)
Yeah, I think it's off by a second or so.
I put it on a (WinHotkey) hotkey because Task Manager is not so convenient.

@Idle -- Thanks Idle!

Re: "uptime" for Windows

Posted: Thu Mar 13, 2025 9:24 am
by Randy Walker
Also a lot faster than typing systeminfo | find /i "Boot Time" into powershell.

Re: "uptime" for Windows

Posted: Thu Mar 13, 2025 9:28 am
by idle
Randy Walker wrote: Thu Mar 13, 2025 9:24 am Also a lot faster than typing systeminfo | find /i "Boot Time" into powershell.
GetTickCount is a lot easier for sure.

Re: "uptime" for Windows

Posted: Thu Mar 13, 2025 12:53 pm
by Caronte3D
:lol:

Re: "uptime" for Windows

Posted: Thu Mar 13, 2025 2:01 pm
by BarryG
idle wrote: Thu Mar 13, 2025 9:28 amGetTickCount is a lot easier for sure
Actually, I just saw that. GetTickCount_() is only valid for 49 days, and then it wraps. Does the code take that into account?

Re: "uptime" for Windows

Posted: Thu Mar 13, 2025 2:40 pm
by Quin
BarryG wrote: Thu Mar 13, 2025 2:01 pm
idle wrote: Thu Mar 13, 2025 9:28 amGetTickCount is a lot easier for sure
Actually, I just saw that. GetTickCount_() is only valid for 49 days, and then it wraps. Does the code take that into account?
Nope, for that you'd need to import GetTickCount64().

Re: "uptime" for Windows

Posted: Thu Mar 13, 2025 3:24 pm
by Axolotl
Maybe like this?

Code: Select all

Import "" ; --- from Kernel32.dll ---
  GetTickCount64()
EndImport 

Debug FormatDate("Computer was switched on at:: %YYYY-%MM-%DD %HH:%II:%SS", Date() - (GetTickCount64() / 1000)) 

BTW: You have to keep in mind, that the result depends on the setting for fast startup on Windows.
HINT:
Under the "Shutdown settings" section, check the "Turn on fast startup" option.

Re: "uptime" for Windows

Posted: Thu Mar 13, 2025 3:37 pm
by Quin
Axolotl wrote: Thu Mar 13, 2025 3:24 pm Maybe like this?

Code: Select all

Import "" ; --- from Kernel32.dll ---
  GetTickCount64()
EndImport 

Debug FormatDate("Computer was switched on at:: %YYYY-%MM-%DD %HH:%II:%SS", Date() - (GetTickCount64() / 1000)) 

BTW: You have to keep in mind, that the result depends on the setting for fast startup on Windows.
HINT:
Under the "Shutdown settings" section, check the "Turn on fast startup" option.
Yup, this works, although fast startup can actually make the results less accurate. If you shut down and then boot up the computer again with fast startup, it won't reset your uptime.

Re: "uptime" for Windows

Posted: Thu Mar 13, 2025 7:45 pm
by Randy Walker
BarryG wrote: Thu Mar 13, 2025 2:01 pm Actually, I just saw that. GetTickCount_() is only valid for 49 days, and then it wraps. Does the code take that into account?
Thanks for pointing that out BarryG!!! I'll modify my OP to include the getTickCount64 as Axolotl suggested, And thanks for that Axolotl !!!

Re: "uptime" for Windows

Posted: Thu Mar 13, 2025 9:37 pm
by jacdelad

Code: Select all

GetTickCount64()  ; Modified (3/13/2035) to include this Import sugested by Azolotl
You mean, you'll modify this in ten years with the code from our well-known forum member Azolotl. :wink:

Re: "uptime" for Windows

Posted: Thu Mar 13, 2025 11:52 pm
by Randy Walker
jacdelad wrote: Thu Mar 13, 2025 9:37 pm

Code: Select all

GetTickCount64()  ; Modified (3/13/2035) to include this Import sugested by Azolotl
You mean, you'll modify this in ten years with the code from our well-known forum member Azolotl. :wink:
:lol:
Not just a master of I/O errors. I'm also master of typos. :oops:

Re: "uptime" for Windows

Posted: Thu Mar 13, 2025 11:58 pm
by Randy Walker
jacdelad wrote: Thu Mar 13, 2025 9:37 pm ... our well-known forum member Azolotl. :wink:
I think he's related to Aristotle. :)