' GRE2DAY&(0.0)  Convert Gregorian Date to Day Number  02/01/1989-10/15/2004
' --------------------------------------------------------------------------
' Copyright (C) 1989-2004 by Vladimir Veytsel                  www.davar.net

' Type ---------------------------------------------------------------------

'    Function

' Description --------------------------------------------------------------

'    GRE2DAY& function converts Gregorian date into absolute day number.

' Declaration --------------------------------------------------------------

'    DECLARE FUNCTION GRE2DAY&(Greg.Date$)

' Parameter ----------------------------------------------------------------

'    Greg.Date$  - Gregorian date in the form of MM-DD-YY or MM-DD-CCYY

' Value --------------------------------------------------------------------

'    If specified Gregorian 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 absolute day number for specified Gregorian date is returned
'            to the point of function invocation
'       else 0 (zero) is returned to the point of 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 one, and absolute A.D. day
'    number is returned for the CURRENT date (default).

'  - Program doesn't take into account pre-Gregorian (Julian)calendar 
'    differences and date adjustment made on 09/14/1752 - when England 
'    and its colonies switched from Julian calendar to currently used 
'    Gregorian.  Since program is intended for the modern-time date 
'    calculations, those factors are insignificant.

' Examples -----------------------------------------------------------------

'    GRE2DAY&("*"         )= 0
'    GRE2DAY&(""          )= Absolute day number for the current date
'    GRE2DAY&("01-01-0001")= 1
'    GRE2DAY&("12-31-0001")= 365
'    GRE2DAY&("01-01-0002")= 366
'    GRE2DAY&("12-31-0002")= 730
'    GRE2DAY&("01-01-0003")= 731
'    GRE2DAY&("12-31-0003")= 1095
'    GRE2DAY&("01-01-0004")= 1096
'    GRE2DAY&("12-31-0004")= 1461
'    GRE2DAY&("01-01-0005")= 1462
'    GRE2DAY&("12-31-1986")= 725371
'    GRE2DAY&("01-01-1987")= 725372
'    GRE2DAY&("12-31-1987")= 725736
'    GRE2DAY&("01-01-1988")= 725737
'    GRE2DAY&("12-31-1899")= 693595
'    GRE2DAY&("01-01-1900")= 693596
'    GRE2DAY&("12-31-1999")= 730119
'    GRE2DAY&("01-01-00"  )= 730120

' External Function --------------------------------------------------------

     DECLARE FUNCTION GRE2JUL$(Greg.Date$)

' Start Function -----------------------------------------------------------

     DEFINT A-Z  ' All defaulted variables are integer

     FUNCTION GRE2DAY&(Greg.Date$) PUBLIC

' Convert Gregorian Date to Julian Format ----------------------------------
' (Checks date validity and handles default empty date - current)

     Jul.Date$=GRE2JUL$(Greg.Date$)

     IF (LEN(Jul.Date$)=0) THEN
        GRE2DAY&=0
        EXIT FUNCTION
     END IF

' Add Current Century to Short Date (YY-DDD) -------------------------------

     IF (LEN(Jul.Date$)=6) THEN
        Jul.Date$=MID$(DATE$,7,2)+Jul.Date$   
     END IF

' Parse Date to Be Converted -----------------------------------------------

     Day    =VAL(RIGHT$(Jul.Date$,3))
     Year   =VAL( LEFT$(Jul.Date$,4))-1  ' First day of A.D. is 01-01-0001
     Century=INT(Year/100)

' Compute and Return Absolute Day Number to the Point of Invocation --------

     GRE2DAY&=Year*365+Day+INT(Year/4)-Century+INT(Century/4)

' Finish.Function ----------------------------------------------------------

     END FUNCTION
