Anjay Lite
|
Strongly-typed time utilities used across Anjay Lite. More...
#include <anj/init.h>
#include <inttypes.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
Go to the source code of this file.
Data Structures | |
struct | anj_time_duration_t |
Relative duration of time. More... | |
struct | anj_time_monotonic_t |
Absolute monotonic time (time since boot). More... | |
struct | anj_time_real_t |
Absolute real (calendar) time. More... | |
Macros | |
#define | ANJ_TIME_US_MAX INT64_MAX |
Maximum value for a duration in microseconds. | |
#define | ANJ_TIME_DURATION_AS_STRING_MAX_LEN (sizeof("-9223372036854775808")) |
Maximum length for buffers created by ANJ_TIME_DURATION_AS_STRING and related macros. | |
#define | ANJ_TIME_DURATION_AS_STRING(duration, unit) |
Formats a duration as a string in the given unit. | |
#define | ANJ_TIME_MONOTONIC_AS_STRING(time, unit) |
Formats a monotonic timestamp as a duration string in the given unit. | |
#define | ANJ_TIME_REAL_AS_STRING(time, unit) |
Formats a real timestamp as a duration string in the given unit. | |
Enumerations | |
enum | anj_time_unit_t { ANJ_TIME_UNIT_DAY , ANJ_TIME_UNIT_HOUR , ANJ_TIME_UNIT_MIN , ANJ_TIME_UNIT_S , ANJ_TIME_UNIT_MS , ANJ_TIME_UNIT_US } |
Time unit of a scalar value. More... | |
Functions | |
anj_time_duration_t | anj_time_duration_new (int64_t scalar, anj_time_unit_t unit) |
Creates a duration from an integer scalar in the given unit . | |
anj_time_duration_t | anj_time_duration_fnew (double scalar, anj_time_unit_t unit) |
Creates a duration from a floating-point scalar in the given unit . | |
int64_t | anj_time_duration_to_scalar (anj_time_duration_t duration, anj_time_unit_t unit) |
Converts a duration to an integer scalar in the given unit . | |
double | anj_time_duration_to_fscalar (anj_time_duration_t duration, anj_time_unit_t unit) |
Converts a duration to a floating-point scalar in the given unit . | |
static bool | anj_time_duration_is_valid (anj_time_duration_t duration) |
Checks whether a duration value is valid. | |
static anj_time_duration_t | anj_time_duration_add (const anj_time_duration_t lhs, const anj_time_duration_t rhs) |
Adds two durations. | |
static anj_time_duration_t | anj_time_duration_sub (const anj_time_duration_t lhs, const anj_time_duration_t rhs) |
Subtracts two durations. | |
static anj_time_duration_t | anj_time_duration_mul (const anj_time_duration_t duration, const int32_t factor) |
Multiplies a duration by an integer factor. | |
static anj_time_duration_t | anj_time_duration_fmul (const anj_time_duration_t duration, const double factor) |
Multiplies a duration by a floating-point factor. | |
static anj_time_duration_t | anj_time_duration_div (const anj_time_duration_t duration, const int32_t divisor) |
Divides a duration by an integer divisor. | |
static bool | anj_time_duration_eq (const anj_time_duration_t lhs, const anj_time_duration_t rhs) |
Equality comparison for durations. | |
static bool | anj_time_duration_lt (const anj_time_duration_t lhs, const anj_time_duration_t rhs) |
Less-than comparison for durations. | |
static bool | anj_time_duration_gt (const anj_time_duration_t lhs, const anj_time_duration_t rhs) |
Greater-than comparison for durations. | |
static bool | anj_time_duration_leq (const anj_time_duration_t lhs, const anj_time_duration_t rhs) |
Less-or-equal comparison for durations. | |
static bool | anj_time_duration_geq (const anj_time_duration_t lhs, const anj_time_duration_t rhs) |
Greater-or-equal comparison for durations. | |
const char * | _anj_time_duration_as_string_impl (anj_time_duration_t duration, anj_time_unit_t unit, char(*buffer)[ANJ_TIME_DURATION_AS_STRING_MAX_LEN]) |
Internal helper that renders a duration to a string buffer. | |
static anj_time_monotonic_t | anj_time_monotonic_from_duration (const anj_time_duration_t duration) |
Constructs a monotonic timestamp from a duration since the monotonic epoch. | |
static anj_time_duration_t | anj_time_monotonic_to_duration (const anj_time_monotonic_t time) |
Converts a monotonic timestamp to a duration since the monotonic epoch. | |
static anj_time_monotonic_t | anj_time_monotonic_new (const int64_t scalar, const anj_time_unit_t unit) |
Creates a monotonic timestamp from an integer scalar and unit. | |
static anj_time_monotonic_t | anj_time_monotonic_fnew (const double scalar, const anj_time_unit_t unit) |
Creates a monotonic timestamp from a floating-point scalar and unit. | |
static bool | anj_time_monotonic_is_valid (const anj_time_monotonic_t time) |
Checks whether a monotonic timestamp is valid. | |
static int64_t | anj_time_monotonic_to_scalar (const anj_time_monotonic_t time, const anj_time_unit_t unit) |
Converts a monotonic timestamp to an integer scalar in the given unit. | |
static double | anj_time_monotonic_to_fscalar (const anj_time_monotonic_t time, const anj_time_unit_t unit) |
Converts a monotonic timestamp to a floating-point scalar. | |
static anj_time_monotonic_t | anj_time_monotonic_add (const anj_time_monotonic_t start, const anj_time_duration_t duration) |
Adds a relative duration to a monotonic timestamp. | |
static anj_time_monotonic_t | anj_time_monotonic_sub (const anj_time_monotonic_t start, const anj_time_duration_t duration) |
Subtracts a relative duration from a monotonic timestamp. | |
static anj_time_duration_t | anj_time_monotonic_diff (const anj_time_monotonic_t end, const anj_time_monotonic_t start) |
Computes the difference between two monotonic timestamps. | |
static bool | anj_time_monotonic_lt (const anj_time_monotonic_t lhs, const anj_time_monotonic_t rhs) |
Less-than comparison for monotonic timestamps. | |
static bool | anj_time_monotonic_gt (const anj_time_monotonic_t lhs, const anj_time_monotonic_t rhs) |
Greater-than comparison for monotonic timestamps. | |
static bool | anj_time_monotonic_eq (const anj_time_monotonic_t lhs, const anj_time_monotonic_t rhs) |
Equality comparison for monotonic timestamps. | |
static bool | anj_time_monotonic_leq (const anj_time_monotonic_t lhs, const anj_time_monotonic_t rhs) |
Less-or-equal comparison for monotonic timestamps. | |
static bool | anj_time_monotonic_geq (const anj_time_monotonic_t lhs, const anj_time_monotonic_t rhs) |
Greater-or-equal comparison for monotonic timestamps. | |
static anj_time_real_t | anj_time_real_from_duration (const anj_time_duration_t duration) |
Constructs a real (wall-clock) timestamp from a duration since Unix epoch. | |
static anj_time_duration_t | anj_time_real_to_duration (const anj_time_real_t time) |
Converts a real timestamp to a duration since the Unix epoch. | |
static anj_time_real_t | anj_time_real_new (const int64_t scalar, const anj_time_unit_t unit) |
Creates a real timestamp from an integer scalar and unit. | |
static anj_time_real_t | anj_time_real_fnew (const double scalar, const anj_time_unit_t unit) |
Creates a real timestamp from a floating-point scalar and unit. | |
static int64_t | anj_time_real_to_scalar (const anj_time_real_t time, const anj_time_unit_t unit) |
Converts a real timestamp to an integer scalar in the given unit. | |
static double | anj_time_real_to_fscalar (const anj_time_real_t time, const anj_time_unit_t unit) |
Converts a real timestamp to a floating-point scalar. | |
static bool | anj_time_real_is_valid (const anj_time_real_t time) |
Checks whether a real timestamp is valid. | |
static anj_time_real_t | anj_time_real_add (const anj_time_real_t start, const anj_time_duration_t duration) |
Adds a relative duration to a real timestamp. | |
static anj_time_real_t | anj_time_real_sub (const anj_time_real_t start, const anj_time_duration_t duration) |
Subtracts a relative duration from a real timestamp. | |
static anj_time_duration_t | anj_time_real_diff (const anj_time_real_t end, const anj_time_real_t start) |
Computes the difference between two real timestamps. | |
static bool | anj_time_real_lt (const anj_time_real_t lhs, const anj_time_real_t rhs) |
Less-than comparison for real timestamps. | |
static bool | anj_time_real_gt (const anj_time_real_t lhs, const anj_time_real_t rhs) |
Greater-than comparison for real timestamps. | |
static bool | anj_time_real_eq (const anj_time_real_t lhs, const anj_time_real_t rhs) |
Equality comparison for real timestamps. | |
static bool | anj_time_real_leq (const anj_time_real_t lhs, const anj_time_real_t rhs) |
Less-or-equal comparison for real timestamps. | |
static bool | anj_time_real_geq (const anj_time_real_t lhs, const anj_time_real_t rhs) |
Greater-or-equal comparison for real timestamps. | |
Variables | |
const anj_time_duration_t | ANJ_TIME_DURATION_ZERO |
Zero duration (0 microseconds). | |
const anj_time_real_t | ANJ_TIME_REAL_ZERO |
Real time set to 0 (Unix epoch). | |
const anj_time_monotonic_t | ANJ_TIME_MONOTONIC_ZERO |
Monotonic time set to 0 (monotonic epoch). | |
const anj_time_duration_t | ANJ_TIME_DURATION_INVALID |
Sentinel duration representing “infinity”. | |
const anj_time_real_t | ANJ_TIME_REAL_INVALID |
Sentinel real time representing “infinity”. | |
const anj_time_monotonic_t | ANJ_TIME_MONOTONIC_INVALID |
Sentinel monotonic time representing “infinity”. | |
Strongly-typed time utilities used across Anjay Lite.
The module provides:
#define ANJ_TIME_DURATION_AS_STRING | ( | duration, | |
unit | |||
) |
Formats a duration as a string in the given unit.
The macro allocates a temporary stack buffer and returns a pointer to it, making it convenient for logging:
duration | Duration to format. |
unit | Unit to print the value in. |
const char *
to a NUL-terminated string valid until the end of the full expression (implementation uses a compound literal). #define ANJ_TIME_DURATION_AS_STRING_MAX_LEN (sizeof("-9223372036854775808")) |
Maximum length for buffers created by ANJ_TIME_DURATION_AS_STRING and related macros.
Includes one extra byte for a null terminator.
#define ANJ_TIME_MONOTONIC_AS_STRING | ( | time, | |
unit | |||
) |
Formats a monotonic timestamp as a duration string in the given unit.
Useful for logs that want to show “time since boot” in milliseconds, etc.
time | Monotonic timestamp. |
unit | Unit to print the value in. |
#define ANJ_TIME_REAL_AS_STRING | ( | time, | |
unit | |||
) |
Formats a real timestamp as a duration string in the given unit.
This prints the numeric offset from the Unix epoch (e.g., in seconds or milliseconds). For human-readable calendars, convert externally.
time | Real timestamp. |
unit | Unit to print the value in. |
#define ANJ_TIME_US_MAX INT64_MAX |
Maximum value for a duration in microseconds.
Used as a sentinel to represent an invalid duration.
enum anj_time_unit_t |
Time unit of a scalar value.
Used by conversion helpers to interpret or present time quantities.
const char * _anj_time_duration_as_string_impl | ( | anj_time_duration_t | duration, |
anj_time_unit_t | unit, | ||
char(*) | buffer[ANJ_TIME_DURATION_AS_STRING_MAX_LEN] | ||
) |
Internal helper that renders a duration to a string buffer.
Returns either:
buffer
with the formatted integer scalar in the requested unit,duration
equals ANJ_TIME_DURATION_INVALID, orduration | Duration to print. |
unit | Unit to print in. |
buffer | Pointer to a character array of size ANJ_TIME_DURATION_AS_STRING_MAX_LEN. |
|
inlinestatic |
Adds two durations.
lhs | Left-hand operand. |
rhs | Right-hand operand. |
lhs + rhs
, or ANJ_TIME_DURATION_INVALID if any operand is invalid.
|
inlinestatic |
Divides a duration by an integer divisor.
duration | Duration to divide. |
divisor | Integer divisor (must not be 0). |
divisor
is 0.
|
inlinestatic |
Equality comparison for durations.
|
inlinestatic |
Multiplies a duration by a floating-point factor.
The intermediate result is computed in double and then truncated when converted back to microseconds.
duration | Duration to scale. |
factor | Floating-point factor. |
factor
is NaN/Inf.anj_time_duration_t anj_time_duration_fnew | ( | double | scalar, |
anj_time_unit_t | unit | ||
) |
Creates a duration from a floating-point scalar in the given unit
.
The value is converted to microseconds and truncated toward zero.
scalar | Floating-point value to convert. |
unit | Unit of scalar . |
|
inlinestatic |
Greater-or-equal comparison for durations.
|
inlinestatic |
Greater-than comparison for durations.
|
inlinestatic |
Checks whether a duration value is valid.
duration | Duration to check. |
true
if duration
is neither the invalid sentinel nor otherwise invalid; false
otherwise.
|
inlinestatic |
Less-or-equal comparison for durations.
|
inlinestatic |
Less-than comparison for durations.
|
inlinestatic |
Multiplies a duration by an integer factor.
duration | Duration to scale. |
factor | Integer factor. |
duration
is invalid or factor
is not supported.anj_time_duration_t anj_time_duration_new | ( | int64_t | scalar, |
anj_time_unit_t | unit | ||
) |
Creates a duration from an integer scalar in the given unit
.
scalar | Integer value to convert. |
unit | Unit of scalar . |
|
inlinestatic |
Subtracts two durations.
lhs | Left-hand operand. |
rhs | Right-hand operand. |
lhs - rhs
, or ANJ_TIME_DURATION_INVALID if any operand is invalid.double anj_time_duration_to_fscalar | ( | anj_time_duration_t | duration, |
anj_time_unit_t | unit | ||
) |
Converts a duration to a floating-point scalar in the given unit
.
duration | Duration to convert. |
unit | Target unit. |
unit
. int64_t anj_time_duration_to_scalar | ( | anj_time_duration_t | duration, |
anj_time_unit_t | unit | ||
) |
Converts a duration to an integer scalar in the given unit
.
The value is truncated toward zero.
duration | Duration to convert. |
unit | Target unit. |
unit
.
|
inlinestatic |
Adds a relative duration to a monotonic timestamp.
start | Base monotonic time. |
duration | Duration to add. |
start + duration
.
|
inlinestatic |
Computes the difference between two monotonic timestamps.
end | Later timestamp. |
start | Earlier timestamp. |
end - start
as a duration (may be negative if end < start
).
|
inlinestatic |
Equality comparison for monotonic timestamps.
|
inlinestatic |
Creates a monotonic timestamp from a floating-point scalar and unit.
scalar | Floating-point value. |
unit | Unit of scalar . |
|
inlinestatic |
Constructs a monotonic timestamp from a duration since the monotonic epoch.
duration | Duration since the monotonic epoch. |
duration
after the epoch.
|
inlinestatic |
Greater-or-equal comparison for monotonic timestamps.
|
inlinestatic |
Greater-than comparison for monotonic timestamps.
|
inlinestatic |
Checks whether a monotonic timestamp is valid.
time | Timestamp to check. |
true
if valid; false
otherwise.
|
inlinestatic |
Less-or-equal comparison for monotonic timestamps.
|
inlinestatic |
Less-than comparison for monotonic timestamps.
|
inlinestatic |
Creates a monotonic timestamp from an integer scalar and unit.
scalar | Integer value. |
unit | Unit of scalar . |
|
inlinestatic |
Subtracts a relative duration from a monotonic timestamp.
start | Base monotonic time. |
duration | Duration to subtract. |
start - duration
.
|
inlinestatic |
Converts a monotonic timestamp to a duration since the monotonic epoch.
time | Monotonic timestamp to convert. |
|
inlinestatic |
Converts a monotonic timestamp to a floating-point scalar.
time | Timestamp to convert. |
unit | Target unit. |
|
inlinestatic |
Converts a monotonic timestamp to an integer scalar in the given unit.
time | Timestamp to convert. |
unit | Target unit. |
|
inlinestatic |
Adds a relative duration to a real timestamp.
start | Base real time. |
duration | Duration to add. |
start + duration
.
|
inlinestatic |
Computes the difference between two real timestamps.
end | Later timestamp. |
start | Earlier timestamp. |
end - start
as a duration (may be negative).
|
inlinestatic |
Equality comparison for real timestamps.
|
inlinestatic |
Creates a real timestamp from a floating-point scalar and unit.
scalar | Floating-point value. |
unit | Unit of scalar . |
|
inlinestatic |
Constructs a real (wall-clock) timestamp from a duration since Unix epoch.
duration | Duration since the Unix epoch. |
duration
after the epoch.
|
inlinestatic |
Greater-or-equal comparison for real timestamps.
|
inlinestatic |
Greater-than comparison for real timestamps.
|
inlinestatic |
Checks whether a real timestamp is valid.
time | Timestamp to check. |
true
if valid; false
otherwise.
|
inlinestatic |
Less-or-equal comparison for real timestamps.
|
inlinestatic |
Less-than comparison for real timestamps.
|
inlinestatic |
Creates a real timestamp from an integer scalar and unit.
scalar | Integer value. |
unit | Unit of scalar . |
|
inlinestatic |
Subtracts a relative duration from a real timestamp.
start | Base real time. |
duration | Duration to subtract. |
start - duration
.
|
inlinestatic |
Converts a real timestamp to a duration since the Unix epoch.
time | Real timestamp to convert. |
|
inlinestatic |
Converts a real timestamp to a floating-point scalar.
time | Real timestamp to convert. |
unit | Target unit. |
|
inlinestatic |
Converts a real timestamp to an integer scalar in the given unit.
time | Real timestamp to convert. |
unit | Target unit. |
|
extern |
Sentinel duration representing “infinity”.
|
extern |
Zero duration (0 microseconds).
|
extern |
Sentinel monotonic time representing “infinity”.
|
extern |
Monotonic time set to 0 (monotonic epoch).
|
extern |
Sentinel real time representing “infinity”.
|
extern |
Real time set to 0 (Unix epoch).