fix up the iso8601 string format date utilities

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@3548 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Linas Vepstas 2001-01-29 07:11:23 +00:00
parent 5186dc79ba
commit 86c18268f4
2 changed files with 58 additions and 9 deletions

View File

@ -330,9 +330,10 @@ char dateSeparator ()
/* hack alert -- this routine returns incorrect values for
* dates before 1970 */
Timespec
gnc_iso8601_to_timespec(const char *str)
static Timespec
gnc_iso8601_to_timespec(const char *str, int do_localtime)
{
char buf[4];
Timespec ts;
struct tm stm;
long int nsec =0;
@ -355,16 +356,49 @@ gnc_iso8601_to_timespec(const char *str)
stm.tm_sec = atoi (str);
/* the decimal point, optionally present ... */
/* hack alert -- we should count number of decimal places, */
/* hack alert -- this algo breaks if more than 9 decimal places present */
if (strchr (str, '.'))
{
int decimals, i, multiplier=1000000000;
str = strchr (str, '.') +1;
nsec = atoi(str) *10000000;
decimals = strcspn (str, "+- ");
for (i=0; i<decimals; i++) multiplier /= 10;
nsec = atoi(str) * multiplier;
}
stm.tm_isdst = -1;
/* timezone format can be +hh or +hhmm or +hh.mm (or -) */
str += strcspn (str, "+-");
stm.tm_hour += atoi(str);
buf[0] = str[0];
buf[1] = str[1];
buf[2] = str[2];
buf[3] = 0;
stm.tm_hour -= atoi(buf);
str +=3;
if ('.' == *str) str++;
if (isdigit (*str) && isdigit (*(str+1)))
{
int cyn;
/* copy sign from hour part */
if ('+' == buf[0]) { cyn = -1; } else { cyn = +1; }
buf[0] = str[0];
buf[1] = str[1];
buf[2] = str[2];
buf[3] = 0;
stm.tm_min += cyn * atoi(buf);
}
/* adjust for the local timezone */
if (do_localtime)
{
int tz_hour=0;
localtime (&tz_hour); /* bogus call, forces 'timezone' to be set */
tz_hour = timezone/3600;
stm.tm_hour -= tz_hour;
stm.tm_min -= (timezone - 3600*tz_hour)/60;
}
/* compute number of seconds */
ts.tv_sec = mktime (&stm);
@ -373,6 +407,18 @@ gnc_iso8601_to_timespec(const char *str)
return ts;
}
Timespec
gnc_iso8601_to_timespec_local(const char *str)
{
return gnc_iso8601_to_timespec(str, 1);
}
Timespec
gnc_iso8601_to_timespec_gmt(const char *str)
{
return gnc_iso8601_to_timespec(str, 0);
}
/********************************************************************\
\********************************************************************/
@ -395,8 +441,8 @@ gnc_timespec_to_iso8601_buff (Timespec ts, char * buff)
/* we also have to print the sign by hand, to work around a bug
* in the glibc 2.1.3 printf (where %+02d fails to zero-pad)
*/
cyn = '+';
if (0>tz_hour) { cyn = '-'; tz_hour = -tz_hour; }
cyn = '-';
if (0>tz_hour) { cyn = '+'; tz_hour = -tz_hour; }
len = sprintf (buff, "%4d-%02d-%02d %02d:%02d:%02d.%06ld %c%02d%02d",
parsed.tm_year+1900, parsed.tm_mon+1, parsed.tm_mday,

View File

@ -148,10 +148,12 @@ Timespec gnc_dmy2timespec (int day, int month, int year);
/* Same as gnc_dmy2timespec, but last second of the day */
Timespec gnc_dmy2timespec_end (int day, int month, int year);
/* The gnc_iso8601_to_timespec() routine converts an ISO-8601 style
/* The gnc_iso8601_to_timespec_xxx() routines converts an ISO-8601 style
* date/time string to Timespec.
* For example: 1998-07-17 11:00:00.68-05
* is 680 milliseconds after 11 o'clock, central daylight time
* The _gmt() routine returns the time in gmt. The _local() routine
* returns the local time.
*
* The gnc_timespec_to_iso8601_buff() routine prints a Timespec
* as an ISO-8601 style string. The buffer must be long enough
@ -159,7 +161,8 @@ Timespec gnc_dmy2timespec_end (int day, int month, int year);
* routine returns a pointer to the null terminator (and can
* thus be used in the 'stpcpy' metaphor of string concatenation).
*/
Timespec gnc_iso8601_to_timespec(const char *);
Timespec gnc_iso8601_to_timespec_local(const char *);
Timespec gnc_iso8601_to_timespec_gmt(const char *);
char * gnc_timespec_to_iso8601_buff (Timespec ts, char * buff);
#endif /* __XACC_DATE_H__ */