Jump to content

Help with getting proper time diffrence


MadBoy
 Share

Recommended Posts

I'm writting a program and need little time comparision. What i would like to achieve is to be able to compare to know how much time it passed since boot. So i created some functions that give current time and time of a boot. While i could simply compare $boot_min with $current_min when other $boot.. and $current.. variables match i came to point where i think it's a bit bare solution (and wouldn't realy work if its 23:59 since then the day will change and $boot_day and $current_day will be diffrent. I'm preety sure i seen this somewhere on forum but couldn't find it this time. More I think about the problem the more i go away from solution :) My brain is empty today.

#include <Date.au3>
#include <array.au3>
$strComputer = "localhost"

Global $time_of_boot, $current_time 

_GatherTimeBoot()
    

Func _GatherTimeBoot()
Local $time_current, $time_of_boot
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
       $time_of_boot = StringLeft($objItem.LastBootUpTime, 14)
       $time_current = StringLeft($objItem.LocalDateTime, 14)
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_OperatingSystem" )
   Exit
Endif

$boot_year = StringLeft($time_of_boot, 4)
$boot_month = StringMid($time_of_boot, 5,2)
$boot_day = StringMid($time_of_boot, 7,2)
$boot_hour = StringMid($time_of_boot, 9,2)
$boot_min = StringMid($time_of_boot, 11,2)
$boot_sec = StringMid($time_of_boot, 13,2)
$current_year = StringLeft($time_current, 4)
$current_month = StringMid($time_current, 5,2)
$current_day = StringMid($time_current, 7,2)
$current_hour = StringMid($time_current, 9,2)
$current_min = StringMid($time_current, 11,2)
$current_sec = StringMid($time_current, 13,2)
$boot_time = _ArrayCreate("6", $boot_year, $boot_month, $boot_day, $boot_hour, $boot_min, $boot_sec)
$current_time = _ArrayCreate("6", $current_year, $current_month , $current_day, $current_hour, $current_min, $current_sec)
EndFunc

Func WMIDateStringToDate($dtmDate)
    Return (StringMid($dtmDate, 5, 2) & "/" & _
            StringMid($dtmDate, 7, 2) & "/" & StringLeft($dtmDate, 4) _
             & " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate, 13, 2))
EndFunc   ;==>WMIDateStringToDate

Thanks,

MadBoy

My little company: Evotec (PL version: Evotec)

Link to comment
Share on other sites

  • Developers

does this one help ?

Func PC_UPTIME()
    Local $day = ""
    Local $type = ""
    Local $hour = 0
    Local $min = 0
    Local $sec = 0
    Local $uptime
;
    $ret = DllCall("kernel32.dll", "long", "GetTickCount")
    If IsArray($ret) Then
        $msec = StringRight("00" & Mod($ret[0], 1000), 3)
        $uptime = Int($ret[0] / 1000)
        $sec = StringRight("00" & Mod($uptime, 60), 2)
        If $uptime >= 60 Then
            $uptime = Int($uptime / 60)
            $min = StringRight("00" & Mod($uptime, 60), 2)
            If $uptime >= 60 Then
                $uptime = Int($uptime / 60)
                $hour = StringRight("00" & Mod($uptime, 24), 2)
                If $uptime >= 24 Then; convert hours to days
                    $day = Int($uptime / 24)
                    $type = ""
                    If $day > 1 Then $type = "s"
                    $type = " Day" & $type & " "
                EndIf
            EndIf
        EndIf
        Return ($day & $type & $hour & ":" & $min & ":" & $sec)
    EndIf
EndFunc  ;==>PC_UPTIME

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

does this one help ?

Func PC_UPTIME()
     Local $day = ""
     Local $type = ""
     Local $hour = 0
     Local $min = 0
     Local $sec = 0
     Local $uptime
;
     $ret = DllCall("kernel32.dll", "long", "GetTickCount")
     If IsArray($ret) Then
         $msec = StringRight("00" & Mod($ret[0], 1000), 3)
         $uptime = Int($ret[0] / 1000)
         $sec = StringRight("00" & Mod($uptime, 60), 2)
         If $uptime >= 60 Then
             $uptime = Int($uptime / 60)
             $min = StringRight("00" & Mod($uptime, 60), 2)
             If $uptime >= 60 Then
                 $uptime = Int($uptime / 60)
                 $hour = StringRight("00" & Mod($uptime, 24), 2)
                 If $uptime >= 24 Then; convert hours to days
                     $day = Int($uptime / 24)
                     $type = ""
                     If $day > 1 Then $type = "s"
                     $type = " Day" & $type & " "
                 EndIf
             EndIf
         EndIf
         Return ($day & $type & $hour & ":" & $min & ":" & $sec)
     EndIf
 EndFunc ;==>PC_UPTIME
Thank You. Exactly what i was looking for. Just couldn't find it :)

My little company: Evotec (PL version: Evotec)

Link to comment
Share on other sites

Not sure if this applies anymore but don't GetTickCount have a turnover point (It says 49.7 days in the link)? This thread seems to address the issue, even if a "complete" solution is not found due to sleep modes and such.

Link to comment
Share on other sites

There is an issue with LastBootTime not being set correctly sometimes. This page shows an alternative using Win32_PerfRawData_PerfOS_System :

http://groups.google.co.uk/group/microsoft...5010130e03e7617

Check this out:

; Generated by AutoIt Scriptomatic

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_System", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
      $Output &= "SystemUpTime: " & $objItem.SystemUpTime & " seconds" & @CRLF
      $Output &= "SystemUpTime: " & $objItem.SystemUpTime / 60 & " minutes" & @CRLF
      $Output &= "SystemUpTime: " & ($objItem.SystemUpTime / 60) /60 & " hours" & @CRLF
      if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
      $Output=""
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_PerfFormattedData_PerfOS_System" )
Endif
Link to comment
Share on other sites

Not sure if this applies anymore but don't GetTickCount have a turnover point (It says 49.7 days in the link)? This thread seems to address the issue, even if a "complete" solution is not found due to sleep modes and such.

Uten fortunetly for my little project that won't be an issue :) Thanks for information!

My little company: Evotec (PL version: Evotec)

Link to comment
Share on other sites

  • 3 months later...

I found the following modification of Jos's code to be useful in writing a startup program. Using this function, my script will wait until more important startup programs have had time to load, before it does any intensive processing.

MsgBox(64, "_PC_UptimeInMinutes()", _PC_UptimeInMinutes())
If _PC_UptimeInMinutes() < 5 Then
    Sleep(140000)
EndIf
Func _PC_UptimeInMinutes()
    Local $retVal
    $retVal = DllCall("kernel32.dll", "long", "GetTickCount")
    Return Round($retVal[0]/1000/60)
EndFunc   ;==>_PC_UptimeInMinutes

Snippet Scrounger's rating - :)

Edited by Squirrely1

Das Häschen benutzt Radar

Link to comment
Share on other sites

  • 3 years later...

There is an issue with LastBootTime not being set correctly sometimes. This page shows an alternative using Win32_PerfRawData_PerfOS_System :

http://groups.google.co.uk/group/microsoft...5010130e03e7617

Check this out:

; Generated by AutoIt Scriptomatic

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_System", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
      $Output &= "SystemUpTime: " & $objItem.SystemUpTime & " seconds" & @CRLF
      $Output &= "SystemUpTime: " & $objItem.SystemUpTime / 60 & " minutes" & @CRLF
      $Output &= "SystemUpTime: " & ($objItem.SystemUpTime / 60) /60 & " hours" & @CRLF
      if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
      $Output=""
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_PerfFormattedData_PerfOS_System" )
Endif

This code works for most of my workstations, but some do return a negative number ... any idea why?
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...