SD_EVENT_NOW(3)
π
SD_EVENT_NOW(3) β the sd_event_now glow-up, bestie edition
okay so real quick, this is the sd_event_now page from the sd-event girlies, part of the whole systemd situation. pull up a chair, we're learning about timestamps π
β¨ NAME β¨
sd_event_now β grab the timestamp of the current event loop iteration, no cap π°οΈ
π SYNOPSIS π
#include <systemd/sd-event.h>
int sd_event_now(sd_event *event, clockid_t clock, uint64_t *ret);
π¦ DESCRIPTION π¦
so sd_event_now() hands you back the time when the most recent event loop iteration began, okay? the timestamp is taken right after we come back from the event sleep, and before any event sources get dispatched. so it's like, the moment the loop woke up, before anybody did anything π
the event parameter is which event loop object you wanna pull the timestamp from. the clock parameter is which clock you want the timestamp for, and it has to be one of:
CLOCK_REALTIME(or, equivalently,CLOCK_REALTIME_ALARM)CLOCK_MONOTONICCLOCK_BOOTTIME(or, equivalently,CLOCK_BOOTTIME_ALARM)
see clock_gettime(2) if you want the whole lore on the various clocks, iykyk π°οΈβ¨
the retrieved timestamp lands in the ret parameter, measured in ΞΌs since the clock's epoch. yes, microseconds, not millis, not nanos, ΞΌs π«Ά
and here's the iconic plot twist: if you call this function before the first event loop iteration has even run, you just get the current time back, as reported by clock_gettime(). to tell that case apart from a regular invocation, check the return value: it'll be positive in that before-anything-happened case, and zero when the timestamp you got actually refers to a real event loop iteration. main character distinction energy β
π₯ RETURN VALUE π₯
- if the first event loop iteration hasn't run yet,
sd_event_now()writes the current time toretand returns a positive return value. - otherwise, it writes the requested timestamp to
retand returns 0. slay. - on failure?? it returns a negative
errno-style error code. sad but honest π
π Errors
the returned values may be telling you about one of these problems:
-EINVALβ you passed an invalid parameter. check ur args bestie.-EOPNOTSUPPβ that clock type isn't supported. not every clock can sit with us π-ECHILDβ the event loop object was created in a different process, library, or module instance. it's not your object, it's somebody else's, and that's not gonna fly.
πΈ NOTES πΈ
the functions described here are available as a shared library, which you can compile against and link to with the libsystemd pkg-config(1) file. standard systemd girlie setup π
all functions listed here are thread-agnostic, and only a single thread may operate on a given object at any given time. different threads can access the same object at different times, and multiple independent objects can be used from different threads in parallel. so like, share the object, don't share the moment β¨
β οΈ okay this part matters: the code described here usesgetenv(3), which is declared to be not multi-thread-safe. that means any code calling the functions described here must not callsetenv(3)from a parallel thread. the recommendation is to only callsetenv()from an early phase of the program, when no other threads have been started yet. do it before the group chat gets loud, fr fr.
π HISTORY π
sd_event_now() was added in version 229. an icon from the start π
π SEE ALSO π
systemd(1), sd-event(3), sd_event_new(3), sd_event_add_time(3), clock_gettime(2)
and that's the page!! brought to you by systemd 261.3 β mwah, go forth and timestamp responsibly ππ«Άπ
π¦ where this page comes from
- ubuntu stonking: libsystemd-dev 261.3-0ubuntu3 Β· LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms