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_MONOTONIC
  • CLOCK_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 to ret and returns a positive return value.
  • otherwise, it writes the requested timestamp to ret and 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 uses getenv(3), which is declared to be not multi-thread-safe. that means any code calling the functions described here must not call setenv(3) from a parallel thread. the recommendation is to only call setenv() 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