Jump to content

_DateIsValid() is returning false positives


Recommended Posts

Please forgive if there is already a topic regarding this issue, however I could not find one.

It seems that the _DateIsValid function can return 1(Valid) even when the date is not valid. I am using the latest releases of Autoit and SciTE

Example of 3 non-valid dates:
 

#include <Date.au3>
Local $checkDate, $aDateExamples[3]
$aDateExamples[0] = "rrrr/rr/rr 05:23:09"
$aDateExamples[1] = "2007/12/12 rr:rrfrr"
$aDateExamples[2] = "2007/12/08 rrfrrfrr"

For $i = 0 To UBound($aDateExamples) - 1
    $checkDate = _DateIsValid($aDateExamples[$i])
    If $checkDate = 0 Then
        $checkDate = "Not Valid"
    Else
        $checkDate = "Valid"
    EndIf
    ConsoleWrite($aDateExamples[$i] & " returns: " & $checkDate & @CRLF)
Next

 

I am sure I could check the strings first but I do believe that the _Dateisvalid function does that already.

Any help would be greatly appreciated!:)

Link to comment
Share on other sites

  • Developers

This is indeed a flaw in the current UDF as the _DateTimeSplit() udf assumes this date has the "yyyy/mm/dd hh:mm" format and for the hh=rr and mm=rrfrr, the INT() is taken which is 0, so it "thinks" the time is 00:00.

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

  • Developers

...I am actually pretty sure somebody could come up with a regex that does this test, which weren't available at the time I wrote these under my alias JdeB. :) 

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

I'm sure there is a better way to do this but this is my work around for this situationo:)

#include <Date.au3>
Local $aDate, $aTime, $verifyNumbersOnly, $checkDate, $aDateExamples[3]
$aDateExamples[0] = "2007/12/12 05:22:09"
$aDateExamples[1] = "2007/12/12 rr:rrfrr"
$aDateExamples[2] = "2007/12/08 rrfrrfrr"

For $i = 0 To UBound($aDateExamples) - 1
    ; Remove "/" and ":" and " " from the string
    $verifyNumbersOnly = StringRegExpReplace($aDateExamples[$i], "[/: ]", "")
    $checkDate = "Not Checked because it's not Int!"
    If StringIsInt($verifyNumbersOnly) Then ;Only check the date if it's doesn't contain letters
        $checkDate = _DateIsValid($aDateExamples[$i])
        If $checkDate = 0 Then
            $checkDate = "Not Valid"
        Else
            $checkDate = "Valid"
        EndIf
    EndIf
    ConsoleWrite($aDateExamples[$i] & " returns: " & $checkDate & @CRLF)
Next

 

Link to comment
Share on other sites

Try this:

#include <Date.au3>

Local $aDate
Local $aDateExamples = ["2007/12/12 05:22:09", "2007/12/12 rr:rrfrr", "2007/12/12 rrfrrfrr", "2007/12/12 35:22:09"]
For $i = 0 To UBound($aDateExamples) - 1
    Local $checkDate = "Valid"
    If Not (StringRegExp($aDateExamples[$i], "^\d\d\d\d[/-]\d\d[/-]\d\d \d\d:\d\d:\d\d$") And _DateIsValid($aDateExamples[$i])) Then
        $checkDate = "Not " & $checkDate
    EndIf
    ConsoleWrite($aDateExamples[$i] & " returns: " & $checkDate & @CRLF)
Next

 

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Or like you did it 

just for a note, note that the dateisvalid   works even without expressing the time units after the space in the date but i'm guessing the digits in the date are mandatory for the function to return true

#include <Date.au3>
Local $aDate, $aTime, $verifyNumbersOnly, $checkDate, $aDateExamples[3]
$aDateExamples[0] = "2007/12/12 05:22:09"
$aDateExamples[1] = "2007/12/12 rr:rrfrr"
$aDateExamples[2] = "2007/12/08 rrfrrfrr"

For $i = 0 To UBound($aDateExamples) - 1
    If Not StringRegExp(StringRegExpReplace($aDateExamples[$i], "[/: ]", ""), "\D") And _DateIsValid($aDateExamples[$i]) Then
        $checkDate = "Valid"
    Else
        $checkDate = "Not Valid"
    EndIf
    ConsoleWrite($aDateExamples[$i] & " returns: " & $checkDate & @CRLF)
Next

 

Edited by Deye
Link to comment
Share on other sites

55 minutes ago, Deye said:

Or like you did it 

just for a note, note that the dateisvalid   works even without expressing the time units after the space in the date but i'm guessing the digits in the date are mandatory for the function to return true

#include <Date.au3>
Local $aDate, $aTime, $verifyNumbersOnly, $checkDate, $aDateExamples[3]
$aDateExamples[0] = "2007/12/12 05:22:09"
$aDateExamples[1] = "2007/12/12 rr:rrfrr"
$aDateExamples[2] = "2007/12/08 rrfrrfrr"

For $i = 0 To UBound($aDateExamples) - 1
    If Not StringRegExp(StringRegExpReplace($aDateExamples[$i], "[/: ]", ""), "\D") And _DateIsValid($aDateExamples[$i]) Then
        $checkDate = "Valid"
    Else
        $checkDate = "Not Valid"
    EndIf
    ConsoleWrite($aDateExamples[$i] & " returns: " & $checkDate & @CRLF)
Next

 

Yes they were mandatory for the function to return true 

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...