summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog12
-rw-r--r--eval.c2
-rw-r--r--lib.c34
-rw-r--r--lib.h2
-rw-r--r--txr.125
5 files changed, 75 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index ee79b4be..be0057a5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2014-01-20 Kaz Kylheku <kaz@kylheku.com>
+
+ * lib.c (broken_down_time_list): New static function.
+ (time_fields_local, time_fields_utc): New functions.
+
+ * lib.h (time_fields_local, time_fields_utc): Declared.
+
+ * eval.c (eval_init): Intern time_fields_local and time_fields_utc
+ as intrinsic functions.
+
+ * txr.1: Documented.
+
2014-01-16 Kaz Kylheku <kaz@kylheku.com>
* hash.c (group_by): New function.
diff --git a/eval.c b/eval.c
index f57a6f78..e43bd2cc 100644
--- a/eval.c
+++ b/eval.c
@@ -2665,6 +2665,8 @@ void eval_init(void)
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("time-fields-local"), user_package), func_n1(time_fields_local));
+ reg_fun(intern(lit("time-fields-utc"), user_package), func_n1(time_fields_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));
diff --git a/lib.c b/lib.c
index 683b8e3d..cbc50869 100644
--- a/lib.c
+++ b/lib.c
@@ -5163,6 +5163,40 @@ val time_string_utc(val time, val format)
return timestr;
}
+static val broken_time_list(struct tm *tms)
+{
+ return list(num(tms->tm_year + 1900),
+ num_fast(tms->tm_mon + 1),
+ num_fast(tms->tm_mday),
+ num_fast(tms->tm_hour),
+ num_fast(tms->tm_min),
+ num_fast(tms->tm_sec),
+ tms->tm_isdst ? t : nil,
+ nao);
+}
+
+val time_fields_local(val time)
+{
+ struct tm tms;
+ time_t secs = c_num(time);
+
+ if (localtime_r(&secs, &tms) == 0)
+ return nil;
+
+ return broken_time_list(&tms);
+}
+
+val time_fields_utc(val time)
+{
+ struct tm tms;
+ time_t secs = c_num(time);
+
+ if (gmtime_r(&secs, &tms) == 0)
+ return nil;
+
+ return broken_time_list(&tms);
+}
+
static val make_time_impl(time_t (*pmktime)(struct tm *),
val year, val month, val day,
val hour, val minute, val second,
diff --git a/lib.h b/lib.h
index d5158b64..80d1874c 100644
--- a/lib.h
+++ b/lib.h
@@ -687,6 +687,8 @@ 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);
+val time_fields_local(val time);
+val time_fields_utc(val time);
val make_time(val year, val month, val day,
val hour, val minute, val second,
val isdst);
diff --git a/txr.1 b/txr.1
index 96af04b7..22c6615a 100644
--- a/txr.1
+++ b/txr.1
@@ -11212,6 +11212,31 @@ 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.
+.SS Functions time-fields-local and time-fields-utc
+
+.TP
+Syntax:
+
+ (time-fields-local <time>)
+ (time-fields-utc <time>)
+
+.TP
+Description:
+
+These functions take the numeric time returned by the time function,
+and convert it to a list of seven fields.
+
+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 fields returned as a list are six integers, and a boolean value.
+The six integers represent the year, month, day, hour, minute and second.
+The boolean value indicates whether daylight savings time is in effect
+(always nil in the case of time-fields-utc).
+
+The <time> argument is an integer representing seconds obtained from the
+time function or from the time-usec function.
+
.SS Functions make-time and make-time-utc
.TP