summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2015-08-31 08:35:23 -0700
committerKaz Kylheku <kaz@kylheku.com>2015-08-31 08:35:23 -0700
commit4f5154e6e22f133edf3a43b61eb44e789c425c45 (patch)
treec47eeada15d9c8ebe7af6f811bfe5ed11a1dc57a
parent7660334154c96f46c4051403fce18f3828e852ed (diff)
downloadtxr-4f5154e6e22f133edf3a43b61eb44e789c425c45.tar.gz
txr-4f5154e6e22f133edf3a43b61eb44e789c425c45.tar.bz2
txr-4f5154e6e22f133edf3a43b61eb44e789c425c45.zip
Time structure.
* eval.c (eval_init): Register time-struct-local and time-struct-utc intrinsic funtions. * lib.c (time_s, year_s, month_s, day_s, hour_s, min_s, sec_s): New global symbol variables. (broken_time_struct, time_init): New static functions. (time_struct_local, time_struct_utc): New functions. (init): Call time_init. * lib.h (time_struct_local, time_struct_utc): Declared.
-rw-r--r--eval.c2
-rw-r--r--lib.c58
-rw-r--r--lib.h2
3 files changed, 62 insertions, 0 deletions
diff --git a/eval.c b/eval.c
index d5345c78..eddc3809 100644
--- a/eval.c
+++ b/eval.c
@@ -4614,6 +4614,8 @@ void eval_init(void)
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("time-struct-local"), user_package), func_n1(time_struct_local));
+ reg_fun(intern(lit("time-struct-utc"), user_package), func_n1(time_struct_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 fde73899..d3354e21 100644
--- a/lib.c
+++ b/lib.c
@@ -112,6 +112,8 @@ val list_f, less_f, greater_f;
val prog_string;
+val time_s, year_s, month_s, day_s, hour_s, min_s, sec_s, dst_s;
+
static val env_list;
mem_t *(*oom_realloc)(mem_t *, size_t);
@@ -7517,6 +7519,22 @@ static val broken_time_list(struct tm *tms)
nao);
}
+static val broken_time_struct(struct tm *tms)
+{
+ args_decl(args, ARGS_MIN);
+ val ts = make_struct(time_s, nil, args);
+
+ slotset(ts, year_s, num(tms->tm_year + 1900));
+ slotset(ts, month_s, num_fast(tms->tm_mon + 1));
+ slotset(ts, day_s, num_fast(tms->tm_mday));
+ slotset(ts, hour_s, num_fast(tms->tm_hour));
+ slotset(ts, min_s, num_fast(tms->tm_min));
+ slotset(ts, sec_s, num_fast(tms->tm_sec));
+ slotset(ts, dst_s, tnil(tms->tm_isdst));
+
+ return ts;
+}
+
val time_fields_local(val time)
{
struct tm tms;
@@ -7539,6 +7557,29 @@ val time_fields_utc(val time)
return broken_time_list(&tms);
}
+val time_struct_local(val time)
+{
+ struct tm tms;
+ time_t secs = c_num(time);
+
+ if (localtime_r(&secs, &tms) == 0)
+ return nil;
+
+ return broken_time_struct(&tms);
+}
+
+val time_struct_utc(val time)
+{
+ struct tm tms;
+ time_t secs = c_num(time);
+
+ if (gmtime_r(&secs, &tms) == 0)
+ return nil;
+
+ return broken_time_struct(&tms);
+}
+
+
static val make_time_impl(time_t (*pmktime)(struct tm *),
val year, val month, val day,
val hour, val minute, val second,
@@ -7631,6 +7672,22 @@ val make_time_utc(val year, val month, val day,
return make_time_impl(pmktime, year, month, day, hour, minute, second, isdst);
}
+static void time_init(void)
+{
+ time_s = intern(lit("time"), user_package);
+ year_s = intern(lit("year"), user_package);
+ month_s = intern(lit("month"), user_package);
+ day_s = intern(lit("day"), user_package);
+ hour_s = intern(lit("hour"), user_package);
+ min_s = intern(lit("min"), user_package);
+ sec_s = intern(lit("sec"), user_package);
+ dst_s = intern(lit("dst"), user_package);
+
+ make_struct_type(time_s, nil,
+ list(year_s, month_s, day_s,
+ hour_s, min_s, sec_s, dst_s, nao), nil, nil);
+}
+
void init(const wchar_t *pn, mem_t *(*oom)(mem_t *, size_t),
val *stack_bottom)
{
@@ -7665,6 +7722,7 @@ void init(const wchar_t *pn, mem_t *(*oom)(mem_t *, size_t),
glob_init();
#endif
cadr_init();
+ time_init();
gc_state(gc_save);
}
diff --git a/lib.h b/lib.h
index 93a2b446..86c3bf1d 100644
--- a/lib.h
+++ b/lib.h
@@ -893,6 +893,8 @@ 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 time_struct_local(val time);
+val time_struct_utc(val time);
val make_time(val year, val month, val day,
val hour, val minute, val second,
val isdst);