//-----------------------------------------------------------------------------
// Copyright © 2004 - Philip Howard - All rights reserved
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//-----------------------------------------------------------------------------
// package	libh/time
// homepage	http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author	Philip Howard
// email	libh at ipal dot org
// homepage	http://phil.ipal.org/
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 120 columns wide.
//-----------------------------------------------------------------------------
// reference	Explanatory Supplement to the Astronomical Almanac,
//		P. Kenneth Seidelmann, editor  [ISBN 0-935702-68-7]
//		http://shop.bn.com/bookSearch/isbnInquiry.asp?isbn=0935702687
//-----------------------------------------------------------------------------

#include <ctype.h>

#include "time_lib.h"


__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	eday_to_ecal_gregorian
// alias	eday_to_ecal
//
// purpose	Convert an Earth Day value to Gregorian calendar elements.
//
// arguments	1 (eday_t) Earth Day value
//		2 (ecal_t *) where to store calendar elements.
//
// returns	(void)
//-----------------------------------------------------------------------------
#define eday_to_ecal eday_to_ecal_gregorian
void
eday_to_ecal_gregorian (
    eday_t	arg_eday
    ,
    ecal_t *	arg_caltime
    )
__PROTO_END__
{
    long	day		;
    long	quadcent	;
    long	cent		;
    long	quadyear	;
    long	year		;


    //------------------------------------------------------------------
    // Adjust epoch from Earth Day to Gregorian era at year 0.
    //------------------------------------------------------------------
    day = arg_eday - EDAY_1_JAN_0;

    //------------------------------------------------------------------
    // Calculate the quad century number.
    //------------------------------------------------------------------
    // Dates and times require a consistent periodic modulus even when
    // the dividend is negative.  Standard C 1999 defines the / and %
    // operators to use "division by truncation toward zero" which is
    // not what produces a consistent periodic modulus.  Instead, the
    // quo_ni() and mod_ni() functions are used to get the proper results.
    //------------------------------------------------------------------
    quadcent = quo_ni( day, 146097 );

    //------------------------------------------------------------------
    // Normalize the day number to within one quad-century.
    //------------------------------------------------------------------
    // This is easy because a quad-century (400 years) is always
    // exactly 146097 days in the Gregorian calendar.  A negative
    // quad-century number will move the negative day number up to
    // a positive value.
    //------------------------------------------------------------------
    day -= quadcent * 146097;

    //------------------------------------------------------------------
    // Calculate the century within the quad-century.
    //------------------------------------------------------------------
    // The first century has an extra leap year so it is longer by one
    // day.  Shift the range of days down by this one so the centuries
    // line up with where the division gives different quotients.
    //------------------------------------------------------------------
    -- day;
    cent = ( day < 0 ) ? 0 : day / 36524;

    //------------------------------------------------------------------
    // Normalize the day number to within one century.
    //------------------------------------------------------------------
    // Since the first century has an extra leap year, if this century
    // is after the first one, an extra day must be subtracted to
    // account for the extra leap year.  Since one is already
    // subtracted, just add one back if this is the first century
    // (none preceeding).
    //------------------------------------------------------------------
    day -= cent * 36524;
    day += ( cent == 0 );

    //------------------------------------------------------------------
    // Calculate quad-year within the century.
    //------------------------------------------------------------------
    // Since the first quad-year in centuries other than the first
    // century has no leap year, if this is that first century, then
    // shift the range of days up by one so the quad-years line up
    // with where the division gives different quotients.
    //------------------------------------------------------------------
    quadyear = ( day + ( cent > 0 ) ) / 1461;

    //------------------------------------------------------------------
    // Normalize the day number to within one quad-year.
    //------------------------------------------------------------------
    // Since the first quad-year in centuries other than the first
    // lacks a leap year where otherwise there is one, in the case of
    // the first quad-year being a preceeding one in a century other
    // than the first, add back the day that was overly subtracted.
    //------------------------------------------------------------------
    day -= quadyear * 1461;
    day += ( quadyear > 0 && cent > 0 );

    //------------------------------------------------------------------
    // Calculate year within the quad-year.
    //------------------------------------------------------------------
    // Since the first year in a quad-year is leap, but only when the
    // quad-year is other than first or when the century is the first,
    // shift the range of days down by one so the years line up with
    // where the division gives different quotients.
    //------------------------------------------------------------------
    // Standard C 1999 guarantees division is "truncation to zero"
    // which means -1/365 = 0.  If not Standard C 1999, then we have
    // to handle the first day to ensure the quotient is zero since
    // some division may result in "truncation down" to -1.  Only when
    // day == 0 can this rare problem happen and only when division
    // is not conforming to Standard C 1999.
    //------------------------------------------------------------------
#if defined(__STDC_VERSION__) && __STDC_VERSION__ == 199901L
    year =                    ( day - ( quadyear > 0 || cent == 0 ) ) / 365;
#else
    year = ( day == 0 ) ? 0 : ( day - ( quadyear > 0 || cent == 0 ) ) / 365;
#endif

    //------------------------------------------------------------------
    // Normalize the day number to within one year.
    //------------------------------------------------------------------
    // Since the first year in a quad-year is leap, but only when the
    // quad-year is other than first or when the century is the first,
    // if the first year preceeds this year, and this is a quad-year
    // other than the first or is the first century, an extra day is
    // subtracted to account for the extra day in the leap year.
    //------------------------------------------------------------------
    day -= year * 365;
    if ( year > 0 && ( quadyear > 0 || cent == 0 ) ) -- day;

    //------------------------------------------------------------------
    // Reconstruct the actual Gregorian calendar elements.
    //------------------------------------------------------------------
    arg_caltime->year	= year + ( ( quadcent * 4 + cent ) * 25 + quadyear ) * 4;
    arg_caltime->isleap	= year == 0 && ( quadyear > 0 || cent == 0 );
    arg_caltime->month	= doy_to_mon( day, arg_caltime->isleap );
    arg_caltime->mday	= doy_to_dom( day, arg_caltime->isleap );
    arg_caltime->yday	= day;

    //------------------------------------------------------------------
    // Calculate the day of week.
    //------------------------------------------------------------------
    // The eday epoch is 1 day offset from giving the day of week
    // directly so it is necessary to add 1 before calculating the
    // modulus.  Because this would overflow on EDAY_MAX, subtract 6
    // instead for values over 7.  Then an additional 1 is added to
    // make the day of week be in the range of 1..7.
    //------------------------------------------------------------------
    arg_caltime->wday = mod_ni( arg_eday + ( arg_eday > 7 ? -6 : 1 ), 7 ) + 1;

    //--------------------------------------------------------------
    // The week number is 0 to 53.  The first full week is number 1.
    //--------------------------------------------------------------
    arg_caltime->week	= ( day + 8 - arg_caltime->wday ) / 7;

    //--------------------------------------------------------
    // Make it known this is a date in the Gregorian calendar.
    //--------------------------------------------------------
    arg_caltime->type	= ECAL_TYPE_GREGORIAN;

    //-------------------------------------------------------------
    // Set time elements to zero to represent the start of the day.
    //-------------------------------------------------------------
    arg_caltime->hour	= 0;
    arg_caltime->min	= 0;
    arg_caltime->sec	= 0;
    arg_caltime->nsec	= 0;
    return;
}

