' DAY2GRE$(0.0)  Convert Abs Day Number to Gregorian Date  02/01/1989-02/05/2010
' ------------------------------------------------------------------------------
' Copyright (C) 1989-2010 by Vladimir Veytsel                      www.davar.net

' Type -------------------------------------------------------------------------

'    Function

' Description ------------------------------------------------------------------

'    DAY2GRE$ function converts absolute day number into Gregorian date.

' Parameter --------------------------------------------------------------------

'    Day_Numb&  - Gregorian date in the form of MM-DD-YY or MM-DD-CCYY.

' Value ------------------------------------------------------------------------

'    IF specified absolute day number is greater than zero,
'       THEN corresponding 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.

' Note ---------------------------------------------------------------------

'  - 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 -----------------------------------------------------------------

'    DAY2GRE$(     0)=""
'    DAY2GRE$(     1)="01-01-0001"
'    DAY2GRE$(   365)="12-31-0001"
'    DAY2GRE$(   366)="01-01-0002"
'    DAY2GRE$(   730)="12-31-0002"
'    DAY2GRE$(   731)="01-01-0003"
'    DAY2GRE$(  1095)="12-31-0003"
'    DAY2GRE$(  1096)="01-01-0004"
'    DAY2GRE$(  1460)="12-30-0004"
'    DAY2GRE$(  1461)="12-31-0004"
'    DAY2GRE$(  1462)="01-01-0005"
'    DAY2GRE$(725371)="12-31-1986"
'    DAY2GRE$(725372)="01-01-1987"
'    DAY2GRE$(725736)="12-31-1987"
'    DAY2GRE$(725737)="01-01-1988"
'    DAY2GRE$(693595)="12-31-1899"
'    DAY2GRE$(693596)="01-01-1900"
'    DAY2GRE$(730119)="12-31-1999"
'    DAY2GRE$(730120)="01-01-2000"

' External Functions -------------------------------------------------------

     #INCLUDE ONCE "JUL2GRE"
     #INCLUDE ONCE "LEAP"

' Start Function -----------------------------------------------------------

     DEFINT A-Z  ' All defaulted variables are integer

     FUNCTION DAY2GRE$(Day_Numb&)

' Check Day Number Validity ------------------------------------------------

     IF (Day_Numb&<=0) THEN
        DAY2GRE$=""
        EXIT FUNCTION
     END IF

' Compute Components of Julian Date (Year and Day) -------------------------

     Year=1  ' First day of A.D. is 01-01-0001
     
     WHILE (Day_Numb&>365)
           Day_Numb&=Day_Numb&-365+LEAP%(RIGHT$("000"+LTRIM$(STR$(Year)),4))
           Year=Year+1
     WEND

' Adjust Date for 12/31 of Leap Year ---------------------------------------
     
     IF (Day_Numb&=0)AND _
        (LEAP%(RIGHT$("000"+LTRIM$(STR$(Year-1)),4))) THEN
        Day_Numb&=366
        Year=Year-1
     END IF     

' Compose, Convert, and Return Gregorian Date to the Point of Invocation ---

     DAY2GRE$=JUL2GRE$(RIGHT$("000"+LTRIM$(STR$(Year     )),4)+"-"+ _
                       RIGHT$("00" +LTRIM$(STR$(Day_Numb&)),3))

' Finish.Function ----------------------------------------------------------

     END FUNCTION