datetime_tzutils
What is datetime_tzutils
A handful of small utility methods I find useful for dealing with datetime.datetime objects and timezones. Some of these are just a couple of lines but they're easy to get wrong so I find it useful to package them into a tested module. I also find it makes code more readable and less repetitive.
datetime_tzutils is a pure Python module with no dependencies.
Includes:
datetime_has_tz(dt: datetime.datetime) -> bool: returns True if the datetime has a timezonedatetime_naive_to_local(dt: datetime.datetime) -> datetime.datetime: converts a naive datetime to the local timezonedatetime_naive_to_utc(dt: datetime.datetime) -> datetime.datetime: converts a naive datetime to UTCdatetime_remove_tz(dt: datetime.datetime) -> datetime.datetime:: removes the timezone from a datetimedatetime_to_new_tz(dt: datetime.datetime, offset) -> datetime.datetime: converts a datetime to a new timezonedatetime_tz_to_utc(dt: datetime.datetime) -> datetime.datetime: converts a datetime with a timezone to UTCdatetime_utc_to_local(dt: datetime.datetime) -> datetime.datetime: converts a UTC datetime to the local timezoneget_local_tz(dt: datetime.datetime) -> datetime.tzinfo: returns the local timezone for a given datetimeutc_offset_seconds(dt: datetime.datetime) -> int: returns the UTC offset in seconds for a given datetime
Installation
pip install datetime_tzutils
Synopsis
>>> import datetime_tzutils
>>> import datetime
>>> dt = datetime.datetime(2019,12,1)
>>> datetime_tzutils.get_local_tz(dt)
datetime.timezone(datetime.timedelta(days=-1, seconds=57600), 'PST')
>>> datetime_tzutils.datetime_has_tz(dt)
False
>>> dt = datetime_tzutils.datetime_naive_to_local(dt)
>>> dt
datetime.datetime(2019, 12, 1, 0, 0, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=57600), 'PST'))
>>> datetime_tzutils.datetime_has_tz(dt)
True
>>> utc = datetime_tzutils.datetime_tz_to_utc(dt)
>>> utc
datetime.datetime(2019, 12, 1, 8, 0, tzinfo=datetime.timezone.utc)
>>> dt = datetime_tzutils.datetime_remove_tz(dt)
>>> dt
datetime.datetime(2019, 12, 1, 0, 0)
>>> utc = datetime_tzutils.datetime_naive_to_utc(dt)
>>> utc
datetime.datetime(2019, 12, 1, 0, 0, tzinfo=datetime.timezone.utc)
>>> local = datetime_tzutils.datetime_utc_to_local(utc)
>>> local
datetime.datetime(2019, 11, 30, 16, 0, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=57600), 'PST'))
>>>
Source Code
The source code is available on GitHub
Testing
datetime_tzutils has been tested with Python 3.9, 3.10, and 3.11 on Linux, macOS, and Windows.
100% test coverage with pytest:
pip install -r requirements_dev.txtpython -m pytest --cov=datetime_tzutils --cov-report=term-missing
Contributing
Contributions are welcome. Please open an issue or submit a pull request.
The tests are written with pytest and require a couple of extra packages. Install them with:
pip install -r requirements_dev.txt
License
MIT License
Metadata
Release files for datetime_tzutils 1.0.1
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 | |
|---|---|---|---|---|
| datetime_tzutils-1.0.1.tar.gz | 5.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| datetime_tzutils-1.0.1-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 9.0 kB
Release files / datetime_tzutils-1.0.1.tar.gz
| Download URL | datetime_tzutils-1.0.1.tar.gz |
|---|---|
| Size | 5.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
018d8301d1b3a2b86e849cf891d36f90cf0732642d47cbfe049b0e7c6cb1769f
|
|
BLAKE2b-256 checksum How to use checksums |
2d5f439bd7206f9efe144ddc88a8fbc3d803f1c325834fdfe4ddf2d7c4084a48
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.28.2
|
Release files / datetime_tzutils-1.0.1-py2.py3-none-any.whl
| Download URL | datetime_tzutils-1.0.1-py2.py3-none-any.whl |
|---|---|
| Size | 3.9 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
a37d502d9f8b5ba139952e892f8f84064a79f3883245b4a5fb6a4f2ee3700ba0
|
|
BLAKE2b-256 checksum How to use checksums |
432dc323f26a4a051c06e4621feb41f878a7e80d52b2974bc7882d3273e61145
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.28.2
|
Security for datetime_tzutils 1.0.1
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.