DavidLago

_DateDiff giving me 0 when working with variables

8 posts in this topic

#1 ·  Posted

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)

 


Just a server analyst that has never been into programming that much. So, small fish here :)

Share this post


Link to post
Share on other sites



#2 ·  Posted

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


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#3 ·  Posted

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

 

Share this post


Link to post
Share on other sites

#4 ·  Posted

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.


Just a server analyst that has never been into programming that much. So, small fish here :)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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
1 person likes this

Share this post


Link to post
Share on other sites

#6 ·  Posted

@Subz I just tested the variable output to a consolewrite and it showed no prefix spaces, just the exact format of the string that is supposed to work.

Check the attached image. Am I missing something?

Screenshot - 20_04_2017 , 08_08_25.png


Just a server analyst that has never been into programming that much. So, small fish here :)

Share this post


Link to post
Share on other sites

#7 ·  Posted

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


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#8 ·  Posted

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.


Just a server analyst that has never been into programming that much. So, small fish here :)

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

  • Similar Content

    • ur
      By ur
      I am assigning the current time stamp to a variable $strtTime and holding the execution for 5 seconds with Sleep and calculating the current time stamp.
      Ideally, the _DateDiff should give 5 seconds as output as i gave the parameter as s.But it is giving zero 0.
      Any suggestions....!
      Global $startTime = _Date_Time_GetLocalTime() $startTime = _Date_Time_SystemTimeToDateTimeStr($startTime) Sleep(5000)     $tCur = _Date_Time_GetLocalTime()     $tCur = _Date_Time_SystemTimeToDateTimeStr($tCur) $iDateCalc = _DateDiff('s', $startTime, $tCur)     MsgBox(0,"$iDateCalc",$iDateCalc)     FileWrite ($QTPfile,CHr(34) & $tCur & CHr(34) & Chr(44) & """" & $iDateCalc & """,")