diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2020-10-07 07:28:43 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2020-10-07 07:28:43 -0700 |
commit | 59f8d2082b2687669d0ec55213776456bfde9506 (patch) | |
tree | acf8c1c80989f6ed481c066704ccdcef0c04a89f /time.c | |
parent | dff49e8198641254308cf77cc953742acd5ad935 (diff) | |
download | txr-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.
Diffstat (limited to 'time.c')
-rw-r--r-- | time.c | 17 |
1 files changed, 17 insertions, 0 deletions
@@ -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)); |