' VALDGRE%(0.0)  Check Date for Valid Gregorian Value      04/28/1997-02/02/2010
' ------------------------------------------------------------------------------
' Copyright (C) 1997-2010 by Vladimir Veytsel                      www.davar.net

' Type -------------------------------------------------------------------------

'    Function (predicate)

' Description ------------------------------------------------------------------

'    VALDGRE% function is a predicate that returns validity characteristic
'    of the specified date in Gregorian calendar system.

' Parameter --------------------------------------------------------------------

'    Spec_Date$  - Specified date in the form of MM-DD-YY or MM-DD-CCYY

' Value ------------------------------------------------------------------------

'    IF specified date is either EMPTY or valid, i.e:
'       it has length either of 8 or 10 characters and
'       it is DIGITAL (with the exception of date field delimiters) and
'       month value lies within 1-12 and
'       day number lies within range valid for the specified month
'       (considering specified year leap characteristic when
'       evaluating day range for February),
'       THEN -1 (true)  is returned to the point of function invocation,
'       ELSE  0 (false) is returned to the point of function invocation.

' Notes ------------------------------------------------------------------------

'  - Delimiters of parameter date fields are irrelevant and can be any
'    symbols.  Date fields are extracted from FIXED positions.

'  - EMPTY date is considered to be a valid date, since in date programs
'    this is an indication to use CURRENT date (default).

' Examples ---------------------------------------------------------------------

'    VALDGRE%(""           )=-1
'    VALDGRE%("11-11-1"    )= 0
'    VALDGRE%("11-11-111"  )= 0
'    VALDGRE%("11-11-11111")= 0
'    VALDGRE%("*1-11-11"   )= 0
'    VALDGRE%("11-*1-11"   )= 0
'    VALDGRE%("11-11-*1"   )= 0
'    VALDGRE%("00-01-00"   )= 0
'    VALDGRE%("13-01-00"   )= 0
'    VALDGRE%("01-00-00"   )= 0
'    VALDGRE%("01-32-00"   )= 0
'    VALDGRE%("02-29-1900" )= 0
'    VALDGRE%("02-30-2000" )= 0
'    VALDGRE%("02-27-1900" )=-1
'    VALDGRE%("02-28-2000" )=-1
'    VALDGRE%("01-01-2001" )=-1
'    VALDGRE%("06-15-2002" )=-1
'    VALDGRE%("12-15-9999" )=-1

' External Functions -----------------------------------------------------------

     #INCLUDE ONCE "DIGITAL"
     #INCLUDE ONCE "LEAP"

' Start Function ---------------------------------------------------------------

     DEFINT A-Z  ' All defaulted variables are integer

     FUNCTION VALDGRE%(Spec_Date$)

' Check Specified Date for Empty Value -----------------------------------------

     IF (LEN(Spec_Date$)=0) THEN
        VALDGRE%=-1
        EXIT FUNCTION
     END IF

' Check Specified Date for Valid Length (8 or 10 Characters) -------------------

     IF ((LEN(Spec_Date$)<> 8)  AND  _
         (LEN(Spec_Date$)<>10)) THEN
        VALDGRE%=0
        EXIT FUNCTION
     END IF

' Check Specified Date for Digital Value ---------------------------------------

     IF (DIGITAL%(LEFT$(Spec_Date$,2)    + _
                   MID$(Spec_Date$,4,2)  + _
                   MID$(Spec_Date$,7),"")) THEN
        Month=VAL(LEFT$(Spec_Date$,2))
        Day  =VAL( MID$(Spec_Date$,4,2))
        Year$=     MID$(Spec_Date$,7)
     ELSE
        VALDGRE%=0
        EXIT FUNCTION
     END IF

' Check Month and Day for Lying within Proper Borders --------------------------

     IF ((Month>0)   AND _
         (Month<13)) THEN
        VALDGRE%=((Day>0)AND _
                  (Day<(VAL(MID$("322932313231323231323132",Month*2-1,2))+ _
                        (Month=2)*LEAP%(Year$))))
     ELSE
        VALDGRE%=0
     END IF

' Finish.Function --------------------------------------------------------------

     END FUNCTION