Jump to content

Recommended Posts

Posted (edited)

I have spent 2 days, trying to figure out why this is not returing the correct compare of dates. It is meant to compare 2 dates in the format in format of "Year:YearDay:Hour:Min". For some reason on this comparing, it returns the wrong answer, The return is the same as the UDF date.au3 _Date_Time_CompareFileTime function. if 1st date is earlier it is supposed to return -1, 0 if they are equal, and 1 if the 1st date is later than the second date.

This Is my Function, and the 2 dates, I found an error with. This returns a '-1' instead of '1' as I would have expected. Hopefully some1 can see what I am missing here.

MsgBox(0,"",CompareDate("2010:225:10:01","2010:225:5:18"))
Func CompareDate($pFileTime1,$pFileTime2)
            $tc1=StringSplit($pFileTime1,":")
            $tc2=StringSplit($pFileTime2,":")

            If $tc1[1]>$tc2[1] Then Return(1)
            If $tc1[1]<$tc2[1] Then Return(-1)
            If $tc1[1]=$tc2[1] And $tc1[2]>$tc2[2] Then Return(1)
            If $tc1[1]=$tc2[1] And $tc1[2]<$tc2[2] Then Return(-1)
            If $tc1[1]=$tc2[1] And $tc1[2]=$tc2[2] And $tc1[3]>$tc2[3] Then Return(1)
            If $tc1[1]=$tc2[1] And $tc1[2]=$tc2[2] And $tc1[3]<$tc2[3] Then Return(-1)
            If $tc1[1]=$tc2[1] And $tc1[2]=$tc2[2] And $tc1[3]=$tc2[3] And $tc1[4]>$tc2[4] Then Return(1)
            If $tc1[1]=$tc2[1] And $tc1[2]=$tc2[2] And $tc1[3]=$tc2[3] And $tc1[4]<$tc2[4] Then Return(-1)
            If $tc1[1]=$tc2[1] And $tc1[2]=$tc2[2] And $tc1[3]=$tc2[3] And $tc1[4]=$tc2[4] Then Return(0)
EndFunc
Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry. 

Posted

Think about it. StringSplit returns an array of strings. Do you want a string comparison? No.

You want a number comparison:

MsgBox(0,"",CompareDate("2010:225:10:01","2010:225:5:18"))
Func CompareDate($pFileTime1,$pFileTime2)
            $tc1=StringSplit($pFileTime1,":")
            $tc2=StringSplit($pFileTime2,":")

            If Number($tc1[1]) > Number($tc2[1]) Then Return(1) ;Either put Number in front of everything
            If Number($tc1[1]) < Number($tc2[1]) Then Return(-1)

            For $iX = 1 To $tc1[0] ;Or just convert all before
                $tc1[$iX] = Number($tc1[$iX])
            Next
            For $iX = 1 To $tc2[0]
                $tc2[$iX] = Number($tc2[$iX])
            Next

            If $tc1[1]=$tc2[1] And $tc1[2]>$tc2[2] Then Return(1)
            If $tc1[1]=$tc2[1] And $tc1[2]<$tc2[2] Then Return(-1)
            If $tc1[1]=$tc2[1] And $tc1[2]=$tc2[2] And $tc1[3]>$tc2[3] Then Return(1)
            If $tc1[1]=$tc2[1] And $tc1[2]=$tc2[2] And $tc1[3]<$tc2[3] Then Return(-1)
            If $tc1[1]=$tc2[1] And $tc1[2]=$tc2[2] And $tc1[3]=$tc2[3] And $tc1[4]>$tc2[4] Then Return(1)
            If $tc1[1]=$tc2[1] And $tc1[2]=$tc2[2] And $tc1[3]=$tc2[3] And $tc1[4]<$tc2[4] Then Return(-1)
            If $tc1[1]=$tc2[1] And $tc1[2]=$tc2[2] And $tc1[3]=$tc2[3] And $tc1[4]=$tc2[4] Then Return(0)
EndFunc

Atleast I think so. Am I wrong? ;)

Posted

Converted to Number on all strings, and the test finally gave me the correct return. I was assuming it would compare whole string verse string, instead of character steps. Originally, I thought I had an operator backwards somewhere, and just couldn't see it myself. Thanks for you help ;)

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry. 

Posted

It is good practice to use the number thing.

Also I think you need to look at loops to complete your task

For example

MsgBox(0, "", CompareDate("2010:225:10:01", "2010:225:10:01"))
Func CompareDate($pFileTime1, $pFileTime2)
    $tc1 = StringSplit($pFileTime1, ":")
    $tc2 = StringSplit($pFileTime2, ":")
    For $i = 1 To $tc1[0]
        If Number($tc2[$i]) <> Number($tc1[$i]) Then
            If Number($tc2[$i]) > Number($tc1[$i]) Then
                Return -1
            Else
                Return 1
            EndIf
        EndIf
    Next
    Return 0
EndFunc

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Posted

I wrote that snip a long time ago, before I understood For...To...Next loops. Thanks John, that would make my personal UDF smoother, and allow for other Date formats to be checked as well with a few added parameters.

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry. 

Posted (edited)

A simpler Number()-less method?

Func CompareDate($pFileTime1, $pFileTime2)
    $tc1 = StringSplit($pFileTime1, ":")
    $tc2 = StringSplit($pFileTime2, ":")
    For $i = 1 To $tc1[0]
        $j = $tc1[$i] - $tc2[$i]
        If $j Then Return $j / Abs($j)
    Next
EndFunc
Edited by Spiff59

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
×
×
  • Create New...