ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
time.h File Reference

⏱️ High-precision timing utilities using sokol_time.h and uthash More...

Go to the source code of this file.

Data Structures

struct  timer_record
 Individual timer record for a named timing operation. More...
 
struct  adaptive_sleep_config_t
 Configuration for adaptive sleep behavior. More...
 
struct  adaptive_sleep_state_t
 Runtime state for adaptive sleep. More...
 

Macros

#define TIME_ATOMIC_UINT64   atomic_t
 
#define TIME_ATOMIC_UINT64_INIT(val)   {0}
 
#define NS_PER_US   1000.0
 
#define NS_PER_MS   (1000.0 * NS_PER_US)
 
#define NS_PER_SEC   (1000.0 * NS_PER_MS)
 
#define NS_PER_MIN   (60.0 * NS_PER_SEC)
 
#define NS_PER_HOUR   (60.0 * NS_PER_MIN)
 
#define NS_PER_DAY   (24.0 * NS_PER_HOUR)
 
#define NS_PER_YEAR   (365.25 * NS_PER_DAY)
 
#define NS_PER_US_INT   1000ULL
 
#define NS_PER_MS_INT   (1000ULL * NS_PER_US_INT)
 
#define NS_PER_SEC_INT   (1000ULL * NS_PER_MS_INT)
 
#define US_PER_MS_INT   1000ULL
 
#define US_PER_SEC_INT   1000000ULL
 
#define MS_PER_SEC_INT   1000ULL
 
#define SEC_PER_MIN   60ULL
 
#define SEC_PER_HOUR   (SEC_PER_MIN * SEC_PER_MIN)
 
#define SEC_PER_DAY   (24ULL * SEC_PER_HOUR)
 
#define NS_PER_MIN_INT   (SEC_PER_MIN * NS_PER_SEC_INT)
 
#define NS_PER_HOUR_INT   (SEC_PER_HOUR * NS_PER_SEC_INT)
 
#define NS_PER_DAY_INT   (SEC_PER_DAY * NS_PER_SEC_INT)
 
#define START_TIMER(name_fmt, ...)
 Start a timer with formatted name.
 
#define START_TIMER_GLOBAL(name_fmt, ...)
 Start a timer without thread ID (for cross-thread timers)
 
#define STOP_TIMER_GLOBAL(name_fmt, ...)
 Stop a cross-thread timer and return elapsed time.
 
#define STOP_TIMER(name_fmt, ...)
 Stop a timer with formatted name and return elapsed time.
 
#define STOP_TIMER_AND_LOG(log_level, threshold_ns, timer_name, msg_fmt, ...)
 Stop a timer and log the result with a custom message.
 
#define STOP_TIMER_AND_LOG_EVERY(log_level, interval_ns, threshold_ns, timer_name, msg_fmt, ...)
 Stop a timer and log the result with rate limiting.
 

Typedefs

typedef struct timer_record timer_record_t
 Individual timer record for a named timing operation.
 

Functions

uint64_t time_get_ns (void)
 Get current monotonic time in nanoseconds.
 
uint64_t time_get_realtime_ns (void)
 Get current wall-clock (real) time in nanoseconds.
 
void time_sleep_ns (uint64_t ns)
 Sleep for specified nanoseconds.
 
uint64_t time_elapsed_ns (uint64_t start_ns, uint64_t end_ns)
 Calculate elapsed time with wraparound safety.
 
bool timer_system_init (void)
 Initialize the timing system.
 
void timer_system_destroy (void)
 Cleanup the timing system.
 
bool timer_start (const char *name)
 Start a named timer.
 
double timer_stop (const char *name)
 Stop a named timer and return elapsed time.
 
bool timer_is_initialized (void)
 Check if timing system is initialized.
 
int time_pretty (uint64_t nanoseconds, int decimals, char *buffer, size_t buffer_size)
 Format nanoseconds as pretty duration with spaces and configurable precision.
 
int time_pretty_now (int decimals, char *buffer, size_t buffer_size)
 Format current monotonic time as pretty duration.
 
int time_human_readable (int64_t nanoseconds, char *buffer, size_t buffer_size)
 Format nanoseconds as human-readable relative duration (moment.js style)
 
int time_human_readable_unsigned (uint64_t nanoseconds, char *buffer, size_t buffer_size)
 Format elapsed time as human-readable relative duration (unsigned version)
 
int time_human_readable_now (char *buffer, size_t buffer_size)
 Format current monotonic time as human-readable relative duration.
 
int format_uptime_hms (int hours, int minutes, int seconds, char *buffer, size_t buffer_size)
 Format uptime as HH:MM:SS string.
 
void adaptive_sleep_init (adaptive_sleep_state_t *state, const adaptive_sleep_config_t *config)
 Initialize adaptive sleep state with configuration.
 
uint64_t adaptive_sleep_calculate (adaptive_sleep_state_t *state, size_t queue_depth, size_t target_depth)
 Calculate adaptive sleep time based on queue depth.
 
void adaptive_sleep_do (adaptive_sleep_state_t *state, size_t queue_depth, size_t target_depth)
 Calculate sleep time and immediately sleep for that duration.
 
bool time_format_is_valid_strftime (const char *format_str)
 Validate strftime format string against known safe specifiers.
 
int time_format_now (const char *format_str, char *buf, size_t buf_size)
 Format current time using strftime format string.
 
asciichat_error_t time_format_safe (const char *format_str, char *buf, size_t buf_size)
 Safe wrapper for time formatting with validation and error codes.
 
uint64_t time_timespec_to_ns (const struct timespec *ts)
 Convert struct timespec to nanoseconds.
 
void time_ns_to_timespec (uint64_t ns, struct timespec *ts)
 Convert nanoseconds to struct timespec.
 

Detailed Description

⏱️ High-precision timing utilities using sokol_time.h and uthash

Definition in file time.h.