aiozoneinfo

0.2.3 · active · verified Thu Apr 16

aiozoneinfo is an asyncio-compatible Python library that provides tools to asynchronously fetch and access zoneinfo timezone data. It integrates seamlessly with asynchronous applications requiring up-to-date timezone information. The current version is 0.2.3, with a release cadence focused on minor fixes and improvements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `ZoneInfo` client and asynchronously fetch timezone data using `async_get_timezone`. It also includes basic error handling for invalid timezone names.

import asyncio
from aiozoneinfo import ZoneInfo

async def get_timezone_data(timezone_name: str):
    """Fetches timezone data asynchronously."""
    zoneinfo_fetcher = ZoneInfo()
    try:
        tz = await zoneinfo_fetcher.async_get_timezone(timezone_name)
        print(f"Fetched timezone for {timezone_name}: {tz}")
        # You can now use 'tz' with datetime objects:
        # import datetime
        # dt_utc = datetime.datetime.now(datetime.timezone.utc)
        # dt_local = dt_utc.astimezone(tz)
        # print(f"Current time in {timezone_name}: {dt_local}")
    except Exception as e:
        print(f"Error fetching timezone {timezone_name}: {e}")

async def main():
    await get_timezone_data("America/New_York")
    await get_timezone_data("Europe/London")
    await get_timezone_data("Invalid/Timezone")

if __name__ == "__main__":
    asyncio.run(main())

view raw JSON →