From 71552a57dead2ae1639043ca3008674d87c7688c Mon Sep 17 00:00:00 2001 From: Tenari Date: Thu, 2 Jul 2026 08:59:18 -0500 Subject: [PATCH] time --- all.h | 28 ++++++++++++++++++++++++++++ linux_os.c | 8 -------- mac_os.c | 6 ------ unix_os.c | 21 +++++++++++++++++++++ win32_os.c | 15 +++++++++++++++ 5 files changed, 64 insertions(+), 14 deletions(-) diff --git a/all.h b/all.h index 878b149..30f9251 100644 --- a/all.h +++ b/all.h @@ -611,6 +611,17 @@ typedef struct ThreadContext { LaneCtx lane_ctx; } ThreadContext; +typedef struct HumanTime { + i16 year; + u8 month; + u8 month_i; + u8 date; + u8 hour; + u8 min; + u8 sec; + u8 zone; +} HumanTime; + ///// HARDCODED GLOBALS global const u64 MAX_u64 = 0xffffffffffffffffull; global const i64 MAX_i64 = 9223372036854775807LL; @@ -627,6 +638,19 @@ global const u8 MAX_u8 = 0xff; #ifndef SQRT_3 # define SQRT_3 (1.7320508076) #endif +#ifndef ONE_MILLION +# define ONE_MILLION (1000000) +#endif +#ifndef ONE_THOUSAND +# define ONE_THOUSAND (1000) +#endif +#ifndef MICROSECONDS_PER_SECOND +# define MICROSECONDS_PER_SECOND ONE_MILLION +#endif +#ifndef NANOSECONDS_PER_MICROSECOND +# define NANOSECONDS_PER_MICROSECOND ONE_THOUSAND +#endif + ///// CUSTOM ENTRY POINT /* TODO? @@ -732,8 +756,10 @@ fn void* osMemoryReserve(u64 size); fn void osMemoryCommit(void* memory, u64 size); fn void osMemoryDecommit(void* memory, u64 size); fn void osMemoryRelease(void* memory, u64 size); + fn u64 osTimeMicrosecondsNow(); fn void osSleepMicroseconds(u32 t); +fn HumanTime osHumanTimeFromMicroseconds(u64 micros); // Files fn bool osFileExists(String filename); @@ -808,7 +834,9 @@ typedef struct StringArena { Mutex mutex; } StringArena; +#define stringChunkListAlloc(a, s) allocStringChunkList(a, s) fn StringChunkList allocStringChunkList(StringArena* a, String string); +#define stringChunkListRelease(a, l) releaseStringChunkList(a, l) fn void releaseStringChunkList(StringArena* a, StringChunkList* list); fn String stringChunkToString(Arena* a, StringChunkList list); fn String stringChunkToStringWithPadding(Arena* a, StringChunkList list, u32 extra); diff --git a/linux_os.c b/linux_os.c index b3f6f4b..c02012d 100644 --- a/linux_os.c +++ b/linux_os.c @@ -27,14 +27,6 @@ fn void osBarrierWait(Barrier barrier) { } // Time -fn u64 osTimeMicrosecondsNow() { - struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - return ((u64)ts.tv_sec * 1000000) + ((u64)ts.tv_nsec / 1000000); -} - -#define MICROSECONDS_PER_SECOND 1000000 -#define NANOSECONDS_PER_MICROSECOND 1000 fn void osSleepMicroseconds(u32 t) { struct timespec ts = { t / MICROSECONDS_PER_SECOND, (t % MICROSECONDS_PER_SECOND)*NANOSECONDS_PER_MICROSECOND }; nanosleep(&ts, NULL); diff --git a/mac_os.c b/mac_os.c index d8aea68..624076c 100644 --- a/mac_os.c +++ b/mac_os.c @@ -27,12 +27,6 @@ fn void osBarrierWait(Barrier barrier) { } // Time -fn u64 osTimeMicrosecondsNow() { - struct timespec ts; - clock_gettime(CLOCK_MONOTONIC_RAW, &ts); - return ((u64)ts.tv_sec * 1000000) + ((u64)ts.tv_nsec / 1000000); -} - fn void osSleepMicroseconds(u32 t) { usleep(t); } diff --git a/unix_os.c b/unix_os.c index fd52209..766a005 100644 --- a/unix_os.c +++ b/unix_os.c @@ -273,3 +273,24 @@ fn bool osFileWriteOpenFile(Resulti64 handle, String data) { return true; } +// Time +fn u64 osTimeMicrosecondsNow() { + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + return ((u64)ts.tv_sec * MICROSECONDS_PER_SECOND) + ((u64)ts.tv_nsec / NANOSECONDS_PER_MICROSECOND); +} + +fn HumanTime osHumanTimeFromMicroseconds(u64 micros) { + HumanTime result = {0}; + time_t secs = (time_t)(micros / 1000000ULL); + struct tm t; + localtime_r(&secs, &t); + result.year = 1900 + t.tm_year; + result.month = t.tm_mon + 1; + result.month_i = t.tm_mon; + result.date = t.tm_mday; + result.hour = t.tm_hour; + result.min = t.tm_min; + result.sec = t.tm_sec; + return result; +} diff --git a/win32_os.c b/win32_os.c index 75c7917..499da82 100644 --- a/win32_os.c +++ b/win32_os.c @@ -353,3 +353,18 @@ bool osThreadJoin(Thread handle, u64 endt_us) { } return (wait_result == WAIT_OBJECT_0); } + +fn HumanTime osHumanTimeFromMicroseconds(u64 micros) { + HumanTime result = {0}; + time_t secs = (time_t)(micros / MICROSECONDS_PER_SECOND); + struct tm t; + localtime_s(&t, &secs); + result.year = 1900 + t.tm_year; + result.month = t.tm_mon + 1; + result.month_i = t.tm_mon; + result.date = t.tm_mday + result.hour = t.tm_hour; + result.min = t.tm_min; + result.sec = t.tm_sec; + return result; +}