Jump to content
DigDeep

Date_Time_GetSystemTime ( ) giving false results

Recommended Posts

DigDeep

Hi,

Since sometime I have been seeing a weird issue that all my applications are sometimes giving previous day date instead of current date.

After looking at all the sections I finally found if the machine was turned ON prior 12am and the application launches after 12 am which is probably the next day of boot, Date_Time_GetSystemTime ( ) shows the previous day date.

Eg. Today is 19/11/2017. No matter how many times I will launch the application during the day will always show me the current date as 19/11/2017. But if I will launch the application after 12am on 20/11/2017 this function for some reason reads the bootup time as 19th and displays the same.

Can someone test this once and tell me if it's just me? I will then provide the code.

 

 

Edited by DigDeep

Share this post


Link to post
Share on other sites
argumentum

Don't have to show more than the pertinent code section in a form of an example.
Is very unlikely that the call to kernel32.dll fails.

Share this post


Link to post
Share on other sites
DigDeep

@argumentum, could you please try out this sample once?

If you try it out prior 12AM it will report correctly but if you do not restart the machine and run the same sample after 12AM, it will tell "Alert! System has not booted since more than 2 days", instead of "System was booted up yesterday".

#include <Process.au3>
#include <Date.au3>


$BootFile = 'C:\Temp\GetBootUpDate.txt'
_RunDos(@SystemDir & '\wbem\WMIC.exe OS get LastBootUpTime > C:\Temp\GetBootUpDate.txt')
$GetBD = StringLeft(FileReadLine($BootFile, 2), 8)

$SysUpDate = StringMid($GetBD, 5, 2) & '/' & StringRight($GetBD, 2) & '/' & StringLeft($GetBD, 4)
    Local $TodayDate = _Date_Time_GetSystemTime()
    Local $TodayDate1 = _Date_Time_SystemTimeToDateStr($TodayDate)
    Local $PreviousDayDate = _DateAdd('d', -1, _NowCalcDate())
    $GetPreviousDayDate = StringSplit($PreviousDayDate, "/")
    $sGetPreviousDayDate = $GetPreviousDayDate[2] & "/" & $GetPreviousDayDate[3] & "/" & $GetPreviousDayDate[1]

    If $SysUpDate = $TodayDate1 Then
        MsgBox(0, "", "System was booted up today.")

    ElseIf $SysUpDate = $sGetPreviousDayDate Then ; This is to check if the application ran after 12 AM, it should mean as day2 and not same day.
        MsgBox(0, "", "System was booted up yesterday.")

    Else
        MsgBox(0, "", "Alert! System has not booted since more than 2 days.")
    EndIf

 

Share this post


Link to post
Share on other sites
argumentum
5 hours ago, DigDeep said:

run the same sample after 12AM

UTC time vs. local time =/
 

#include <Process.au3>
#include <Date.au3>

Local $sMsg = ""
$BootFile = @ScriptDir & '\GetBootUpDate.txt'
_RunDos(@SystemDir & '\wbem\WMIC.exe OS get LastBootUpTime > "' & $BootFile & '"')
$GetBD = StringLeft(FileReadLine($BootFile, 2), 8)
$sMsg &= '> $GetBD >' & $GetBD & '<' & @CR
$SysUpDate = StringMid($GetBD, 5, 2) & '/' & StringRight($GetBD, 2) & '/' & StringLeft($GetBD, 4)
$sMsg &= '> $SysUpDate >' & $SysUpDate & '<' & @CR

Local $TodayDate = _Date_Time_GetSystemTime() ; Retrieves the current system date and time expressed in UTC
Local $TodayDate1 = _Date_Time_SystemTimeToDateStr($TodayDate)
$sMsg &= '> $TodayDate1 >' & $TodayDate1 & '<-- UTC time' & @CR

Local $PreviousDayDate = _DateAdd('d', -1, _NowCalcDate())
$sMsg &= '> $PreviousDayDate >' & $PreviousDayDate & '<' & @CRLF

$GetPreviousDayDate = StringSplit($PreviousDayDate, "/")
$sGetPreviousDayDate = $GetPreviousDayDate[2] & "/" & $GetPreviousDayDate[3] & "/" & $GetPreviousDayDate[1]
$sMsg &= '> $sGetPreviousDayDate >' & $sGetPreviousDayDate & '<' & @CR & @CR

$sMsg &=  'If ' & $SysUpDate & ' = ' & $TodayDate1 & ' Then '& @CR
$sMsg &=  'ElseIf ' & $SysUpDate & ' = ' & $sGetPreviousDayDate & ' Then '& @CR

