shithub: femtolisp

ref: 10d16c10bed72a177a99e0a9a3f3caca6c38ce94
dir: /sys_posix.c/

View raw version
#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;

	localtime_r(&tme, &tm);
	strftime(buf, sz, "%c", &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"
};

int
main(int argc, char **argv)
{
	setlocale(LC_NUMERIC, "C");
	flmain(boot, sizeof(boot), argc, argv);
}