From 89a9a4f613a6b4f1bccddec6f01c3af317398132 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Mon, 22 Aug 2016 20:32:21 -0700 Subject: Adding time-parse function: wrapper for strptime. * configure: Test for strptime. * eval.c (eval_init): register time-parse intrinsic. * lib.c (time_parse): New function. * lib.h (time_parse): Declared. * txr.1: Documented. --- configure | 18 ++++++++++++++++++ eval.c | 1 + lib.c | 18 ++++++++++++++++++ lib.h | 3 +++ txr.1 | 28 ++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+) diff --git a/configure b/configure index cbbe2369..f3ec930b 100755 --- a/configure +++ b/configure @@ -1688,6 +1688,24 @@ else printf "no\n" fi +printf "Checking for strptime function ... " + +cat > conftest.c < + +int main(int argc, char **argv) +{ + struct tm stm = { 0 }; + strptime("2016-08-20 00:00:00", "%Y-%m-%d %H:%M:%S", &stm); + return 0; +} +! +if conftest ; then + printf "yes\n" + printf "#define HAVE_STRPTIME 1\n" >> $config_h +else + printf "no\n" +fi printf "Checking for POSIX sleep function ... " diff --git a/eval.c b/eval.c index 39ee9652..78cc5a34 100644 --- a/eval.c +++ b/eval.c @@ -5337,6 +5337,7 @@ void eval_init(void) reg_fun(intern(lit("time-struct-utc"), user_package), func_n1(time_struct_utc)); reg_fun(intern(lit("make-time"), user_package), func_n7(make_time)); reg_fun(intern(lit("make-time-utc"), user_package), func_n7(make_time_utc)); + reg_fun(intern(lit("time-parse"), user_package), func_n2(time_parse)); reg_fun(intern(lit("source-loc"), user_package), func_n1(source_loc)); reg_fun(intern(lit("source-loc-str"), user_package), func_n2o(source_loc_str, 1)); diff --git a/lib.c b/lib.c index d497eb20..6352eb2a 100644 --- a/lib.c +++ b/lib.c @@ -9374,6 +9374,24 @@ val make_time(val year, val month, val day, return make_time_impl(mktime, year, month, day, hour, minute, second, isdst); } +#if HAVE_STRPTIME + +val time_parse(val format, val string) +{ + struct tm tms = { 0 }; + 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); + int ret = ptr != 0; + free(fmt); + free(str); + return ret ? broken_time_struct(&tms) : nil; +} + +#endif + #if !HAVE_SETENV void setenv(const char *name, const char *value, int overwrite) diff --git a/lib.h b/lib.h index c815658a..0785639f 100644 --- a/lib.h +++ b/lib.h @@ -992,6 +992,9 @@ val make_time(val year, val month, val day, val make_time_utc(val year, val month, val day, val hour, val minute, val second, val isdst); +#if HAVE_STRPTIME +val time_parse(val format, val string); +#endif void init(mem_t *(*oom_realloc)(mem_t *, size_t), val *stack_bottom); int compat_fixup(int compat_ver); diff --git a/txr.1 b/txr.1 index 3c824e57..44141f12 100644 --- a/txr.1 +++ b/txr.1 @@ -36516,6 +36516,34 @@ function or from the .code time-usec function. +.coNP Function @ time-parse +.synb +.mets (time-parse < format << string ) +.syne +.desc +The +.code time-parse +function scans a time description in +.meta string +according to the specification given in the +.meta format +string. If the scan is successful, a structure +of type +.code time +is returned, otherwise +.codn nil . + +The +.meta format +argument follows the same conventions as the POSIX +C library function +.codn strptime . + +Note: the availability of +.code time-parse +depends on the availability of +.codn strptime . + .coNP Methods @ time-local and @ time-utc .synb .mets << time-struct .(time-local) -- cgit v1.2.3