' DAY2GRE$(0.0)  Convert Day Number to Gregorian Date  02/01/1989-10/15/2004
' --------------------------------------------------------------------------
' Copyright (C) 1989-2004 by Vladimir Veytsel                  www.davar.net

' Type ---------------------------------------------------------------------

'    Function

' Description --------------------------------------------------------------

'    DAY2GRE$ function converts absolute day number into Gregorian date.

' Declaration --------------------------------------------------------------

'    DECLARE FUNCTION DAY2GRE$(Day.Numb&)

' 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 -------------------------------------------------------

     DECLARE FUNCTION JUL2GRE$(Jul.Date$)
     DECLARE FUNCTION LEAP%   (Year$)

' Start Function -----------------------------------------------------------

     DEFINT A-Z  ' All defaulted variables are integer

     FUNCTION DAY2GRE$(Day.Numb&) PUBLIC

' Check Day Number Validity ------------------------------------------------

     IF (Day.Numb&&lt;=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
