summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2020-10-07 07:28:43 -0700
committerKaz Kylheku <kaz@kylheku.com>2020-10-07 07:28:43 -0700
commit59f8d2082b2687669d0ec55213776456bfde9506 (patch)
treeacf8c1c80989f6ed481c066704ccdcef0c04a89f
parentdff49e8198641254308cf77cc953742acd5ad935 (diff)
downloadtxr-59f8d2082b2687669d0ec55213776456bfde9506.tar.gz
txr-59f8d2082b2687669d0ec55213776456bfde9506.tar.bz2
txr-59f8d2082b2687669d0ec55213776456bfde9506.zip
New function: time-nsec.
* configure: Test for clock_gettime, generating HAVE_CLOCK_GETTIME symbol in config.h. * time.c (time_sec_nsec): New function. (time_init): time-nsec intrinsic registered. * time.h (time_sec_nsec): Declared. * txr.1: Documented.
-rwxr-xr-xconfigure20
-rw-r--r--time.c17
-rw-r--r--time.h1
-rw-r--r--txr.117
4 files changed, 54 insertions, 1 deletions
diff --git a/configure b/configure
index 92e8715d..edbf398b 100755
--- a/configure
+++ b/configure
@@ -3593,6 +3593,26 @@ else
printf "no\n"
fi
+printf "Checking for clock_gettime ... "
+cat > conftest.c <<!
+#include <time.h>
+
+int main(void)
+{
+ struct timespec ts;
+ (void) clock_gettime(CLOCK_REALTIME, &ts);
+ return 0;
+}
+!
+
+if conftest ; then
+ printf "yes\n"
+ printf "#define HAVE_CLOCK_GETTIME 1\n" >> config.h
+ have_sys_types=y
+else
+ printf "no\n"
+fi
+
printf "Checking for loff_t ... "
cat > conftest.c <<!
#include <sys/types.h>
diff --git a/time.c b/time.c
index a24255b7..07d2d695 100644
--- a/time.c
+++ b/time.c
@@ -63,6 +63,22 @@ val time_sec_usec(void)
return cons(num_time(tv.tv_sec), num(tv.tv_usec));
}
+val time_sec_nsec(void)
+{
+#if HAVE_CLOCK_GETTIME
+ struct timespec ts;
+ if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
+ return nil;
+ return cons(num_time(ts.tv_sec), num(ts.tv_nsec));
+#else
+ struct timeval tv;
+ if (gettimeofday(&tv, 0) == -1)
+ return nil;
+ return cons(num_time(tv.tv_sec), num(1000 * tv.tv_usec));
+#endif
+}
+
+
#if !HAVE_GMTIME_R
struct tm *gmtime_r(const time_t *timep, struct tm *result);
struct tm *localtime_r(const time_t *timep, struct tm *result);
@@ -497,6 +513,7 @@ void time_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-nsec"), user_package), func_n0(time_sec_nsec));
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));
diff --git a/time.h b/time.h
index e05691a6..b33531d6 100644
--- a/time.h
+++ b/time.h
@@ -31,6 +31,7 @@ extern val dst_s, gmtoff_s, zone_s;
val time_sec(void);
val time_sec_usec(void);
+val time_sec_nsec(void);
val time_string_local(val time, val format);
val time_string_utc(val time, val format);
val time_fields_local(val time);
diff --git a/txr.1 b/txr.1
index b08edbfd..395697c3 100644
--- a/txr.1
+++ b/txr.1
@@ -55976,10 +55976,11 @@ parameter, which defaults to the value of
.codn *random-state* .
.SS* Time
-.coNP Functions @ time and @ time-usec
+.coNP Functions @, time @ time-usec and @ time-nsec
.synb
.mets (time)
.mets (time-usec)
+.mets (time-nsec)
.syne
.desc
The
@@ -55998,6 +55999,20 @@ field holds the seconds measured in the same way, and whose
field extends the precision by giving
number of microseconds as an integer value between 0 and 999999.
+The
+.code time-nsec
+function is similar to
+.code time-usec
+except that the returned cons cell's
+.code cdr
+field gives a number of nanoseconds as an integer value
+between 0 and 999999999.
+
+Note: on hosts where obtaining nanosecond precision is not available, the
+.code time-nsec
+function obtains a microseconds value instead, and multiplies
+it by 1000.
+
.coNP Functions @ time-string-local and @ time-string-utc
.synb
.mets (time-string-local < time << format )