Skip to main content

A class for converting and manipulating datetime objects based on given timezone offset or name.

Project description

TimeZoneAdapter

A Python library that provides an easy-to-use interface for converting and manipulating datetime objects across different time zones.

PyPI version License: MIT Python Versions

Features

  • Convert between time zones using numeric offset or timezone name
  • Convert to and from UTC
  • Calculate minimum and maximum dates for a specific day
  • Support for both timezone offsets and timezone names (e.g., 'America/Bogota')
  • Type hints support
  • Zero dependencies (except pytz)

Installation

pip install timezone-adapter

Quick Start

from datetime import datetime
from timezone_adapter import TimeZoneAdapter

# Using numeric offset (hours)
tz_adapter = TimeZoneAdapter(-5)  # UTC-5

# Using timezone name
tz_adapter = TimeZoneAdapter('America/Bogota')

# Convert to UTC
local_time = datetime.now()
utc_time = tz_adapter.to_utc(local_time)

# Get range of dates for today
min_date, max_date = tz_adapter.get_min_max_datetime_today()

API Reference

TimeZoneAdapter

__init__(timezone: Union[int, str])

Initialize with either a numeric offset (hours from UTC) or timezone name.

to_utc(date_time: datetime) -> datetime

Convert a local datetime to UTC.

from_utc(date_time: datetime) -> datetime

Convert a UTC datetime to local time.

get_min_max_datetime_today() -> Tuple[datetime, datetime]

Get the minimum and maximum datetime for the current day.

get_min_max_datetime_by_date(date_time: datetime) -> Tuple[datetime, datetime]

Get the minimum and maximum datetime for a specific date.

Development

Setup Development Environment

Clone the repository

git clone https://github.com/miguepoloc/timezone-adapter.git
cd timezone-adapter

Create and activate virtual environment

python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate

Install development dependencies

pip install -e ".[dev]"

Run tests

pytest

Code Quality

We use several tools to ensure code quality:

  • black for code formatting
  • isort for import sorting
  • mypy for type checking
  • flake8 for style guide enforcement

Run all checks with:

pre-commit run --all-files

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Miguel Angel Polo Castañeda - @miguepoloc

Acknowledgments

  • Thanks to the pytz library for timezone support
  • Inspired by the need for simpler timezone handling in Python

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

timezone_adapter-0.1.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

timezone_adapter-0.1.1-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file timezone_adapter-0.1.1.tar.gz.

File metadata

  • Download URL: timezone_adapter-0.1.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.7

File hashes

Hashes for timezone_adapter-0.1.1.tar.gz
Algorithm Hash digest
SHA256 383d50de8364ed5a6aea2d36a7ead8e91c0bb7f079bc4a1d028a678fdb668bb9
MD5 9e8858f110f245a615f220d78e528501
BLAKE2b-256 999b4740784ecf368789b9deb9f537981f5fd174311a7f6c56add93439223a63

See more details on using hashes here.

File details

Details for the file timezone_adapter-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for timezone_adapter-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c078b67dc7ec3aa403bbf73740547727c56db32c752c62afe31d1260e9254c7b
MD5 077fb8a21a480ac91033ff8a831055cd
BLAKE2b-256 962dfbe4f0d48965e43db52192e42229c811075d4aea8ab7bf3bad848e0aeec6

See more details on using hashes here.

Supported by

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