LemonCore. Clock
(lemon_core v0.1.0)
View Source
Time utilities for Lemon.
Provides consistent time handling across the umbrella.
Summary
Functions
Calculate the time elapsed since a timestamp in milliseconds.
Check if a timestamp has expired given a TTL in milliseconds.
Convert milliseconds to a DateTime.
Get the current time in milliseconds.
Get the current time in seconds.
Get the current UTC datetime.
Convert a DateTime to milliseconds.
Functions
@spec elapsed_ms(timestamp_ms :: non_neg_integer()) :: integer()
Calculate the time elapsed since a timestamp in milliseconds.
@spec expired?(timestamp_ms :: non_neg_integer(), ttl_ms :: non_neg_integer()) :: boolean()
Check if a timestamp has expired given a TTL in milliseconds.
Examples
iex> old_ts = LemonCore.Clock.now_ms() - 10_000
iex> LemonCore.Clock.expired?(old_ts, 5_000)
true
@spec from_ms(ms :: non_neg_integer()) :: DateTime.t()
Convert milliseconds to a DateTime.
Examples
iex> dt = LemonCore.Clock.from_ms(1_700_000_000_000)
iex> dt.year
2023
@spec now_ms() :: non_neg_integer()
Get the current time in milliseconds.
@spec now_sec() :: non_neg_integer()
Get the current time in seconds.
@spec now_utc() :: DateTime.t()
Get the current UTC datetime.
@spec to_ms(datetime :: DateTime.t()) :: non_neg_integer()
Convert a DateTime to milliseconds.
Examples
iex> dt = DateTime.from_unix!(1_700_000_000, :second)
iex> LemonCore.Clock.to_ms(dt)
1_700_000_000_000