Install & Compatibility
Where this runs
tested against v5.1.0.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.9MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.5s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
get_localzone
✓ from tzlocal import get_localzone
✗ from tzlocal-stubs import get_localzone
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}")
Debug
Known issues
breakingWith tzlocal version 5.0, the `pytz_deprecation_shim` was removed. This means `tzlocal.get_localzone()` now exclusively returns `zoneinfo` objects, similar to version 3.0. If your application relies on `pytz` objects, you must remain on `tzlocal` version 4.x or adapt your code to `zoneinfo`'s API.fixEither pin 'tzlocal<5' if `pytz` compatibility is essential, or update your code to work with `zoneinfo.ZoneInfo` objects (standard in Python 3.9+).
affects: tzlocal >= 5.0
gotchaSince `tzlocal` version 5.2, the `tzlocal` library itself bundles its type annotations (stubs). If you are using `tzlocal` 5.2 or newer, `types-tzlocal` becomes redundant and should be uninstalled to avoid potential conflicts or unnecessary package overhead.fixRun `pip uninstall types-tzlocal` if your `tzlocal` version is 5.2 or higher.
affects: tzlocal >= 5.2
gotchaOn some Unix-like systems, if the timezone name is not properly configured (e.g., in `/etc/timezone` or via symlinks), `tzlocal.get_localzone_name()` might raise an error, while `tzlocal.get_localzone()` would still successfully return a `tzinfo` object. Unconfigured systems might default to UTC.fixEnsure your operating system's timezone configuration is correctly set up. If only the `tzinfo` object is needed, prefer `get_localzone()` over `get_localzone_name()`.
affects: All versions of tzlocal
deprecatedThe `pytz_deprecation_shim` was introduced in `tzlocal` 4.0 to restore partial `pytz` compatibility after `tzlocal` 3.0 switched to `zoneinfo` objects. This shim was then removed in `tzlocal` 5.0. Relying on it for `pytz` compatibility in `tzlocal` 4.x is a temporary measure and should be migrated away from.fixMigrate to using `zoneinfo.ZoneInfo` objects (native in Python 3.9+) or other modern timezone handling, or explicitly manage `pytz` directly if specific `pytz` features are required.
affects: tzlocal 4.x (usage of shim)
Upgrade
Version history
5.1.0.1latest on PyPI · released Oct 24, 2023
Audit
Dependencies
tzlocalrequiredThis package provides type stubs for the 'tzlocal' runtime library. 'tzlocal' itself requires 'tzdata'.