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.
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:
blackfor code formattingisortfor import sortingmypyfor type checkingflake8for style guide enforcement
Run all checks with:
pre-commit run --all-files
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - 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
pytzlibrary for timezone support - Inspired by the need for simpler timezone handling in Python
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
383d50de8364ed5a6aea2d36a7ead8e91c0bb7f079bc4a1d028a678fdb668bb9
|
|
| MD5 |
9e8858f110f245a615f220d78e528501
|
|
| BLAKE2b-256 |
999b4740784ecf368789b9deb9f537981f5fd174311a7f6c56add93439223a63
|
File details
Details for the file timezone_adapter-0.1.1-py3-none-any.whl.
File metadata
- Download URL: timezone_adapter-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c078b67dc7ec3aa403bbf73740547727c56db32c752c62afe31d1260e9254c7b
|
|
| MD5 |
077fb8a21a480ac91033ff8a831055cd
|
|
| BLAKE2b-256 |
962dfbe4f0d48965e43db52192e42229c811075d4aea8ab7bf3bad848e0aeec6
|