//-----------------------------------------------------------------------------
// 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.
//-----------------------------------------------------------------------------

#include "time_lib.h"

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	str_cal_to_eday
//
// purpose	Convert date in a string to type eday for either Gregorian or
//		Julian calendar.
//
// arguments	1 (const char *) date and time string
//		2 (eday_t (*)(long,int,int)) function to do conversion
//
// returns	(eday_t) == EDAY_ERROR: error
//		(eday_t) != EDAY_ERROR: date converted
//
// format	yyyy/mm/dd[_hh:mm:ss.nnnnnnnnnnnnnnnnnn]
//
// note		Any non-numeric character may be used as separators, but
//		such use should be limited to reasonable punctuation only
//		for future compatibility.
//
// note		Time parts may be specified but will be ignored.
//-----------------------------------------------------------------------------
eday_t
str_cal_to_eday (
    const char *	arg_str
    ,
    eday_t ( *		arg_fun		)( long, int, int )
    )
__PROTO_END__
{
    long	year	;
    int		month	;
    int		mday	;
    int		neg	;
    int		ch	;

    if ( ! arg_str ) return EDAY_ERROR;

    //--------------
    // Convert year.
    //--------------
    neg = 0;
    if ( ( ch = * arg_str ) == '-' ) {
	neg = 1;
	++ arg_str;
    }
    else if ( ( ch = * arg_str ) == '+' ) {
	++ arg_str;
    }
    for ( year = 0; ( ch = * arg_str ) >= '0' && ch <= '9'; ++ arg_str ) {
	year *= 10;
	year += ch - '0';
    }
    if ( ( ( ch == 'a' || ch == 'A' ) && ( arg_str[1] == 'd' || arg_str[1] == 'D' ) ) ||
	 ( ( ch == 'c' || ch == 'C' ) && ( arg_str[1] == 'e' || arg_str[1] == 'E' ) ) ) {
	arg_str += 2;
    }
    else if ( ( ch == 'b' || ch == 'B' ) && ( arg_str[1] == 'c' || arg_str[1] == 'C' ) ) {
	year = 1 - year;
	arg_str += 2;
	if ( ( ch = * arg_str ) == 'e' || ch == 'E' ) ++ arg_str;
    }
    else if ( neg ) {
	year = 0 - year;
    }
    if ( * arg_str ) ++ arg_str;

    //---------------
    // Convert month.
    //---------------
    for ( month = 0; ( ch = * arg_str ) >= '0' && ch <= '9'; ++ arg_str ) {
	month *= 10;
	month += ch - '0';
    }
    if ( * arg_str ) ++ arg_str;

    //----------------------
    // Convert day of month.
    //----------------------
    for ( mday = 0; ( ch = * arg_str ) >= '0' && ch <= '9'; ++ arg_str ) {
	mday *= 10;
	mday += ch - '0';
    }
    if ( * arg_str ) ++ arg_str;

    //-----------------------------------
    // Convert to eday format and return.
    //-----------------------------------
    return (* arg_fun)( year, month, mday );
}

