' GRE2JUL$(0.0)  Convert Gregorian Date to Julian Format   04/05/1988-02/04/2010
' ------------------------------------------------------------------------------
' Copyright (C) 1988-2010 by Vladimir Veytsel                      www.davar.net

' Type -------------------------------------------------------------------------

'    Function

' Description ------------------------------------------------------------------

'    GRE2JUL$ function converts Gregorian date into Julian date format.

' Declaration ------------------------------------------------------------------

'    DECLARE FUNCTION GRE2JUL$(Greg_Date$)

' Parameter --------------------------------------------------------------------

'    Greg_Date$  - Gregorian date in the form of MM-DD-YY or MM-DD-CCYY

' Value ------------------------------------------------------------------------

'    IU 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 YY-DDD or CCYY-DDD (date in Julian format) is returned 
'            to the point of function invocation
'            (where DDD is a day number within a year),
'       ELSE "" (empty string) 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 date in Julian
'    format CCYY-DDD is returned for the CURRENT date (default).

' Examples ---------------------------------------------------------------------

'    GRE2JUL$(""          )=Current date in Julian format
'    GRE2JUL$("02-29-1987")=""
'    GRE2JUL$("01-01-87"  )="87-001"
'    GRE2JUL$("01-31-87"  )="87-031"
'    GRE2JUL$("02-28-87"  )="87-059"
'    GRE2JUL$("03-31-87"  )="87-090"
'    GRE2JUL$("04-30-87"  )="87-120"
'    GRE2JUL$("05-31-87"  )="87-151"
'    GRE2JUL$("06-30-87"  )="87-181"
'    GRE2JUL$("07-31-87"  )="87-212"
'    GRE2JUL$("08-31-87"  )="87-243"
'    GRE2JUL$("09-30-87"  )="87-273"
'    GRE2JUL$("10-31-87"  )="87-304"
'    GRE2JUL$("11-30-87"  )="87-334"
'    GRE2JUL$("12-31-87"  )="87-365"

'    GRE2JUL$("01-01-1988")="1988-001"
'    GRE2JUL$("01-31-1988")="1988-031"
'    GRE2JUL$("02-29-1988")="1988-060"
'    GRE2JUL$("03-31-1988")="1988-091"
'    GRE2JUL$("04-30-1988")="1988-121"
'    GRE2JUL$("05-31-1988")="1988-152"
'    GRE2JUL$("06-30-1988")="1988-182"
'    GRE2JUL$("07-31-1988")="1988-213"
'    GRE2JUL$("08-31-1988")="1988-244"
'    GRE2JUL$("09-30-1988")="1988-274"
'    GRE2JUL$("10-31-1988")="1988-305"
'    GRE2JUL$("11-30-1988")="1988-335"
'    GRE2JUL$("12-31-1988")="1988-366"

' External Functions -----------------------------------------------------------

     #INCLUDE ONCE "LEAP"
     #INCLUDE ONCE "VALDGRE"

' Start Function ---------------------------------------------------------------

     DEFINT A-Z  ' All defaulted variables are integer

     FUNCTION GRE2JUL$(Greg_Date$)

' Check Date Validity and Handle Default Empty Date (Current) ------------------

     IF (LEN(Greg_Date$)=0) THEN
        DT$=DATE$
     ELSEIF (VALDGRE%(Greg_Date$)) THEN
        DT$=Greg_Date$
     ELSE
        GRE2JUL$=""
        EXIT FUNCTION
     END IF

' Parse Date to Be Converted ---------------------------------------------------

     Month=VAL(LEFT$(DT$,2))
     Day  =VAL( MID$(DT$,4,2))
     Year$=     MID$(DT$,7)

' Compute Julian Day Number ----------------------------------------------------

     DDD=Day+ _
         VAL(MID$("000,031,059,090,120,151,181,212,243,273,304,334",4*Month-3,3))+ _
         (Month>2)*(LEAP%(Year$))

' Form and Return Julian Date Value to the Point of Invocation -----------------

     GRE2JUL$=Year$+"-"+RIGHT$("00"+LTRIM$(STR$(DDD)),3)

' Finish Function --------------------------------------------------------------

     END FUNCTION