diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2017-04-04 00:08:01 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2017-04-04 00:08:01 -0700 |
commit | 93f0e3d10580b0a8126018d0a05dd46b1aa84bf6 (patch) | |
tree | f16479c293e657f411cde1346f37df224fe821f2 /lib.c | |
parent | d2aac0db3dbabc163aad4722025db6051f56d804 (diff) | |
download | txr-93f0e3d10580b0a8126018d0a05dd46b1aa84bf6.tar.gz txr-93f0e3d10580b0a8126018d0a05dd46b1aa84bf6.tar.bz2 txr-93f0e3d10580b0a8126018d0a05dd46b1aa84bf6.zip |
New time-parse-local and time-parse-utc functions.
* eval.c (eval_init): Register intrinsic functions
time-parse-local and time-parse-utc.
* lib.c (strptime_wrap): New static function.
(time_parse): Now implemented as by call to strptime_wrap.
(time_parse_local, time_parse_utc): New functions.
These get the time_t time from struct tm without
constructing the intermediate Lisp structure.
* lib.h (time_parse_local, time_parse_utc): Declared.
* txr.1: Documented new functions.
Diffstat (limited to 'lib.c')
-rw-r--r-- | lib.c | 32 |
1 files changed, 29 insertions, 3 deletions
@@ -10657,17 +10657,23 @@ static struct tm epoch_tm(void) return ep; } -val time_parse(val format, val string) +static int strptime_wrap(val string, val format, struct tm *ptms) { - struct tm tms = epoch_tm(); const wchar_t *w_str = c_str(string); const wchar_t *w_fmt = c_str(format); char *str = utf8_dup_to(w_str); char *fmt = utf8_dup_to(w_fmt); - char *ptr = strptime(str, fmt, &tms); + char *ptr = strptime(str, fmt, ptms); int ret = ptr != 0; free(fmt); free(str); + return ret; +} + +val time_parse(val format, val string) +{ + struct tm tms = epoch_tm(); + int ret = strptime_wrap(string, format, &tms); return ret ? broken_time_struct(&tms) : nil; } @@ -10787,6 +10793,26 @@ static val time_parse_meth(val time_struct, val format, val string) return ret; } +val time_parse_local(val format, val string) +{ + struct tm tms = epoch_tm(); + if (!strptime_wrap(string, format, &tms)) + return nil; + return num(mktime(&tms)); +} + +val time_parse_utc(val format, val string) +{ + struct tm tms = epoch_tm(); + if (!strptime_wrap(string, format, &tms)) + return nil; +#if HAVE_TIMEGM + return num(timegm(&tms)); +#else + return num(timegm_hack(&tms)); +#endif +} + #endif static void time_init(void) |