ref: fd1e36f98d997c8183d3040b66fa6f9cd91e825b
dir: /sys_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;
struct tm tm;
*buf = 0;
if(localtime_r(&tme, &tm) != nil)
strftime(buf, sz, "%a %b %e %H:%M:%S %Y", &tm);
}
double
parsetime(const char *s)
{
char *res;
time_t t;
struct tm tm;
res = strptime(s, "%c", &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);
}
}
static const uint8_t boot[] = {
#include "flisp.boot.h"
};
char __os_version__[64];
#include <sys/utsname.h>
int
main(int argc, char **argv)
{
setlocale(LC_NUMERIC, "C");
struct utsname u;
__os_version__[0] = 0;
if(uname(&u) == 0)
snprintf(__os_version__, sizeof(__os_version__), "%s", u.release);
flmain(boot, sizeof(boot), argc, argv);
}