MsgBox(262144, "", $sMsg)

If $SysUpDate = $TodayDate1 Then
    MsgBox(0, "", "System was booted up today.")
ElseIf $SysUpDate = $sGetPreviousDayDate Then ; This is to check if the application ran after 12 AM, it should mean as day2 and not same day.
    MsgBox(0, "", "System was booted up yesterday.")
Else
    MsgBox(0, "", "Alert! System has not booted since more than 2 days.")
EndIf

 

Share this post


Link to post
Share on other sites
argumentum
#include <WinAPISys.au3>
#include <Date.au3>

Switch DaysSinceBoot()
    Case -1
        MsgBox(262144, "", "error =(")
    Case 0
        MsgBox(262144, "", "System was booted up today.")
    Case 1
        MsgBox(262144, "", "System was booted up yesterday.")
    Case Else
        MsgBox(262144, "", "Alert! System has not booted since more than 2 days.")
EndSwitch

Func DaysSinceBoot()
    ; by argumentum ; https://www.autoitscript.com/forum/topic/191260-date_time_getsystemtime-giving-false-results/?do=findComment&comment=1371960
    Local $ret = _WinAPI_GetTickCount()
    If @error Then Return SetError(1, 0, -1)
    Local $uptime = Int($ret / 1000) ; seconds since boot
    Local $bootDate = StringLeft(_DateAdd("s", $uptime, _NowCalc()), 10) ; boot date
    Local $bootDays = _DateDiff("D", StringLeft(_NowCalc(), 10), $bootDate) ; days since midnight
    Return SetError(0, $uptime, $bootDays)
EndFunc   ;==>DaysSinceBoot

:)

Edited by argumentum
fixed the code

Share this post


Link to post
Share on other sites
DigDeep

@argumentum, I tried with the 1st sample first and on my multiple machines. Prior 12AM the message was correct as usual. But After 12 AM, it still says,

"System was booted up today."

Attaching the 4 snapshots below

This is correct...

Msg 1.jpg = Prior 12AM (11/20/2017)

Msg 2.jpg = Prior 12AM.

 

This should have shown as:

$TodayDate1 > 11/21/2017 (This is the issue)

Msg 3.jpg = After 12AM (11/21/2017)

Msg 4.jpg = This should have given

"System was booted up yesterday."

Msg 1.jpg

Msg 2.jpg

Msg 3.jpg

Msg 4.jpg

Share this post


Link to post
Share on other sites
DigDeep

I did not tried your 2nd sample prior 12AM but only after 12AM and it is still doing the same.

Share this post


Link to post
Share on other sites
BrewManNH

What time zone are you in?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
iamtheky

consolewrite all variables and nowcalc(),  show results of this please such that we can all play along at home

Func DaysSinceBoot()
    ; by argumentum ; https://www.autoitscript.com/forum/topic/191260-date_time_getsystemtime-giving-false-results/?do=findComment&comment=1371960
    Local $ret = _WinAPI_GetTickCount()
    consolewrite($ret & @CRLF)
    If @error Then Return SetError(1, 0, -1)
    Local $uptime = Int($ret / 1000) ; seconds since boot
    consolewrite($uptime & @CRLF)
    Local $bootDate = StringLeft(_DateAdd("s", $uptime, _NowCalc()), 10) ; boot date
    consolewrite(_NowCalc() & @CRLF)
    consolewrite($bootDate & @CRLF)
    Local $bootDays = _DateDiff("D", StringLeft(_NowCalc(), 10), $bootDate) ; days since midnight
    consolewrite($bootDays & @CRLF)
    Return SetError(0, $uptime, $bootDays)
EndFunc   ;==>DaysSinceBoot

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
DigDeep

I am in IST zone.

Share this post


Link to post
Share on other sites
BrewManNH

So, being in IST, that means that at midnight, _Date_Time_GetSystemTime will return 6:30pm, because you're 5.5 hours ahead of UTC time. You have to take that into account because _Date_Time_GetSystemTime works with UTC time and not local time.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
DigDeep

Can we get the above sample set for both IST and UTC time? so it detects the time zone and then get zone date?

Share this post


Link to post
Share on other sites
BrewManNH

Perhaps try _Date_Time_GetTimeZoneInformation to find that out, see the help file.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
argumentum

@argumentum, I tried with the 1st sample

I was showing the error in the code. UTC vs. local time.

Just use post #6 and forget the WMIC stuff.
post #6 does not care about the time zone.

Edited by argumentum

Share this post


Link to post
Share on other sites
DigDeep

I'll try out and come back. Thanks for staying.

Share this post


Link to post
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

×