Jump to content

_DateDiff giving me 0 when working with variables


Recommended Posts

Guys,

I'm experiencing something hard to understand in this code: 

Why it gives me 0 when I use the variable? It looks JUST like the string that I used for calc.

I had to add two different date treatments to run in servers with different languages.

#include <Date.au3>
#include <Constants.au3>
#include <Array.au3>

;All variables
Global $line, $sRealResult, $aArray, $sDateHour, $aValues, $sValue1, $sValue2, $aArrayY, $aArrayM, $aArrayD, $sDataFinalBR, $sDataFinalEN

funcCalcDateLocal()

Func funcCalcDateLocal()
    Local $LocalTime = 'NET TIME \\localhost | findstr /C:"hora" | findstr /v "^$"' ;Command to execute
    $bResult = Run(@ComSpec & " /c " & $LocalTime, @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD) ;Execute the command
    While 1
        $line = StdoutRead($bResult)
        If $line <> "" Then ;Eliminate the first blank result
            $sRealResult = $line
            If @error Then ExitLoop
            ExitLoop
        EndIf
    WEnd
    StdioClose($bResult)
EndFunc   ;==>funcCalcDateLocal

$aArray = StringSplit($sRealResult, ' ‚ ', 1) ;Eliminate the comma and split it
$sDateHour = $aArray[2]; this is the date " " hour
$aValues = StringSplit($sDateHour, ' ', 1) ;Split them
$sValue1 = $aValues[1] ; Then Date
$sValue2 = $aValues[2] ; and Hour

$aArrayDate = StringSplit($sValue1, '/', 1) ;Split them in order to later place them in the right sequence for the servers with different languages

$aArrayY = $aArrayDate[3] ; year
$aArrayM = $aArrayDate[2] ; month
$aArrayD = $aArrayDate[1] ; day

$sDataFinalBR = $aArrayY & "/" & $aArrayM & "/" & $aArrayD & " " & $sValue2 ; Server PT-BR final date
$sDataFinalEN = $aArrayY & "/" & $aArrayD & "/" & $aArrayM & " " & $sValue2 ; Server EN-US final date

MsgBox(64, "Data BR", $sDataFinalBR)
MsgBox(64, "Data EN", $sDataFinalEN)

Local $iDateCalc1 = _DateDiff('s', $sDataFinalBR, "2017/04/19 16:02:52") ; test 1
Local $iDateCalc2 = _DateDiff('s', "2017/04/19 16:02:52", $sDataFinalBR) ; test 2
Local $iDateCalc3 = _DateDiff('s', "2017/04/19 16:02:52", "2017/04/19 16:02:53") ; test 3

MsgBox(64, "Date Calc - Test 1", $iDateCalc1)
MsgBox(64, "Date Calc - Test 2", $iDateCalc2)
MsgBox(64, "Date Calc - Test 3", $iDateCalc3)

 

Link to comment
Share on other sites

  • Developers

So what is the exact content of $sDataFinalBR ?
Just add this line to the script and show me the generated output: Consolewrite("$sDataFinalBR=" & $sDataFinalBR & "|" & @crlf)

Jos

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

You could use WMI to get the information for example:

#include <Array.au3>

Local $aDate = _WMIDateTime()
_ArrayDisplay($aDate)

Func _WMIDateTime($sComputerName = @ComputerName)
    Local $aDateTime[0]
    Local $objWMIService = ObjGet("winmgmts:\\" & $sComputerName & "\root\cimv2")
    Local $colItems = $objWMIService.ExecQuery("Select * From Win32_LocalTime")
    For $objItem in $colItems
        _ArrayAdd($aDateTime, $objItem.Year)
        _ArrayAdd($aDateTime, $objItem.Month)
        _ArrayAdd($aDateTime, $objItem.Day)
        _ArrayAdd($aDateTime, $objItem.Hour)
        _ArrayAdd($aDateTime, $objItem.Minute)
        _ArrayAdd($aDateTime, $objItem.Second)
        _ArrayAdd($aDateTime, $objItem.Milliseconds)
        _ArrayAdd($aDateTime, $objItem.DayOfWeek)
        _ArrayAdd($aDateTime, $objItem.WeekInMonth)
        _ArrayAdd($aDateTime, $objItem.Quarter)
    Next
    Return $aDateTime
