Skip to main content

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 timezone
  • datetime_naive_to_local(dt: datetime.datetime) -> datetime.datetime: converts a naive datetime to the local timezone
  • datetime_naive_to_utc(dt: datetime.datetime) -> datetime.datetime: converts a naive datetime to UTC
  • datetime_remove_tz(dt: datetime.datetime) -> datetime.datetime:: removes the timezone from a datetime
  • datetime_to_new_tz(dt: datetime.datetime, offset) -> datetime.datetime: converts a datetime to a new timezone
  • datetime_tz_to_utc(dt: datetime.datetime) -> datetime.datetime: converts a datetime with a timezone to UTC
  • datetime_utc_to_local(dt: datetime.datetime) -> datetime.datetime: converts a UTC datetime to the local timezone
  • get_local_tz(dt: datetime.datetime) -> datetime.tzinfo: returns the local timezone for a given datetime
  • utc_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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

datetime_tzutils-1.0.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

datetime_tzutils-1.0.1-py2.py3-none-any.whl (3.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file datetime_tzutils-1.0.1.tar.gz.

File metadata

  • Download URL: datetime_tzutils-1.0.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.28.2

File hashes

Hashes for datetime_tzutils-1.0.1.tar.gz
Algorithm Hash digest
SHA256 018d8301d1b3a2b86e849cf891d36f90cf0732642d47cbfe049b0e7c6cb1769f
MD5 b44262ea6005037734f0a284ae45b2de
BLAKE2b-256 2d5f439bd7206f9efe144ddc88a8fbc3d803f1c325834fdfe4ddf2d7c4084a48

See more details on using hashes here.

File details

Details for the file datetime_tzutils-1.0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for datetime_tzutils-1.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a37d502d9f8b5ba139952e892f8f84064a79f3883245b4a5fb6a4f2ee3700ba0
MD5 17509c3e207aa1028d9e1537a99c0763
BLAKE2b-256 432dc323f26a4a051c06e4621feb41f878a7e80d52b2974bc7882d3273e61145

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page