[REVISED] "uptime" for Windows

Windows specific forum
Randy Walker
Addict
Addict
Posts: 1058
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

[REVISED] "uptime" for Windows

Post 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.
Last edited by Randy Walker on Fri Aug 29, 2025 9:10 pm, edited 5 times in total.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
BarryG
Addict
Addict
Posts: 4168
Joined: Thu Apr 18, 2019 8:17 am

Re: "uptime" for Windows

Post by BarryG »

Cool. Matches what Task Manager says, which I was hoping it would. :)
User avatar
idle
Always Here
Always Here
Posts: 5887
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: "uptime" for Windows

Post by idle »

works well. :D
Randy Walker
Addict
Addict
Posts: 1058
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: "uptime" for Windows

Post 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!
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 1058
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: "uptime" for Windows

Post by Randy Walker »

Also a lot faster than typing systeminfo | find /i "Boot Time" into powershell.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
User avatar
idle
Always Here
Always Here
Posts: 5887
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: "uptime" for Windows

Post 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.
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: "uptime" for Windows

Post by Caronte3D »

:lol:
BarryG
Addict
Addict
Posts: 4168
Joined: Thu Apr 18, 2019 8:17 am

Re: "uptime" for Windows

Post 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?
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: "uptime" for Windows

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

Re: "uptime" for Windows

Post 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.
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).
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: "uptime" for Windows

Post 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.
Randy Walker
Addict
Addict
Posts: 1058
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: "uptime" for Windows

Post 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 !!!
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
User avatar
jacdelad
Addict
Addict
Posts: 2004
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: "uptime" for Windows

Post 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:
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
Randy Walker
Addict
Addict
Posts: 1058
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: "uptime" for Windows

Post 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:
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 1058
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: "uptime" for Windows

Post 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. :)
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Post Reply