//-----------------------------------------------------------------------------
// 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	ecal_to_etime
//
// purpose	Convert calendar elements to an Earth Time value.
//
// arguments	1 (ecal_t *) calendar struct to be converted
//
// returns	(etime_t) resultant Earth Time value (beginning of day)
//-----------------------------------------------------------------------------
etime_t
ecal_to_etime (
    ecal_t *	arg_cal_p
    )
__PROTO_END__
{
    etime_t	this_time	;
    etime_t	add_time	;

    if ( ! arg_cal_p ) return ETIME_MIN;

    this_time = ecal_to_eday( arg_cal_p );
    if ( this_time < EDAY_ETIME_MIN ) return EDAY_ETIME_MIN;
    if ( this_time > EDAY_ETIME_MAX ) return EDAY_ETIME_MAX;
    this_time *= ETIME_C(86400000000);

    add_time = arg_cal_p->hour;
    add_time *= ETIME_C(3600000000);
    if ( this_time > ( ETIME_MAX - add_time ) ) return ETIME_MAX;
    if ( this_time < ( ETIME_MIN + add_time ) ) return ETIME_MIN;
    this_time = 0;
    this_time += add_time;

    add_time = arg_cal_p->min;
    add_time *= ETIME_C(60000000);
    if ( this_time > ( ETIME_MAX - add_time ) ) return ETIME_MAX;
    if ( this_time < ( ETIME_MIN + add_time ) ) return ETIME_MIN;
    this_time += add_time;

    add_time = arg_cal_p->sec;
    add_time *= ETIME_C(1000000);
    if ( this_time > ( ETIME_MAX - add_time ) ) return ETIME_MAX;
    if ( this_time < ( ETIME_MIN + add_time ) ) return ETIME_MIN;
    this_time += add_time;

    add_time = arg_cal_p->nsec;
    add_time /= 1000L;
    if ( this_time > ( ETIME_MAX - add_time ) ) return ETIME_MAX;
    if ( this_time < ( ETIME_MIN + add_time ) ) return ETIME_MIN;
    this_time += add_time;

    return this_time;
}

