summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2016-03-24 18:29:34 -0700
committerKaz Kylheku <kaz@kylheku.com>2016-03-24 18:29:34 -0700
commite96cb9e2065a066ede10e3ce36eba828fd42a97b (patch)
tree7fb9c2476df5c2247cba513c307aeec689ed8cec
parent4882a1c512a6d64ae8f6de2ed849033f8637cf90 (diff)
downloadtxr-e96cb9e2065a066ede10e3ce36eba828fd42a97b.tar.gz
txr-e96cb9e2065a066ede10e3ce36eba828fd42a97b.tar.bz2
txr-e96cb9e2065a066ede10e3ce36eba828fd42a97b.zip
Methods in time struct: time-local and time-utc.
* lib.c (time_local_s, time_utc_s): New symbol variables. (time_meth): New static function. (time_init): Initialize new symbol variables. Create the time struct with two static slots, and initialize those static slots to be methods. * txr.1: Introduce "the epoch" term. Document the new methods.
-rw-r--r--lib.c32
-rw-r--r--txr.139
2 files changed, 66 insertions, 5 deletions
diff --git a/lib.c b/lib.c
index 32d5b5e7..e29ff99c 100644
--- a/lib.c
+++ b/lib.c
@@ -113,7 +113,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;
+val time_s, time_local_s, time_utc_s;
+val year_s, month_s, day_s, hour_s, min_s, sec_s, dst_s;
static val env_list;
@@ -8989,9 +8990,27 @@ val make_time_utc(val year, val month, val day,
return make_time_impl(pmktime, year, month, day, hour, minute, second, isdst);
}
+static val time_meth(val utc_p, val time_struct)
+{
+ val year = slot(time_struct, year_s);
+ val month = slot(time_struct, month_s);
+ val day = slot(time_struct, day_s);
+ val hour = slot(time_struct, hour_s);
+ val min = slot(time_struct, min_s);
+ val sec = slot(time_struct, sec_s);
+ val dst = slot(time_struct, dst_s);
+
+ return (utc_p ? make_time_utc : make_time)(year, month, day,
+ hour, min, sec, dst);
+}
+
static void time_init(void)
{
+ val time_st;
+
time_s = intern(lit("time"), user_package);
+ time_local_s = intern(lit("time-local"), user_package);
+ time_utc_s = intern(lit("time-utc"), user_package);
year_s = intern(lit("year"), user_package);
month_s = intern(lit("month"), user_package);
day_s = intern(lit("day"), user_package);
@@ -9000,9 +9019,14 @@ static void time_init(void)
sec_s = intern(lit("sec"), user_package);
dst_s = intern(lit("dst"), user_package);
- make_struct_type(time_s, nil, nil,
- list(year_s, month_s, day_s,
- hour_s, min_s, sec_s, dst_s, nao), nil, nil, nil, nil);
+ time_st = make_struct_type(time_s, nil,
+ list(time_local_s, time_utc_s, nao),
+ list(year_s, month_s, day_s,
+ hour_s, min_s, sec_s, dst_s, nao),
+ nil, nil, nil, nil);
+
+ static_slot_set(time_st, time_local_s, func_f1(nil, time_meth));
+ static_slot_set(time_st, time_utc_s, func_f1(t, time_meth));
}
void init(const wchar_t *pn, mem_t *(*oom)(mem_t *, size_t),
diff --git a/txr.1 b/txr.1
index f5d4214d..a70d1623 100644
--- a/txr.1
+++ b/txr.1
@@ -35159,7 +35159,9 @@ pseudo-random number generator.
The
.code time
function returns the number of seconds that have elapsed since
-midnight, January 1, 1970, in the UTC timezone.
+midnight, January 1, 1970, in the UTC timezone: a point in
+time called
+.IR "the epoch" .
The
.code time-usec
@@ -35302,6 +35304,41 @@ function or from the
.code time-usec
function.
+.coNP Methods @ time-local and @ time-utc
+.synb
+.mets << time-struct .(time-local)
+.mets << time-struct .(time-utc)
+.syne
+.desc
+The
+.code time
+structure has two methods called
+.code time-local
+and
+.codn time-utc .
+
+The
+.code time-local
+function considers the slots of the
+.code time
+structure instance
+.meta time-struct
+to be local time, and returns its integer representation
+as the number of seconds since the epoch.
+
+The
+.code time-utc
+function is similar, except it considers
+the slots of
+.meta time-struct
+to be in the UTC time zone.
+
+Note: these functions work by converting the slots into arguments
+which are applied to
+.code make-time
+or
+.codn make-time-utc .
+
.coNP Functions @ make-time and @ make-time-utc
.synb
.mets (make-time < year < month < day < hour < minute < second << dst-advice )