Typing Stubs for tzlocal

5.1.0.1 · active · verified Sat Apr 11

types-tzlocal provides static type annotations (stubs) for the `tzlocal` library, which is used to obtain the local timezone as a `tzinfo` object. The current version, 5.1.0.1, specifically targets `tzlocal` versions up to 5.1. Note that as of `tzlocal` version 5.2, the core library includes its own type annotations, making `types-tzlocal` redundant for newer versions of `tzlocal`. It is maintained as part of the `typeshed` project and receives updates aligned with `tzlocal` releases.

Warnings

Install

Imports

Quickstart

Demonstrates how to retrieve the local timezone `tzinfo` object and create a timezone-aware `datetime` object using `tzlocal`. It also shows how to get the timezone name, with a note about potential failures on some systems.

from datetime import datetime
from tzlocal import get_localzone

# Get the local timezone tzinfo object
local_tz = get_localzone()
print(f"Local timezone: {local_tz}")

# Create a timezone-aware datetime object
now_naive = datetime.now()
now_aware = local_tz.localize(now_naive)
print(f"Timezone-aware datetime: {now_aware}")

# Get just the timezone name (may fail on some Unix systems)
try:
    local_tz_name = get_localzone_name()
    print(f"Local timezone name: {local_tz_name}")
except Exception as e:
    print(f"Could not get local timezone name: {e}")

view raw JSON →