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

TimeStamper() now uses TZ-aware objects #709

merged 4 commits into from
Mar 8, 2025

Conversation

hynek
Copy link
Owner

@hynek hynek commented Mar 8, 2025

The default output doesn't change but manual formatting allows for TZ data now.

Fixes #703

cc @andrei-korshikov

hynek added 2 commits March 8, 2025 13:02
The default output doesn't change but manual formatting allows for TZ
data now.

Fixes #703
event_dict[key] = now().isoformat()
# 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().rsplit("+", 1)[0]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Counterexample:

>>> datetime.datetime.now().astimezone(datetime.timezone(datetime.timedelta(hours=-5))).isoformat().rsplit("+", 1)[0]
'2025-03-08T09:55:44.415778-05:00'

datetime.isoformat() states that its ISO time zone designator format is +HH:MM[:SS[.ffffff]], but right below they give us a sample with negative offset—'2009-11-27T00:00:00.000100-06:39'.

I.e. their + in +HH:MM is actually "plus or minus". Negative offsets are totally valid by ISO 8601 / RFC 3339 anyway.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bleh true. great, according to the docstring, the TZ is always +zz:zz so before we start imitating using strftime, just a [:-6]?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My proposal: datetime.datetime.now().astimezone().replace(tzinfo=None).isoformat(),
i.e. event_dict[key] = now().replace(tzinfo=None).isoformat()

datetime.astimezone() reads: "If you merely want to remove the timezone object from an aware datetime dt without conversion of date and time data, use dt.replace(tzinfo=None)." I think it is exactly our case: we want to convert aware time object back to naive, i.e. remove tzinfo.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait, this is stupid. with either approach, I'm adding and removing the timezone instead of adding it only when necessary. i.e. in stamper_fmt. behold the genius who finally read his own code: 9e0e47e

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, isoformat() uses _tzstr() which in turn uses _format_offset(), which gives us sing and possibly seconds and microseconds.

I can't imagine real worlds applications for TZ offset with seconds and its fractions (some crazy astronomy stuff??), but is valid per current standards, so just [:-6] is not enough because time zone designator has variable length.

Yes, docstrings for these functions are confusing indeed, they repeatedly use +hh:mm which really is just one possible option...

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we posted at almost the same time, check back my comment before. I think I solved it pretty much perfectly.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I do agree. I also tested it right now and it works as intended. Thanks to both of us:D

@hynek hynek force-pushed the stamper-tz-aware branch from dd3cb86 to 82173f7 Compare March 8, 2025 16:28
@hynek hynek force-pushed the stamper-tz-aware branch from 82173f7 to 9e0e47e Compare March 8, 2025 16:29
@hynek hynek merged commit 1600b0f into main Mar 8, 2025
19 checks passed
@hynek hynek deleted the stamper-tz-aware branch March 8, 2025 17:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use aware time object in processors.TimeStamper()
2 participants