to-utc
Timezones are error-prone. The safest approach is to store and process datetimes in UTC consistently.
to_utc: converts any datetime-like value to a UTC-awaredatetime.datetimeobjectto_naive_utc: converts any datetime-like value to a naivedatetime.datetimeobject (assumes UTC)now: returns the current UTC time as a UTC-awaredatetime.datetimeobjectto_timedelta: converts any timedelta-like value todatetime.timedelta
def to_utc(value: Union[
datetime,
str,
int,
float,
date,
"pendulum.DateTime", # if pendulum installed
"pd.Timestamp", # if pandas installed
"np.datetime64", # if numpy installed
"arrow.Arrow", # if arrow installed
]) -> datetime:
"""
Converts any datetime-like value to a UTC-aware datetime.
- Numbers are handled as timestamps (try seconds → milliseconds → microseconds)
- Strings are converted as follows:
1. Try fixed patterns first:
- %Y-%m-%d
- %Y-%m-%d %H:%M:%S
- %Y-%m-%dT%H:%M:%S
- %Y-%m-%dT%H:%M:%SZ
- %Y-%m-%d %H:%M:%S.%f
- %Y-%m-%dT%H:%M:%S.%fZ
- %Y%m%d
- %Y%m%d%H%M%S
- %Y-%m-%d %H:%M:%S%z
- %Y-%m-%dT%H:%M:%S%z
2. Fallback to dateutil.parser.parse
- Common datetime-like objects are converted as expected (datetime, date, pd.Timestamp, ...)
- Naive datetimes are assumed to be UTC
- Aware datetimes are converted to UTC
"""
pass
def to_naive_utc(value: DatetimeLike) -> datetime:
"""
Converts any datetime-like value to a naive UTC datetime.
This is a convenience wrapper around to_utc() that strips timezone information.
"""
pass
def now() -> datetime:
"""
Returns the current UTC time as a UTC-aware datetime.
"""
pass
...
def to_timedelta(value: Union[
list,
int,
float,
str,
timedelta,
"pd.Timedelta", # if pandas installed
"np.timedelta64", # if numpy installed
"pendulum.Duration", # if pendulum installed
]) -> timedelta:
"""
- Numbers: interpreted as seconds.
- Strings are converted as follows:
1. Parse compact format (\d+d\d+h\d+m\d+s, e.g. "3d5h12m40s")
2. Parse word form ("2 hours 5 minutes", e.g. "1 day 3 hours")
- Common timedelta-like objects converted appropriately (timedelta, pd.Timedelta, ...)
"""
pass
...
from to_utc import to_utc, to_naive_utc, now, to_timedelta
# Convert to UTC-aware datetime
to_utc("2024-01-01T15:00:00+03:00") # -> datetime(2024, 1, 1, 12, 0, 0, tzinfo=timezone.utc)
to_utc(1754942420) # -> datetime(2025, 08, 11, 20, 0, 20, tzinfo=timezone.utc)
# Convert to naive UTC datetime
to_naive_utc("2024-01-01T15:00:00+03:00") # -> datetime(2024, 1, 1, 12, 0, 0)
to_naive_utc(1754942420) # -> datetime(2025, 08, 11, 20, 0, 20)
# Get current UTC time
now() # -> datetime(2025, 11, 10, 14, 30, 45, 123456, tzinfo=timezone.utc)
# Convert to timedelta
to_timedelta(120) # -> timedelta(minutes=2)
to_timedelta("1h30m15s") # -> timedelta(hours=1, minutes=30, seconds=15)
Metadata
Release files for to-utc 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| to_utc-0.3.0.tar.gz | 93.0 kB | Without provenance | Details for to_utc-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| to_utc-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for to_utc-0.3.0-py3-none-any.whl |
Total release size: 101.1 kB
Release files / to_utc-0.3.0.tar.gz
| Download URL | to_utc-0.3.0.tar.gz |
|---|---|
| Size | 93.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d0a9d77c0d5b5bdb4b102ad49715cbd9a6fe271535f4d1f2913775e14f59a8bc
|
|
BLAKE2b-256 checksum How to use checksums |
3831204b1118f2e10228e9ecd1625cb8dfcc20252664624c7ce1b41a11b93a26
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.2
|
Release files / to_utc-0.3.0-py3-none-any.whl
| Download URL | to_utc-0.3.0-py3-none-any.whl |
|---|---|
| Size | 8.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8fd3f21e2e27593049d6486d3b1481a1563d8f4e65100b5d0f9ef5dbb9e472be
|
|
BLAKE2b-256 checksum How to use checksums |
29b207002a5b83b4ee42ed50aa595a7335beb9a2cd9d8092afd07d0ab4fc0fa5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.2
|
Security for to-utc 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.