Geting the system uptime on Windows

Share your advanced PureBasic knowledge/code with the community.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Geting the system uptime on Windows

Post by Mistrel »

I've rebooted the system several times since then so the number there is way off.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Geting the system uptime on Windows

Post by PB »

I see. That's weird, then, since Microsoft is supplying the info.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
pcfreak
User
User
Posts: 75
Joined: Sat May 22, 2004 1:38 am

Re: Geting the system uptime on Windows

Post by pcfreak »

There was a post once about a bit more tricky way to get the system uptime: http://www.purebasic.fr/english/viewtop ... 12&t=39017
Just do something like

Code: Select all

Procedure.s S2Str(time.q)
 time=time
 d.f=time/86400
 d=Round(d,0)
 h.f=time/3600-(d*24)
 h=Round(h,0)
 m.f=time/60-(d*1440)-(h*60)
 m=Round(m,0)
 s.f=time-(d*86400)-(h*3600)-(m*60)
 s=Round(s,0)
 string.s=""
 If d>0 : string+Str(d)+"days " : EndIf
  If h>0 : string+Str(h)+"houres " : EndIf
   If m>0 : string+Str(m)+"minutes " : EndIf
    If s>0 : string+Str(s)+"seconds" : EndIf
ProcedureReturn string
EndProcedure

Debug S2Str(IntQ(GetInterruptTime() / 10000000))
to see the formatted output.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Geting the system uptime on Windows

Post by Mistrel »

Most impressive! Is this in milliseconds, counting from 0 after a reboot?
User avatar
pcfreak
User
User
Posts: 75
Joined: Sat May 22, 2004 1:38 am

Re: Geting the system uptime on Windows

Post by pcfreak »

No, it is in 100 nanoseconds since boot. That's why the division by 10000000 to get to seconds.
User avatar
GeBonet
Enthusiast
Enthusiast
Posts: 135
Joined: Fri Apr 04, 2008 6:20 pm
Location: Belgium

Re: Geting the system uptime on Windows

Post by GeBonet »

Hi !
First, Happy New Year !
Now I'm no expert, but where is the difference with your procedures and:

Code: Select all

Temp.f=ElapsedMilliseconds()/1000
Debug Temp
H=Int(Temp/3600)
Debug "Heures  : "+Str(H)               ; Nombre d'heures...
M=Int(Temp/60)-H*60
Debug "Minutes : "+Str(M)               ; Nombre de minutes
Debug "----------------------"
I think it's the accuracy? Or what? :wink:
Thank you :!:
Sorry for my english :wink: ! (Windows Xp, Vista and Windows 7, Windows 10)
User avatar
pcfreak
User
User
Posts: 75
Joined: Sat May 22, 2004 1:38 am

Re: Geting the system uptime on Windows

Post by pcfreak »

Well, the second call is later of course, but measured in seconds I get the same result:

Code: Select all

;Public domain, created by Rescator.
;Based on info found at http://www.dcl.hpi.uni-potsdam.de/research/WRK/?p=34
CompilerIf #PB_Compiler_Processor=#PB_Processor_x86
Procedure.q GetInterruptTime()
  !_GetInterruptTime_Repeat_Start:
   !MOV edx,dword [7FFE0008h+4]
   !MOV eax,dword [7FFE0008h]
   !MOV ecx,dword [7FFE0008h+8]
   !CMP edx,ecx
   !JNE _GetInterruptTime_Repeat_Start
  ProcedureReturn
EndProcedure
CompilerElse
Procedure.q GetInterruptTime()
  !MOV qword rdx,7FFE0008h
  !_GetInterruptTime_Repeat_Start:
   !MOVSXD rax,dword [rdx+4]
   !MOVSXD rbx,dword [rdx]
   !MOVSXD rcx,dword [rdx+8]
   !CMP rax,rcx
   !JNE _GetInterruptTime_Repeat_Start
  !SAL rax,32
  !ADD rax,rbx
  ProcedureReturn
EndProcedure
CompilerEndIf

Procedure.s S2Str(time.q)
 time=time
 d.f=time/86400
 d=Round(d,0)
 h.f=time/3600-(d*24)
 h=Round(h,0)
 m.f=time/60-(d*1440)-(h*60)
 m=Round(m,0)
 s.f=time-(d*86400)-(h*3600)-(m*60)
 s=Round(s,0)
 string.s=""
 If d>0 : string+Str(d)+"days " : EndIf
  If h>0 : string+Str(h)+"houres " : EndIf
   If m>0 : string+Str(m)+"minutes " : EndIf
    If s>0 : string+Str(s)+"seconds" : EndIf
ProcedureReturn string
EndProcedure

Debug S2Str(IntQ(GetInterruptTime() / 10000000))
Debug S2Str(IntQ(ElapsedMilliseconds() / 1000))
C64
Enthusiast
Enthusiast
Posts: 151
Joined: Sat Dec 18, 2010 4:40 am

Re: Geting the system uptime on Windows

Post by C64 »

pcfreak wrote:

Code: Select all

Procedure.s S2Str(time.q)
time=time
:?: Time equals time at the start of a procedure? Why?
User avatar
pcfreak
User
User
Posts: 75
Joined: Sat May 22, 2004 1:38 am

Re: Geting the system uptime on Windows

Post by pcfreak »

Ups, just ignore that ;)
User avatar
em_uk
Enthusiast
Enthusiast
Posts: 366
Joined: Sun Aug 08, 2010 3:32 pm
Location: Manchester UK

Re: Geting the system uptime on Windows

Post by em_uk »

Here is an adaptaions of PB's way. I use Net statistics workstation to get the up time.

Works pretty much instantly :

Code: Select all

Procedure.s UpTime() ; For XP or higher. Pretty much instant now :) ~ em_uk
  p=RunProgram("cmd.exe","/c net statistics workstation ","",#PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
  If p
    While ProgramRunning(p) : o$+ReadProgramString(p)+#CRLF$ : Wend : CloseProgram(p)
    up$=Trim(Mid(o$,FindString(o$,"Statistics since",1)+16)) : up$=Left(up$,FindString(up$,#CRLF$,1)-1)
  EndIf
  ProcedureReturn up$
EndProcedure

Debug UpTime()
----

R Tape loading error, 0:1
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Re: Geting the system uptime on Windows

Post by NoahPhense »

Nice..
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Geting the system uptime on Windows

Post by jassing »

Why shell to cmd 1st? why not just call net.exe ?

Code: Select all

Procedure.s UpTime() ; For XP or higher. Pretty much instant now :) ~ em_uk
  p=RunProgram("net.exe","statistics workstation ","",#PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
  If p
    While ProgramRunning(p) : o$+ReadProgramString(p)+#CRLF$ : Wend : CloseProgram(p)
    up$=Trim(Mid(o$,FindString(o$,"Statistics since",1)+16)) : up$=Left(up$,FindString(up$,#CRLF$,1)-1)
  EndIf
  ProcedureReturn up$
EndProcedure

Debug UpTime()
User avatar
pcfreak
User
User
Posts: 75
Joined: Sat May 22, 2004 1:38 am

Re: Geting the system uptime on Windows

Post by pcfreak »

But only works if the system is English.
Post Reply