EndFunc

 

Link to comment
Share on other sites

13 hours ago, Jos said:

So what is the exact content of $sDataFinalBR ?
Just add this line to the script and show me the generated output: Consolewrite("$sDataFinalBR=" & $sDataFinalBR & "|" & @crlf)

Jos

Hey, Jos.

The whole console output is as follows:

>Running AU3Check (1.54.22.0)  from:C:\Program Files (x86)\AutoIt3
+>07:18:25 AU3Check ended.rc:0
>Running:(3.3.8.1):C:\Program Files (x86)\AutoIt3\autoit3.exe "C:\Users\davidl\Documents\TESTE.au3"    
--> Press Ctrl+Alt+F5 to Restart or Ctrl+Break to Stop
$sDataFinalBR=2017/04/20 07:18:26
|
+>07:18:32 AutoIt3.exe ended.rc:0
>Exit code: 0    Time: 8.742

 

12 hours ago, Subz said:

You could use WMI to get the information 

 

Thanks a lot for the great example, Subz. But I will have to compare to a time from a Domain Controller later. The question here is WHY _DateDiff is giving me zero when I compare it with a variable. And if instead of the variable, I use the exact copied string from its value, it gives me the comparison I need.

Link to comment
Share on other sites

If you use StringStripWS($sDataFInalBR, 7) what does it return, if you look at the two examples below, the first returns the correct information however if you have a prefix space it will return 0.

#include <Date.au3>
$sDataFinalBR = "2017/04/20 07:18:26"
Local $iDateCalc1 = _DateDiff('s', $sDataFinalBR, "2017/04/19 16:02:52") ; test 1
Local $iDateCalc2 = _DateDiff('s', "2017/04/19 16:02:52", $sDataFinalBR) ; test 2
Local $iDateCalc3 = _DateDiff('s', "2017/04/19 16:02:52", "2017/04/19 16:02:53") ; test 3

MsgBox(64, "Date Calc - Test 1", $iDateCalc1)
MsgBox(64, "Date Calc - Test 2", $iDateCalc2)
MsgBox(64, "Date Calc - Test 3", $iDateCalc3)

$sDataFinalBR = " 2017/04/20 07:18:26"
Local $iDateCalc1 = _DateDiff('s', $sDataFinalBR, "2017/04/19 16:02:52") ; test 1
Local $iDateCalc2 = _DateDiff('s', "2017/04/19 16:02:52", $sDataFinalBR) ; test 2
Local $iDateCalc3 = _DateDiff('s', "2017/04/19 16:02:52", "2017/04/19 16:02:53") ; test 3

MsgBox(64, "Date Calc - Test 1", $iDateCalc1)
MsgBox(64, "Date Calc - Test 2", $iDateCalc2)
MsgBox(64, "Date Calc - Test 3", $iDateCalc3)

 

Edited by Subz
Link to comment
Share on other sites

  • Developers
57 minutes ago, DavidLago said:

The whole console output is as follows:

As you can see: there is an @LF or @CRLF added to the end of the content of the variable. Strip[ it and it will work.

Jos

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

17 minutes ago, Jos said:

As you can see: there is an @LF or @CRLF added to the end of the content of the variable. Strip[ it and it will work.

Jos

Yes!

The sleepy-head got it now. Thanks to @Jos and @Subz

I did this:

 

Local $sDataBR = $aArrayY & "/" & $aArrayM & "/" & $aArrayD & " " & $sValue2 ; Server PT-BR date untreated
Local $sDataEN = $aArrayY & "/" & $aArrayD & "/" & $aArrayM & " " & $sValue2 ; Server EN-US date untreated

$sDataFinalBR = StringStripWS($sDataBR, 7) ;Server PT-BR final date
$sDataFinalEN = StringStripWS($sDataEN, 7) ;Server EN-US final date

Now it's calculating perfectly.

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

×
×
  • Create New...