ref: bda10587a8c0e776dbc7f2717bce95c16e259b8b
dir: /time_posix.c/
#include "flisp.h" #include "timefuncs.h" double sec_realtime(void) { struct timespec now; if(clock_gettime(CLOCK_REALTIME, &now) != 0) return 0; return (double)now.tv_sec + (double)now.tv_nsec/1.0e9; } uint64_t nanosec_monotonic(void) { static int64_t z; struct timespec now; if(clock_gettime(CLOCK_MONOTONIC, &now) != 0) return 0; if(z == 0){ z = now.tv_sec*1000000000LL + now.tv_nsec; return 0; } return now.tv_sec*1000000000LL + now.tv_nsec - z; } void timestring(double s, char *buf, int sz) { time_t tme = (time_t)s; char *fmt = "%c"; /* needed to suppress GCC warning */ struct tm tm; localtime_r(&tme, &tm); strftime(buf, sz, fmt, &tm); } double parsetime(const char *s) { char *fmt = "%c"; /* needed to suppress GCC warning */ char *res; time_t t; struct tm tm; res = strptime(s, fmt, &tm); if(res != nil){ /* Not set by strptime(); tells mktime() to determine * whether daylight saving time is in effect */ tm.tm_isdst = -1; t = mktime(&tm); if(t == (time_t)-1) return -1; return (double)t; } return -1; } void sleep_ms(int ms) { if(ms != 0){ struct timeval timeout; timeout.tv_sec = ms/1000; timeout.tv_usec = (ms % 1000) * 1000; select(0, nil, nil, nil, &timeout); } }