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.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 76b2f2caa0dd8d6b37894a07ff37d099dca51ecdefe4a84429a16e58f4b5d6d6 |
|
MD5 | 7c93b355cd62365261d7d7e761684713 |
|
BLAKE2b-256 | c817709a5165b6799b5e83cad11f78e5bf5df27bc392a17882a6b9b765684efe |