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 | |
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.
-rw-r--r-- | ChangeLog | 14 | ||||
-rw-r--r-- | RELNOTES | 2 | ||||
-rw-r--r-- | eval.c | 2 | ||||
-rw-r--r-- | lib.c | 39 | ||||
-rw-r--r-- | lib.h | 2 | ||||
-rw-r--r-- | txr.1 | 34 |
6 files changed, 88 insertions, 5 deletions
@@ -1,5 +1,19 @@ 2013-05-15 Kaz Kylheku <kaz@kylheku.com> + * 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. + +2013-05-15 Kaz Kylheku <kaz@kylheku.com> + * match.c (match_fun): Support debug stop on the function prior to the call. The first data line number is 1, not zero, if there is data. @@ -37,6 +37,8 @@ - New package-related functions. + - New functions for obtaining time of day and converting to text. + Bugs - Fixed broken (+ <fixnum> <char>) case in addition. @@ -2467,6 +2467,8 @@ void eval_init(void) reg_fun(intern(lit("time"), user_package), func_n0(time_sec)); reg_fun(intern(lit("time-usec"), user_package), func_n0(time_sec_usec)); + reg_fun(intern(lit("time-string-local"), user_package), func_n2(time_string_local)); + reg_fun(intern(lit("time-string-utc"), user_package), func_n2(time_string_utc)); reg_fun(intern(lit("source-loc"), user_package), func_n1(source_loc)); reg_fun(intern(lit("source-loc-str"), user_package), func_n1(source_loc_str)); @@ -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) { @@ -663,6 +663,8 @@ val tostring(val obj); val tostringp(val obj); val time_sec(void); val time_sec_usec(void); +val time_string_local(val time, val format); +val time_string_utc(val time, val format); void init(const wchar_t *progname, mem_t *(*oom_realloc)(mem_t *, size_t), val *stack_bottom); @@ -10266,12 +10266,36 @@ Syntax: .TP Description: -The time functions return the local time in the system's timezone. The time function returns the number of seconds that have elapsed since -midnight, January 1, 1970. The time-usec function returns a cons cell whose -car field holds the seconds measured in the same way, and whose cdr field -extends the precision by giving number of microseconds as an integer value -between 0 and 999999. +midnight, January 1, 1970, in the UTC timezone. + +The time-usec function returns a cons cell whose car field holds the seconds +measured in the same way, and whose cdr field extends the precision by giving +number of microseconds as an integer value between 0 and 999999. + +.SS Functions time-string-local and time-string-utc + +.TP +Syntax: + + (time-string-local <format> <time>) + (time-string-utc <format> <time>) + +.TP +Description: + +These functions take the numeric time returned by the time function, +and convert it to a textual representation in a very flexible way, according to +a detailed format string. + +The time-string-local function converts the time to the local timezone of +the host system. The time-string-utc function produces time in UTC. + +The <format> argument is a string, and follows exactly the same conventions as +the format string of the C library function strftime. + +The <time> argument is an integer representing seconds obtained from the +time function or from the time-usec function. .SH WEB PROGRAMMING SUPPORT |