diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2013-05-15 20:46:35 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2013-05-15 20:46:35 -0700 |
commit | b4efd6c6dc386de5ff9ce9f2c3fba251c34a232b (patch) | |
tree | f809d027234c315c17cf86ab84027b9bdf52f170 /lib.c | |
parent | 10bbad5126477bbf9e46a37d4c323f87f8273dbb (diff) | |
download | txr-b4efd6c6dc386de5ff9ce9f2c3fba251c34a232b.tar.gz txr-b4efd6c6dc386de5ff9ce9f2c3fba251c34a232b.tar.bz2 txr-b4efd6c6dc386de5ff9ce9f2c3fba251c34a232b.zip |
* eval.c (eval_init): New intrinsics, time-string-local and
time-string-utc.
* lib.c (string_time): New static function.
(time_string_local, time_string_utc): New functions.
* lib.h (time_string_local, time_string_utc): Declared.
* txr.1: Documented.
* RELNOTES: Updated.
Diffstat (limited to 'lib.c')
-rw-r--r-- | lib.c | 39 |
1 files changed, 39 insertions, 0 deletions
@@ -36,6 +36,7 @@ #include <errno.h> #include <wchar.h> #include <math.h> +#include <time.h> #include <sys/time.h> #include "config.h" #ifdef HAVE_GETENVIRONMENTSTRINGS @@ -4807,6 +4808,44 @@ val time_sec_usec(void) return cons(num(tv.tv_sec), num(tv.tv_usec)); } +static val string_time(struct tm *(*break_time_fn)(const time_t *, struct tm *), + char *format, time_t time) +{ + char buffer[512] = ""; + struct tm broken_out_time; + + if (break_time_fn(&time, &broken_out_time) == 0) + return nil; + + if (strftime(buffer, sizeof buffer, format, &broken_out_time) == 0) + buffer[0] = 0; + + { + wchar_t *wctime = utf8_dup_from(buffer); + return string_own(wctime); + } +} + +val time_string_local(val time, val format) +{ + time_t secs = c_num(time); + const wchar_t *wcfmt = c_str(format); + char *u8fmt = utf8_dup_to(wcfmt); + val timestr = string_time(localtime_r, u8fmt, secs); + free(u8fmt); + return timestr; +} + +val time_string_utc(val time, val format) +{ + time_t secs = c_num(time); + const wchar_t *wcfmt = c_str(format); + char *u8fmt = utf8_dup_to(wcfmt); + val timestr = string_time(gmtime_r, u8fmt, secs); + free(u8fmt); + return timestr; +} + void init(const wchar_t *pn, mem_t *(*oom)(mem_t *, size_t), val *stack_bottom) { |