add iso8601-format date string handling utility function

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@3542 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Linas Vepstas 2001-01-27 17:41:38 +00:00
parent 6758087ed7
commit 1884c73482
2 changed files with 47 additions and 4 deletions

View File

@ -373,6 +373,42 @@ gnc_iso8601_to_timespec(const char *str)
return ts; return ts;
} }
/********************************************************************\
\********************************************************************/
char *
gnc_timespec_to_iso8601_buff (Timespec ts, char * buff)
{
int len;
int tz_hour, tz_min;
char cyn;
time_t tmp;
struct tm parsed;
tmp = ts.tv_sec;
localtime_r(&tmp, &parsed);
tz_hour = timezone/3600;
tz_min = (timezone - 3600*tz_hour)/60;
if (0>tz_min) { tz_min +=60; tz_hour --; }
/* 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; }
len = sprintf (buff, "%4d-%02d-%02d %02d:%02d:%02d.%06ld %c%02d%02d",
parsed.tm_year+1900, parsed.tm_mon+1, parsed.tm_mday,
parsed.tm_hour, parsed.tm_min, parsed.tm_sec,
ts.tv_nsec/1000, cyn, tz_hour, tz_min);
/* return pointer to end of string */
buff += len;
return buff;
}
/********************************************************************\ /********************************************************************\
\********************************************************************/ \********************************************************************/
/* hack alert -- this routine returns incorrect values for /* hack alert -- this routine returns incorrect values for

View File

@ -148,11 +148,18 @@ Timespec gnc_dmy2timespec (int day, int month, int year);
/* Same as gnc_dmy2timespec, but last second of the day */ /* Same as gnc_dmy2timespec, but last second of the day */
Timespec gnc_dmy2timespec_end (int day, int month, int year); Timespec gnc_dmy2timespec_end (int day, int month, int year);
/* The gnc_iso8601_to_timespec() routine /* The gnc_iso8601_to_timespec() routine converts an ISO-8601 style
* converts an ISO-8601 style date/time string to Timespec. * date/time string to Timespec.
* For example: 1998-07-17 11:00:00.68-05 * For example: 1998-07-17 11:00:00.68-05
* is 680 milliseconds after 11 o'clock, central daylight time * is 680 milliseconds after 11 o'clock, central daylight time
*
* The gnc_timespec_to_iso8601_buff() routine prints a Timespec
* as an ISO-8601 style string. The buffer must be long enough
* to contain the string. The string is null-terminated. This
* 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(const char *);
char * gnc_timespec_to_iso8601_buff (Timespec ts, char * buff);
#endif /* __XACC_DATE_H__ */ #endif /* __XACC_DATE_H__ */