datetime.datetime helper functions for converting to/from UTC and other datetime manipulations
Project description
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.txt
python -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
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
Built Distribution
Hashes for datetime_tzutils-1.0.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a37d502d9f8b5ba139952e892f8f84064a79f3883245b4a5fb6a4f2ee3700ba0 |
|
MD5 | 17509c3e207aa1028d9e1537a99c0763 |
|
BLAKE2b-256 | 432dc323f26a4a051c06e4621feb41f878a7e80d52b2974bc7882d3273e61145 |