From b4efd6c6dc386de5ff9ce9f2c3fba251c34a232b Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 15 May 2013 20:46:35 -0700 Subject: * 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. --- lib.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'lib.c') diff --git a/lib.c b/lib.c index 2d9fa99a..eb34fc62 100644 --- a/lib.c +++ b/lib.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #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) { -- cgit v1.2.3