shithub: npe

Download patch

ref: 720ba9765a63caefebafc4d2923dd10febfe99e9
parent: 85aac117f40895565f61eb703055a29df3c7539f
author: zamfofex <zamfofex@twdb.moe>
date: Thu Dec 21 06:57:18 EST 2023

npe: add clock_gettime

--- a/include/npe/time.h
+++ b/include/npe/time.h
@@ -4,6 +4,7 @@
 #include <npe.h>
 
 typedef long time_t;
+typedef int clockid_t;
 
 struct tm {
 	int tm_sec;
@@ -18,6 +19,16 @@
 	char *tm_zone;
 };
 
+struct timespec {
+	time_t tv_sec;
+	long tv_nsec;
+};
+
+enum {
+	CLOCK_REALTIME,
+	CLOCK_MONOTONIC,
+};
+
 #include <sys/time.h>
 
 #define localtime npe_localtime
@@ -26,5 +37,6 @@
 struct tm *npe_gmtime(time_t *timep);
 size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);
 time_t mktime(struct tm *tm);
+int clock_gettime(clockid_t clockid, struct timespec *tp);
 
 #endif
--- /dev/null
+++ b/libnpe/clock_gettime.c
@@ -1,0 +1,23 @@
+#include <errno.h>
+#include <time.h>
+#include "_npe.h"
+
+int
+clock_gettime(clockid_t clockid, struct timespec *tp)
+{
+	uvlong t;
+
+	if(clockid == CLOCK_REALTIME)
+		t = nsec();
+	else if(clockid == CLOCK_MONOTONIC)
+		t = npe_nanosec();
+	else{
+		errno = EINVAL;
+		return -1;
+	}
+
+	tp->tv_nsec = t % 1000000000;
+	tp->tv_sec = t / 1000000000;
+
+	return 0;
+}
--- a/libnpe/mkfile
+++ b/libnpe/mkfile
@@ -16,6 +16,7 @@
 	acosh.$O\
 	basename.$O\
 	cbrtf.$O\
+	clock_gettime.$O\
 	closedir.$O\
 	dirfd.$O\
 	dirname.$O\