Convert any datetime-like object in Python to UTC-aware datetime
Project description
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)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
to_utc-0.3.0.tar.gz
(93.0 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file to_utc-0.3.0.tar.gz.
File metadata
- Download URL: to_utc-0.3.0.tar.gz
- Upload date:
- Size: 93.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d0a9d77c0d5b5bdb4b102ad49715cbd9a6fe271535f4d1f2913775e14f59a8bc
|
|
| MD5 |
75bf47c695dba0a1baebda788bf1b44f
|
|
| BLAKE2b-256 |
3831204b1118f2e10228e9ecd1625cb8dfcc20252664624c7ce1b41a11b93a26
|
File details
Details for the file to_utc-0.3.0-py3-none-any.whl.
File metadata
- Download URL: to_utc-0.3.0-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8fd3f21e2e27593049d6486d3b1481a1563d8f4e65100b5d0f9ef5dbb9e472be
|
|
| MD5 |
3be26ed85414f9ad140b7a253374b6e0
|
|
| BLAKE2b-256 |
29b207002a5b83b4ee42ed50aa595a7335beb9a2cd9d8092afd07d0ab4fc0fa5
|