Skip to content

datetime

Utility functions for handling time/timezones.

random_delay(min_delay_sec, max_delay_sec)

Sleep for a random number of seconds in [min_delay_sec, max_delay_sec].

Parameters:

Name Type Description Default
min_delay_sec float

The minimum delay in seconds.

required
max_delay_sec float

The maximum delay in seconds.

required

Raises:

Type Description
ValueError

If min_delay_sec < 0 or max_delay_sec <= min_delay_sec.

Returns:

Type Description
None

None

Examples:

>>> random_delay(1.0, 2.0)

utc_datetime_to_utc_int(dt)

Given a UTC datetime object, return the seconds offset from midnight 01 January, 1970 UTC.

Parameters:

Name Type Description Default
dt datetime

The UTC datetime object.

required

Returns:

Type Description
int

The seconds offset from midnight 01 January, 1970 UTC.

Examples:

>>> utc_datetime_to_utc_int(datetime(2024, 11, 1, 12, 45, 14, tzinfo=UTC))
1730465114

utc_time_now()

Return the current time in UTC.

Returns:

Type Description
datetime

The current time in UTC.

Examples:

>>> utc_time_now()
datetime.datetime(202..., ...)

utc_time_now_plus_delta(delta)

Return the current time in UTC + a timedelta.

Parameters:

Name Type Description Default
delta timedelta

The timedelta to add to the current time.

required

Returns:

Type Description
datetime

The current time in UTC + the timedelta.

Examples:

>>> utc_time_now_plus_delta(timedelta(days=1))
datetime.datetime(202..., ...)