-
Notifications
You must be signed in to change notification settings - Fork 33
refactor: replace datetime parsing with utility function for consistency across event handlers #274
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
base: @invertase/fix-timestamp-parsing
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -105,24 +105,11 @@ def on_schedule_wrapped(request: _Request) -> _Response: | |
| schedule_time = _dt.datetime.now(_timezone.utc) | ||
| else: | ||
| try: | ||
| # Try to parse with the stdlib which supports fractional | ||
| # seconds and offsets in Python 3.11+ via fromisoformat. | ||
| # Normalize RFC3339 'Z' to '+00:00' for fromisoformat. | ||
| iso_str = schedule_time_str | ||
| if iso_str.endswith("Z"): | ||
| iso_str = iso_str[:-1] + "+00:00" | ||
| schedule_time = _dt.datetime.fromisoformat(iso_str) | ||
| except ValueError: | ||
| # Fallback to strict parsing without fractional seconds | ||
| try: | ||
| schedule_time = _dt.datetime.strptime( | ||
| schedule_time_str, | ||
| "%Y-%m-%dT%H:%M:%S%z", | ||
| ) | ||
| except ValueError as e: | ||
| # If all parsing fails, log and use current UTC time | ||
| _logging.exception(e) | ||
| schedule_time = _dt.datetime.now(_timezone.utc) | ||
| schedule_time = _util.timestamp_conversion(schedule_time_str) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The refactor to use |
||
| except ValueError as e: | ||
| # If parsing fails, log and use current UTC time. | ||
| _logging.exception(e) | ||
| schedule_time = _dt.datetime.now(_timezone.utc) | ||
| event = ScheduledEvent( | ||
| job_name=request.headers.get("X-CloudScheduler-JobName"), | ||
| schedule_time=schedule_time, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The replacement of manual parsing with
_util.timestamp_conversionintroduces a potential regression for Python versions prior to 3.11. The previous implementation (lines 109-112 in the original code) explicitly handled the 'Z' suffix by replacing it or using a format that worked. However,_util.timestamp_conversionusesstrptimewith the%zdirective for second and microsecond precisions, which does not support the 'Z' suffix in Python < 3.11. Since this library likely supports Python 3.10, this change will cause aValueErrorwhen processing timestamps ending in 'Z'. It is recommended to update the utility function_util.timestamp_conversionto handle the 'Z' suffix for all precisions.