//-----------------------------------------------------------------------------
// 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_julian
//
// purpose	Convert an Earth Day value to Julian calendar elements.
//
// arguments	1 (eday_t) Earth Day value
//		2 (ecal_t *) where to store calendar elements.
//
// returns	(void)
//-----------------------------------------------------------------------------
void
eday_to_ecal_julian (
    eday_t	arg_eday
    ,
    ecal_t *	arg_caltime
    )
__PROTO_END__
{
    long	day		;
    long	quadyear	;
    long	year		;


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

    //------------------------------------------------------------------
    // Calculate the quad-year number.
    //------------------------------------------------------------------
    // Negative values for the day number usually (always in Standard C
    // 1999) do not have consistent quotient periods into the negative
    // range.  This is compensated for by detecting this case with the
    // modulus result being negative and subtracting one more from the
    // quotient.
    //------------------------------------------------------------------
    quadyear = quo_ni( day, 1461 );

    //------------------------------------------------------------------
    // Normalize the day number to within one quad-year.
    //------------------------------------------------------------------
    // This is easy because a quad-year (4 years) is always exactly
    // 1461 days in the Julian calendar.  A negative quad-year number
    // will move the negative day number up to a positive value.
    //------------------------------------------------------------------
    day -= quadyear * 1461;

    //------------------------------------------------------------------
    // Calculate year within the quad-year.
    //------------------------------------------------------------------
    // Since the first year in a quad-year is leap, shift the range of
    // days down by one to 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 special handle the first day to ensure the quotient is zero
    // since some division may result in "truncation down" to -1.
    // Only day == 0 can encounter this rare problem.
    //------------------------------------------------------------------
    -- day;
#if defined(__STDC_VERSION__) && __STDC_VERSION__ == 199901L
    year =                   day / 365;
#else
    year = ( day < 0 ) ? 0 : day / 365;
#endif

    //------------------------------------------------------------------
    // Normalize the day number to within one year.
    //------------------------------------------------------------------
    // Since the first year in a quad-year is a leap year, an extra day
    // must be subtracted to account for the extra leap year.  Since
    // one is already subtracted above, just add one back if this is the
    // first year (none preceeding).
    //------------------------------------------------------------------
    day -= year * 365;
    if ( year == 0 ) ++ day;

    //------------------------------------------------------------------
    // Reconstruct the actual Julian calendar elements.
    //------------------------------------------------------------------
    arg_caltime->year	= year + quadyear * 4;
    arg_caltime->isleap	= year == 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 Julian calendar.
    //-----------------------------------------------------
    arg_caltime->type	= ECAL_TYPE_JULIAN;

    //---------------------------------------------------------------
    // 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;
}

