From e3c72052c108c312d9a8a1d9bad7f8bae9a9e2c3 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 7 Oct 2020 07:28:43 -0700 Subject: 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. --- configure | 20 ++++++++++++++++++++ time.c | 17 +++++++++++++++++ time.h | 1 + txr.1 | 17 ++++++++++++++++- 4 files changed, 54 insertions(+), 1 deletion(-) 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 < + +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 < 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 ) -- cgit v1.2.3