Jump to content

Recommended Posts

Posted

Yes, I did. By the way, I am just thinking all those StringIsInt() seem overkill when a regular expression could be used e.g. [d-/.]

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

Don't worry, I will look at this someday.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

Completed.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • Moderators
Posted

jaberwacky,

 

  On 1/12/2014 at 2:01 PM, jaberwacky said:

I need to get myself into a regexp mindset.

Do not get too fixated on them. As GEOSoft told me when I started learning about SREs, one of the major things to learn about RegExes is when NOT to use them. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted

Sometimes it's just efficient to use String() functions instead of banging your head against a brick wall to create an "all purpose" regular expression.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted (edited)

I set up a comprehensive test of _DateIsValid and I see some weird results.  Maybe I am using the hh:mm:ss part wrong?  I ran it on both 3.3.10.2 and 3.3.11.2 with the same results.

I'm a numbnard sometimes.

#include <Date.au3>

_test_DateIsValid() ; without [ 00:00[:00]] -- does work

_test_DateIsValid("[ 00:00[:00]]") ; with [ 00:00[:00]] -- doesn't work

Func _test_DateIsValid(Const $hhmmss = '')  
  Local $is_valid = ''
 
  Local $days_in_month = ''
 
  Local $year, $month, $day = ''
 
  For $y = 1000 To 1001 ; usually to 2999 but 1001 for quick testing
    $year = StringFormat("%04d", $y)
    
    For $m = 1 To 12      
      $month = StringFormat("%02d", $m)
    
      $days_in_month = _DaysInMonth($year)[$m]
      
      For $d = 1 To $days_in_month    
        $day = StringFormat("%02d", $d)
      
        $is_valid = _DateIsValid($year & '/' & $month & '/' & $day & $hhmmss)
       
        ConsoleWrite(($is_valid ? '>' : '!') & " Date: " & $year & '/' & $month & '/' & $day & $hhmmss & @CRLF)
      Next
    Next
  Next
EndFunc
Edited by jaberwacky
Posted (edited)

Ok, finally figured out the problem in my last post.   ::heh::

And so I found out that Number(...) is not needed.

_test_DateIsValid()

Func _test_DateIsValid()  
  Local $is_valid, $days_in_month = ''
  Local $year, $month, $day, $date = ''
 
  For $y = 1000 To 2999
    $year = StringFormat("%04d", $y)
    
    For $m = 1 To 12      
      $month = StringFormat("%02d", $m)
    
      $days_in_month = _DaysInMonth($year)[$m]
      
      For $d = 1 To $days_in_month    
        $day = StringFormat("%02d", $d)
        
        $date = $year & '/' & $month & '/' & $day & " 00:00:00"
      
        $is_valid = _DateIsValid($date)
       
        ConsoleWrite(($is_valid ? '>' : '!') & " Date: " & $date & @CRLF)
      Next
    Next
  Next
EndFunc

Func _DateIsValid($sDate)  
  If StringRegExp($sDate, "(\d{4}/\d{2}/\d{2}\s+\d{2}:\d{2}(:\d{2}){0,1}|\d{4}/\d{2}/\d{2})") Then
    Return SetError(1)
  EndIf
 
  Local $asDatePart[4]
 
  Local $asTimePart[4]
 
  _DateTimeSplit($sDate, $asDatePart, $asTimePart)
 
  ; check if all contain valid values
  If $asDatePart[1] < 1000 Or $asDatePart[1] > 2999 Then Return SetError(2)
  If $asDatePart[2] < 1 Or $asDatePart[2] > 12 Then Return SetError(3)
  Local Const $iNumDays = _DaysInMonth($asDatePart[1])[$asDatePart[2]]
  If $asDatePart[3] < 1 Or $asDatePart[3] > $iNumDays Then Return SetError(4)
 
  ; check Time portion
  If $asTimePart[0] < 1 Then Return 1 ; No time specified so date must be correct
  If $asTimePart[0] < 2 Then Return 0 ; need at least HH:MM when something is specified
  If $asTimePart[0] = 2 Then $asTimePart[3] = "00" ; init SS when only HH:MM is specified
 
  If $asTimePart[1] < 0 Or $asTimePart[1] > 23 Then Return SetError(5)
  If $asTimePart[2] < 0 Or $asTimePart[2] > 59 Then Return SetError(6)
  If $asTimePart[3] < 0 Or $asTimePart[3] > 59 Then Return SetError(7)
 
  ; we got here so date/time must be good
  Return 1
EndFunc

(My regexp doesn't work though.)

Edited by jaberwacky
Posted (edited)

Is this a submission? As it's not adhering to the UDF standards we expect. For example SetError() without the return parameter is incorrect.

A couple of regular expressions I was thinking of for _DateIsValid()

$sDate = '2014/01/14'
$fIsDate = StringRegExp($sDate, '[^\d.\-/:]') = 0
ConsoleWrite($fIsDate & @CRLF)

$sDate = '2014/01/14T12:51:00'
$fIsDate = StringRegExp($sDate, '(?x)^\d{4}(?:[.\-/]\d{2}){2}   (?:     (?:T|\h)\d{2}:\d{2}     (?::\d{2})?     )?$') = 1
ConsoleWrite($fIsDate & @CRLF)
Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted (edited)
  On 1/14/2014 at 11:08 AM, guinness said:

Is this a submission?

Nope.

Gosh.  SetError:

"[optional] The value to be returned by the function - if no parameter used the return value is undefined."

That's what I get for assuming the function would return 0

Edited by jaberwacky
Posted

So you get my point now?

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted (edited)

Oh, OK.

I won't be paying close attention to this thread much anymore, so if you have a submission post to Trac. Thanks.

Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted (edited)

I know why trancexx chose 2006 and 2007, though she could have chosen 1995 and 1996 as well.

Edit: Just managed to get around to properly looking at that function.

Edit 2: I have left it undocumented for now, just in case people want to play around with it.

Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

That's fun!

; #FUNCTION# ====================================================================================================================
; Author ........: Jason Brand <exodius at gmail dot com>
; Modified.......: guinness
; ===============================================================================================================================
Func _DateToMonth($iMonNum, $iFormat = Default)
    If $iFormat = Default Then $iFormat = 0
    $iMonNum = Int($iMonNum)
    If Not __DateIsMonth($iMonNum) Then Return SetError(1, 0, "")
    Local $tSYSTEMTIME = DllStructCreate($tagSYSTEMTIME)
    DllStructSetData($tSYSTEMTIME, "Year", @YEAR)
    DllStructSetData($tSYSTEMTIME, "Month", $iMonNum)
    DllStructSetData($tSYSTEMTIME, "Day", 1)
    Return _WinAPI_GetDateFormat(BitAND($iFormat, $DOW_LOCALE_LONGNAME) ? $LOCALE_USER_DEFAULT : $LOCALE_INVARIANT, $tSYSTEMTIME, 0, BitAND($iFormat, $DOW_SHORTNAME) ? "MMM" : "MMMM")
EndFunc   ;==>_DateToMonth

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...