vayu.time_utils¶
time_utils
¶
TZ
module-attribute
¶
Default timezone used by module-level helpers.
Resolved once at import time from the TZ environment variable if set,
otherwise from the system's local timezone.
TimeWindow
dataclass
¶
Bases: Interval
An Interval over datetimes/dates, with convenience constructors.
Inherits the full interval algebra (intersects, intersection,
union, & / |, containment, shifting). Both endpoints must be the
same type: both date or both datetime. For tz-aware datetimes, both
must have tzinfo or both must be naive.
Attributes:
| Name | Type | Description |
|---|---|---|
start |
Union[datetime, date, None]
|
Lower bound. Defaults to the Unix epoch when left unset. |
end |
Union[datetime, date, None]
|
Upper bound. Defaults to |
ahead
staticmethod
¶
ahead(
*,
t: datetime = None,
duration: timedelta = None,
days=0,
seconds=0,
microseconds=0,
milliseconds=0,
minutes=0,
hours=0,
weeks=0
) -> TimeWindow
Return the window [t, t + duration] (looking forward from t).
Pass either duration directly or individual timedelta kwargs
(hours=..., minutes=..., etc.). t defaults to time_now().
Source code in vayu/time_utils.py
behind
staticmethod
¶
behind(
*,
t: datetime = None,
duration: timedelta = None,
days=0,
seconds=0,
microseconds=0,
milliseconds=0,
minutes=0,
hours=0,
weeks=0
) -> TimeWindow
Return the window [t - duration, t] (looking back from t).
Pass either duration directly or individual timedelta kwargs.
t defaults to time_now().
Source code in vayu/time_utils.py
around
staticmethod
¶
around(
*,
t: datetime = None,
duration: timedelta = None,
days=0,
seconds=0,
microseconds=0,
milliseconds=0,
minutes=0,
hours=0,
weeks=0
)
Return [t - duration, t + duration] (symmetric window around t).
Pass either duration directly or individual timedelta kwargs.
t defaults to time_now().
Source code in vayu/time_utils.py
from_date
staticmethod
¶
from_date(
year: int, month: int, day: int, tz=None
) -> TimeWindow
Return the window spanning a full calendar day (midnight → end of day).
tz defaults to UTC.
Source code in vayu/time_utils.py
from_timestamp
staticmethod
¶
from_timestamp(
start_ts: [int, float], end_ts: [int, float], tz=None
) -> TimeWindow
Return the window built from two Unix timestamps.
Each timestamp may be seconds or milliseconds (see from_timestamp).
Source code in vayu/time_utils.py
epoch_time
¶
time_now
¶
Return the current time as a tz-aware datetime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
with_ms
|
bool
|
Keep microseconds. Default is to truncate to whole seconds. |
False
|
local
|
bool
|
If True, use the module |
True
|
Source code in vayu/time_utils.py
min_time
¶
Return the earliest datetime on the given date (00:00:00). UTC by default.
max_time
¶
Return the latest datetime on the given date (23:59:59.999999). UTC by default.
ts_ms
¶
utc_datetime
¶
utc_datetime(
year: int,
month: int,
day: int,
hour: int = 0,
minute: int = 0,
second: int = 0,
microsecond: int = 0,
) -> datetime
Construct a UTC datetime. Convenience wrapper over datetime(..., tzinfo=UTC).
Source code in vayu/time_utils.py
local_datetime
¶
local_datetime(
year: int,
month: int,
day: int,
hour: int = 0,
minute: int = 0,
second: int = 0,
microsecond: int = 0,
tz=TZ,
) -> datetime
Construct a datetime in the module TZ (or the given tz).
Source code in vayu/time_utils.py
from_timestamp
¶
from_timestamp(ts: float, tz=TZ) -> datetime
Convert a Unix timestamp to a tz-aware datetime.
Timestamps greater than 1e12 are treated as milliseconds (divided by 1000);
smaller values are treated as seconds. This lets you pass either unit without
worrying about the difference.
Source code in vayu/time_utils.py
from_z_string
¶
timeit
¶
Decorator that prints how long the wrapped function took.
Useful for quick scripts. For library-level instrumentation, wire your own
logging around the call instead — this helper uses print.
Source code in vayu/time_utils.py
to_human_readable_time
¶
Format a duration as a compact human string (e.g. 1h2m3s).
Accepts a timedelta or a number of seconds. Returns "0s" for zero
and "invalid-time" for negative values.