Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TimeStamper() now uses TZ-aware objects #709

Merged
merged 4 commits into from
Mar 8, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Only add TZ when potentially useful
hynek committed Mar 8, 2025
commit 9e0e47e344bde50b2ee9130b1bc206fb1a2ba03b
10 changes: 5 additions & 5 deletions src/structlog/processors.py
Original file line number Diff line number Diff line change
@@ -525,7 +525,9 @@ def now() -> datetime.datetime:
else:

def now() -> datetime.datetime:
return datetime.datetime.now().astimezone()
# We don't need the TZ for our own formatting. We add it only for
# user-defined formats later.
return datetime.datetime.now() # noqa: DTZ005

if fmt is None:

@@ -539,9 +541,7 @@ def stamper_unix(event_dict: EventDict) -> EventDict:
if fmt.upper() == "ISO":

def stamper_iso_local(event_dict: EventDict) -> EventDict:
# We remove the timezone offset for backwards-compatibility. If the
# user wants a timezone, they have to set fmt manually.
event_dict[key] = now().isoformat()[:-6]
event_dict[key] = now().isoformat()
return event_dict

def stamper_iso_utc(event_dict: EventDict) -> EventDict:
@@ -554,7 +554,7 @@ def stamper_iso_utc(event_dict: EventDict) -> EventDict:
return stamper_iso_local

def stamper_fmt(event_dict: EventDict) -> EventDict:
event_dict[key] = now().strftime(fmt)
event_dict[key] = now().astimezone().strftime(fmt)

return event_dict