ClockRealNs
Description
Current value of the real-time (wall) clock, in nanoseconds since the Unix epoch (1970-01-01 00:00:00 UTC).
Unlike ClockMonoNs, this clock is anchored to the civil epoch and CAN jump – it tracks NTP slew and settimeofday, so two readings may decrease. Use it for timestamps and as the base for civil/local time, never for measuring elapsed durations (use ClockMonoNs for that).
Thread-safety: safe to call concurrently; reads a kernel/commpage time source and keeps no shared state of its own.
Success
Returns a UTC nanosecond timestamp.
Failure
Aborts via LOG_FATAL if the platform clock query fails, the same fail-fast contract as ClockMonoNs.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Clock.c:75:
}
u64 ClockRealNs(void) {
#if PLATFORM_WINDOWS
FILETIME ft;- In
Clock.c:87:
clock_timespec ts;
if (!clock_gettime || clock_gettime(CLOCK_REALTIME_ID, &ts) != 0)
LOG_FATAL("ClockRealNs: clock_gettime failed");
return (u64)ts.tv_sec * 1000000000ull + (u64)ts.tv_nsec;
#elif FEATURE_DIRECT_SYSCALL- In
Clock.c:93:
long ret = direct_sys2(MISRA_SYS_clock_gettime, (long)CLOCK_REALTIME_ID, (long)(u64)&ts);
if (ret < 0)
LOG_FATAL("ClockRealNs: clock_gettime syscall failed");
return (u64)ts.tv_sec * 1000000000ull + (u64)ts.tv_nsec;
#else- In
Clock.c:99:
clock_timespec ts;
if (!clock_gettime || clock_gettime(CLOCK_REALTIME_ID, &ts) != 0)
LOG_FATAL("ClockRealNs: clock_gettime unavailable");
return (u64)ts.tv_sec * 1000000000ull + (u64)ts.tv_nsec;
#endif- In
Clock.c:105:
DateTime ClockUtc(void) {
return DateTimeFromUnixNs(ClockRealNs(), 0);
}- In
Clock.c:109:
DateTime ClockLocal(void) {
u64 ns = ClockRealNs();
#if FEATURE_FILE
DefaultAllocator alloc = DefaultAllocatorInit();- In
Clock.c:14:
// recent wall-clock timestamp (well past 2023).
static bool test_real_is_recent_wall_time(void) {
u64 sec = ClockRealNs() / NS_PER_SEC;
return sec > 1700000000ull; // 2023-11-14
}- In
Clock.c:21:
static bool test_real_differs_from_tick(void) {
u64 a = ClockTick();
u64 b = ClockRealNs();
return a != 0 && b != 0;
}
Last updated on