How to manage Timezones and DateTime serialization
Configure application-wide timezones, convert datetimes, and serialize timezone-aware outputs using ZDateTime.
ZCore provides timezone management built on Python's zoneinfo standard library, ensuring consistent UTC storage in databases and formatted timezone-aware outputs for API consumers.
1. Configure the Application Timezone
Set your desired IANA timezone in .env:
# .env
TIMEZONE="America/New_York"
AUTO_CONVERT_TIMEZONE=TrueOr declare it in your Settings subclass:
# config.py
from zcore.config import Settings
class AppSettings(Settings):
TIMEZONE: str = "Europe/London"
AUTO_CONVERT_TIMEZONE: bool = True2. Using Timezone Utilities in Code
Import timezone helpers directly from zcore or zcore.utils.timezone:
from zcore.utils.timezone import now, utc_now, to_app_timezone, format_iso_with_app_timezone
# Get current datetime in configured application timezone
local_time = now()
# Get current datetime in UTC
utc_time = utc_now()
# Convert a database datetime (naive or aware) to the application timezone
converted_time = to_app_timezone(task.created_at)
# Format datetime as ISO 8601 string with application timezone offset
iso_string = format_iso_with_app_timezone(task.created_at)3. Automatic Response Formatting with ZDateTime
Use the ZDateTime type annotation in your Pydantic schemas. When the schema is serialized to JSON, it is automatically converted and formatted to the application's timezone:
# schemas.py
import uuid
from datetime import datetime
from zcore import Zchema
from zcore.utils.timezone import ZDateTime
class TaskResponse(Zchema):
__model__ = "tasks"
id: uuid.UUID
title: str
created_at: ZDateTime
updated_at: ZDateTime | None = NoneTimezone Fallback:
If an invalid or unrecognized timezone string is configured, get_app_timezone() logs a warning and automatically falls back to UTC.
How to configure Database and Logging
Configure asynchronous database engines with DatabaseSettings and customize structured logging with LoggingSettings and file rotation.
How to use CLI commands
Master the ZCore interactive CLI for project scaffolding, domain app generation, environment management, and advanced server orchestration.