11 time_t utc_timestamp = _mkgmtime(&temp);
16 time_t local_timestamp = mktime(&temp);
18 return (
long)(utc_timestamp - local_timestamp);
21static struct tm *localtime_r(
const time_t *timer,
struct tm *buf) {
22 if (timer == NULL || buf == NULL) {
26 errno_t err = localtime_s(buf, timer);
35static struct tm* gmtime_r(
const time_t* timer,
struct tm* buf) {
36 if (timer == NULL || buf == NULL) {
40 errno_t err = gmtime_s(buf, timer);
49#if !defined (__CYGWIN__) && !defined (__MINGW32__)
53#define DELTA_EPOCH_IN_MICROSECS 116444736000000000LL
55int clock_gettime(
int clk_id,
struct timespec *tp) {
59 if (clk_id == CLOCK_REALTIME) {
61 GetSystemTimePreciseAsFileTime(&ft);
63 uint64_t t = ((uint64_t)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
65 t -= DELTA_EPOCH_IN_MICROSECS;
67 tp->tv_sec = (time_t)(t / 10000000);
68 tp->tv_nsec = (long)((t % 10000000) * 100);
71 }
else if (clk_id == CLOCK_MONOTONIC) {
72 static LARGE_INTEGER freq = {0};
76 if (freq.QuadPart == 0) {
77 if (!QueryPerformanceFrequency(&freq))
81 if (!QueryPerformanceCounter(&count))
85 tp->tv_sec = (time_t)(count.QuadPart / freq.QuadPart);
89 tp->tv_nsec = (long)(((count.QuadPart % freq.QuadPart) * 1000000000) / freq.QuadPart);
97int nanosleep(
const struct timespec *req,
struct timespec *rem) {
98 if (req == NULL || req->tv_sec < 0 || req->tv_nsec < 0 || req->tv_nsec >= 1000000000) {
107 int64_t interval = req->tv_sec * 10000000LL + req->tv_nsec / 100;
108 li.QuadPart = -interval;
112#ifndef CREATE_WAITABLE_TIMER_HIGH_RESOLUTION
113#define CREATE_WAITABLE_TIMER_HIGH_RESOLUTION 0x00000002
116 HANDLE timer = CreateWaitableTimerEx(NULL, NULL, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS);
120 timer = CreateWaitableTimer(NULL, TRUE, NULL);
125 if (!SetWaitableTimer(timer, &li, 0, NULL, NULL, FALSE)) {
131 WaitForSingleObject(timer, INFINITE);
170 struct timespec ts{};
171 clock_gettime(CLOCK_REALTIME, &ts);
172 int64_t seconds = ts.tv_sec;
173 int64_t nanoseconds = ts.tv_nsec;
178 struct timespec ts{};
180 ts.tv_nsec = nanoseconds;
181 nanosleep(&ts,
nullptr);
185 time_t t = timestamp;
194 time_t now = time(
nullptr);
196 localtime_r(&now, &local_time);
201 struct timespec ts{};
202 clock_gettime(CLOCK_MONOTONIC, &ts);
203 int64_t seconds = ts.tv_sec;
204 int64_t nanoseconds = ts.tv_nsec;
void * runtime_object_alloc(unsigned long size)
void runtime_finalize_object(YoiObject *object)
unsigned long long gc_refcount
unsigned long long seconds
unsigned long long nanoseconds
static void gc_refcount_decrease(YoiIntAndIntObject *obj)
static YoiIntAndIntObject * create(int64_t seconds, int64_t nanoseconds)
int64_t runtime_time_localtimezone_offset()
Get the local timezone offset in seconds.
long get_tm_gmtoff(struct tm *t)
YoiIntAndIntObject * runtime_time_monotonic_now()
Get the current monotonic time as a YoiIntAndIntObject.
void runtime_time_sleep(int64_t seconds, int64_t nanoseconds)
Sleep for a given number of seconds.
YoiIntAndIntObject * runtime_time_now()
Return the current time as a YoiIntAndIntObject.
void runtime_time_finalize(void *memory)
Finalize the memory created by the time module.
char * runtime_time_strftime(const char *format, int64_t timestamp)
Convert a timestamp to a string according to a given format.