' JUL2GRE$(0.0)  Convert Julian Date to Gregorian Format   04/05/1988-01/05/2010
' ------------------------------------------------------------------------------
' Copyright (C) 1988-2010 by Vladimir Veytsel                      www.davar.net

' Type -------------------------------------------------------------------------

'    Function

' Description ------------------------------------------------------------------

'    JUL2GRE$ function converts Julian date into Gregorian date format.

' Declaration ------------------------------------------------------------------

'    DECLARE FUNCTION JUL2GRE$(Jul_Date$)

' Parameter --------------------------------------------------------------------

'    Jul_Date$  - Julian date in the form of YY-DDD or CCYY-DDD,
'                 where DDD is a day number within a year.

' Value ------------------------------------------------------------------------

'    IF specified Julian date is valid, i.e.:
'       it is non-empty and
'       it has length either of 6 or 8 characters and
'       it is DIGITAL (with the exception of date field delimiter) and
'       day number lies within range of 1-365
'       (or 1-366, considering specified year leap characteristic),
'       THEN MM-DD-YY or MM-DD-CCYY (date in Gregorian format) is returned
'            to the point of function invocation,
'       ELSE "" (empty string) is returned to the point of invocation.

' Notes ------------------------------------------------------------------------

'  - Delimiter of parameter date fields is irrelevant and can be any symbol.
'    Date fields are extracted from fixed positions.

'  - EMPTY date is considered to be an INVALID one - Julian date is an internal
'    format that doesn't require a default.

' Examples ---------------------------------------------------------------------

'    JUL2GRE$(""        )=""
'    JUL2GRE$("87-31"   )=""
'    JUL2GRE$("1987-31" )=""
'    JUL2GRE$("19*7-031")=""
'    JUL2GRE$("1988-0#1")=""
'    JUL2GRE$("87-000"  )=""
'    JUL2GRE$("87-366"  )=""
'    JUL2GRE$("1988-367")=""

'    JUL2GRE$("87-031"  )="01-31-87"
'    JUL2GRE$("87-059"  )="02-28-87"
'    JUL2GRE$("87-090"  )="03-31-87"
'    JUL2GRE$("87-120"  )="04-30-87"
'    JUL2GRE$("87-151"  )="05-31-87"
'    JUL2GRE$("87-181"  )="06-30-87"
'    JUL2GRE$("87-212"  )="07-31-87"
'    JUL2GRE$("87-243"  )="08-31-87"
'    JUL2GRE$("87-273"  )="09-30-87"
'    JUL2GRE$("87-304"  )="10-31-87"
'    JUL2GRE$("87-334"  )="11-30-87"
'    JUL2GRE$("87-365"  )="12-31-87"

'    JUL2GRE$("1988-031")="01-31-1988"
'    JUL2GRE$("1988-060")="02-29-1988"
'    JUL2GRE$("1988-091")="03-31-1988"
'    JUL2GRE$("1988-121")="04-30-1988"
'    JUL2GRE$("1988-152")="05-31-1988"
'    JUL2GRE$("1988-182")="06-30-1988"
'    JUL2GRE$("1988-213")="07-31-1988"
'    JUL2GRE$("1988-244")="08-31-1988"
'    JUL2GRE$("1988-274")="09-30-1988"
'    JUL2GRE$("1988-305")="10-31-1988"
'    JUL2GRE$("1988-335")="11-30-1988"
'    JUL2GRE$("1988-366")="12-31-1988"

' External Functions ----------------------------------------------------------

     #INCLUDE ONCE "DIGITAL"
     #INCLUDE ONCE "LEAP"

' Start Function ---------------------------------------------------------------

     DEFINT A-Z  ' All defaulted variables are integer

     FUNCTION JUL2GRE$(Jul_Date$)

' Constant ---------------------------------------------------------------------

     Month_Days$="31,28,31,30,31,30,31,31,30,31,30,31"

' Check Specified Date for Valid Length (6 or 8 Characters) --------------------

     IF ((LEN(Jul_Date$)<>6)  AND _
         (LEN(Jul_Date$)<>8)) THEN
        JUL2GRE$=""
        EXIT FUNCTION
     END IF

' Check Specified Date for Digital Value ---------------------------------------

     IF (NOT(DIGITAL%(LEFT$(Jul_Date$,2-(LEN(Jul_Date$)=8))+ _
                     RIGHT$(Jul_Date$,3),""))) THEN
        JUL2GRE$=""
        EXIT FUNCTION
     END IF

' Parse Date to Be Converted ---------------------------------------------------

     Year$=     LEFT$(Jul_Date$,2-2*(LEN(Jul_Date$)=8))
     Day  =VAL(RIGHT$(Jul_Date$,3))

' Check Day for Lying within Proper Borders ------------------------------------

     IF (NOT((Day>0) AND _
             (Day<366-LEAP%(Year$)))) THEN
        JUL2GRE$=""
        EXIT FUNCTION
     END IF

' Adjust Number of February Days for the Leap Year -----------------------------

     IF (LEAP%(Year$)) THEN MID$(Month_Days$,5,1)="9"

' Compute Gregorian Month Number and Day ---------------------------------------
 
     DD=Day
     FOR MM=1 TO 12
         MM_Days=VAL(MID$(Month_Days$,3*MM-2,2))
         IF (DD<=MM_Days) THEN EXIT FOR
         DD=DD-MM_Days
     NEXT MM

' Form and Return Gregorian Date Value to the Point of Invocation --------------

     JUL2GRE$=RIGHT$("0"+LTRIM$(STR$(MM)),2)+"-"+ _
              RIGHT$("0"+LTRIM$(STR$(DD)),2)+"-"+Year$

' Finish Function --------------------------------------------------------------

     END FUNCTION