summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog14
-rw-r--r--RELNOTES2
-rw-r--r--eval.c2
-rw-r--r--lib.c39
-rw-r--r--lib.h2
-rw-r--r--txr.134
6 files changed, 88 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index ac50250c..39e252dc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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.
diff --git a/RELNOTES b/RELNOTES
index 2c0d5e11..13c1a0e9 100644
--- a/RELNOTES
+++ b/RELNOTES
@@ -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.
diff --git a/eval.c b/eval.c
index 81c7db88..7d1f1758 100644
--- a/eval.c
+++ b/eval.c
@@ -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));
diff --git a/lib.c b/lib.c
index 2d9fa99a..eb34fc62 100644
--- a/lib.c
+++ b/lib.c
@@ -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)
{
diff --git a/lib.h b/lib.h
index 33207145..67a35d26 100644
--- a/lib.h
+++ b/lib.h
@@ -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);
diff --git a/txr.1 b/txr.1
index fa96fffb..31e408cd 100644
--- a/txr.1
+++ b/txr.1
@@ -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