/**********************************************************************
 
	Copyright (C) 2003 Hirohisa MORI <joshua@nichibun.ac.jp>
 
	This program is free software; you can redistribute it 
	and/or modify it under the terms of the GLOBALBASE 
	Library General Public License (G-LGPL) as published by 

	http://www.globalbase.org/
 
	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.

**********************************************************************/



#include	"memory_debug.h"
#include	"gbgraph.h"
#include	"utils.h"

int
w3c_dtf_to_gb_time(GB_TIME * t,char * str)
{
char * s;
int ret;
char * ptr;
int cnt;
int d0,d1,d2;
int fig;
int fg;
	if ( str == 0 || str[0] == 0 )
		return -1;
	ret = -1;
	s = copy_str(str);
	cnt = 0;
	fig = 0;
	for ( ptr = s ; *ptr && *ptr == ' ' || *ptr == '\t' ; ptr ++ );
	if ( *ptr == '-' )
		ptr ++;
	for ( ; *ptr ; ptr ++ ) {
		if ( *ptr == '-' ) {
			*ptr = ' ';
			cnt ++;
		}
		if ( '0' <= *ptr && *ptr <= '9' )
			fig = 1;
	}
	if ( fig == 0 ) {
		ret = -1;
		goto end;
	}
	switch ( cnt ) {
	case 0:
		sscanf(s,"%i",&d0);
		t->year = d0;
		t->month = -1;
		t->date = -1;
		break;
	case 1:
		sscanf(s,"%i %i",&d0,&d1);
		t->year = d0;
		t->month = d1;
		t->date = -1;
		break;
	default:
		sscanf(s,"%i %i %i",&d0,&d1,&d2);
		t->year = d0;
		t->month = d1;
		t->date = d2;
		break;
	}
	t->hour = -1;
	t->min = -1;
	t->sec = -1;
	t->day = -1;
	ret = 0;
end:
	d_f_ree(s);
	return ret;
}
