12 time_t utc_timestamp = _mkgmtime(&temp);
17 time_t local_timestamp = mktime(&temp);
19 return (
long)(utc_timestamp - local_timestamp);
22static struct tm *localtime_r(
const time_t *timer,
struct tm *buf) {
23 if (timer == NULL || buf == NULL) {
27 errno_t err = localtime_s(buf, timer);
36static struct tm* gmtime_r(
const time_t* timer,
struct tm* buf) {
37 if (timer == NULL || buf == NULL) {
41 errno_t err = gmtime_s(buf, timer);
50#if !defined (__CYGWIN__) && !defined (__MINGW32__)
54#define DELTA_EPOCH_IN_MICROSECS 116444736000000000LL
56int clock_gettime(
int clk_id,
struct timespec *tp) {
60 if (clk_id == CLOCK_REALTIME) {
62 GetSystemTimePreciseAsFileTime(&ft);
64 uint64_t t = ((uint64_t)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
66 t -= DELTA_EPOCH_IN_MICROSECS;
68 tp->tv_sec = (time_t)(t / 10000000);
69 tp->tv_nsec = (long)((t % 10000000) * 100);
72 }
else if (clk_id == CLOCK_MONOTONIC) {
73 static LARGE_INTEGER freq = {0};
77 if (freq.QuadPart == 0) {
78 if (!QueryPerformanceFrequency(&freq))
82 if (!QueryPerformanceCounter(&count))
86 tp->tv_sec = (time_t)(count.QuadPart / freq.QuadPart);
90 tp->tv_nsec = (long)(((count.QuadPart % freq.QuadPart) * 1000000000) / freq.QuadPart);
98int nanosleep(
const struct timespec *req,
struct timespec *rem) {
99 if (req == NULL || req->tv_sec < 0 || req->tv_nsec < 0 || req->tv_nsec >= 1000000000) {
108 int64_t interval = req->tv_sec * 10000000LL + req->tv_nsec / 100;
109 li.QuadPart = -interval;
113#ifndef CREATE_WAITABLE_TIMER_HIGH_RESOLUTION
114#define CREATE_WAITABLE_TIMER_HIGH_RESOLUTION 0x00000002
117 HANDLE timer = CreateWaitableTimerEx(NULL, NULL, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS);
121 timer = CreateWaitableTimer(NULL, TRUE, NULL);
126 if (!SetWaitableTimer(timer, &li, 0, NULL, NULL, FALSE)) {
132 WaitForSingleObject(timer, INFINITE);
168 obj->weak_slots_head =
nullptr;
173 struct timespec ts{};
174 clock_gettime(CLOCK_REALTIME, &ts);
175 int64_t seconds = ts.tv_sec;
176 int64_t nanoseconds = ts.tv_nsec;
181 struct timespec ts{};
183 ts.tv_nsec = nanoseconds;
184 nanosleep(&ts,
nullptr);
188 time_t t = timestamp;
197 time_t now = time(
nullptr);
199 localtime_r(&now, &local_time);
204 struct timespec ts{};
205 clock_gettime(CLOCK_MONOTONIC, &ts);
206 int64_t seconds = ts.tv_sec;
207 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.
void runtime_weak_slot_nullify_all(WeakSlot **weak_slots_head_ptr)