-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(sync): Handle timezone-naive datetimes in sync.toml (#8477) #8487
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
Open
vicheey
wants to merge
9
commits into
develop
Choose a base branch
from
fix-8477
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ee874e4
fix(sync): Handle timezone-naive datetimes in sync.toml (#8477)
vicheey d54391a
Merge branch 'develop' into fix-8477
vicheey e457dff
Merge branch 'develop' into fix-8477
vicheey c59c83d
Merge branch 'develop' into fix-8477
vicheey cd6408c
address comment: add "Z" handler
vicheey 14f4c15
Merge branch 'develop' into fix-8477
vicheey 5496757
Merge branch 'develop' into fix-8477
vicheey 06c7858
Merge branch 'develop' into fix-8477
vicheey 020b7ab
Address PR review comments
vicheey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,6 +65,48 @@ def update_infra_sync_time(self) -> None: | |
| self.latest_infra_sync_time = datetime.now(timezone.utc) | ||
|
|
||
|
|
||
| def _parse_datetime_from_toml(datetime_str: str) -> datetime: | ||
| """ | ||
| Parse datetime string from TOML file and ensure it's timezone-aware UTC. | ||
|
|
||
| Handles three formats: | ||
| 1. With explicit timezone: "2024-05-08T15:16:43+00:00" | ||
| 2. Without timezone: "2024-05-08T15:16:43" | ||
| 3. With Z suffix (Zulu time): "2024-05-08T15:16:43Z" | ||
|
|
||
| Python 3.9/3.10 don't support 'Z' suffix in fromisoformat(), so we convert it. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| datetime_str: str | ||
| ISO format datetime string from TOML file | ||
|
|
||
| Returns | ||
| ------- | ||
| datetime | ||
| Timezone-aware datetime in UTC | ||
|
|
||
| Raises | ||
| ------ | ||
| ValueError | ||
| If datetime_str is not a valid ISO format datetime string | ||
| """ | ||
| try: | ||
| # Handle 'Z' suffix for UTC timezone (Python 3.9/3.10 compatibility) | ||
| if datetime_str.endswith("Z"): | ||
| datetime_str = datetime_str[:-1] + "+00:00" | ||
|
|
||
| parsed_datetime = datetime.fromisoformat(datetime_str) | ||
|
|
||
| # Ensure timezone-aware (handles old sync.toml files without timezone) | ||
| if parsed_datetime.tzinfo is None: | ||
| parsed_datetime = parsed_datetime.replace(tzinfo=timezone.utc) | ||
|
|
||
| return parsed_datetime | ||
| except (ValueError, AttributeError) as e: | ||
| raise ValueError(f"Invalid datetime format in sync.toml: '{datetime_str}'") from e | ||
|
|
||
|
|
||
| def _sync_state_to_toml_document(sync_state: SyncState) -> TOMLDocument: | ||
| """ | ||
| Writes the sync state information to the TOML file. | ||
|
|
@@ -129,9 +171,12 @@ def _toml_document_to_sync_state(toml_document: Dict) -> Optional[SyncState]: | |
| if resource_sync_states_toml_table: | ||
| for resource_id in resource_sync_states_toml_table: | ||
| resource_sync_state_toml_table = resource_sync_states_toml_table.get(resource_id) | ||
| sync_time_str = resource_sync_state_toml_table.get(SYNC_TIME) | ||
vicheey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Parse datetime and ensure it's timezone-aware UTC (consistent with how we write) | ||
| sync_time = _parse_datetime_from_toml(sync_time_str) | ||
| resource_sync_state = ResourceSyncState( | ||
| resource_sync_state_toml_table.get(HASH), | ||
| datetime.fromisoformat(resource_sync_state_toml_table.get(SYNC_TIME)), | ||
| sync_time, | ||
| ) | ||
|
|
||
| # For Nested stack resources, replace "-" with "/" | ||
|
|
@@ -142,9 +187,10 @@ def _toml_document_to_sync_state(toml_document: Dict) -> Optional[SyncState]: | |
| latest_infra_sync_time = None | ||
| if sync_state_toml_table: | ||
| dependency_layer = sync_state_toml_table.get(DEPENDENCY_LAYER) | ||
| latest_infra_sync_time = sync_state_toml_table.get(LATEST_INFRA_SYNC_TIME) | ||
| if latest_infra_sync_time: | ||
| latest_infra_sync_time = datetime.fromisoformat(str(latest_infra_sync_time)) | ||
| latest_infra_sync_time_str = sync_state_toml_table.get(LATEST_INFRA_SYNC_TIME) | ||
vicheey marked this conversation as resolved.
Show resolved
Hide resolved
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. are we sure LATEST_INFRA_SYNC_TIME will always exist? |
||
| if latest_infra_sync_time_str: | ||
| # Parse datetime and ensure it's timezone-aware UTC (consistent with how we write) | ||
| latest_infra_sync_time = _parse_datetime_from_toml(str(latest_infra_sync_time_str)) | ||
| sync_state = SyncState(dependency_layer, resource_sync_states, latest_infra_sync_time) | ||
|
|
||
| return sync_state | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
One thing I learned from my past experience is never write a cutsom function around date time. There will always be a conner case you didn't consider before. If this is a valid usecase let's try to find a public lib that does it.
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.
I am thinking of using epoch time instead of this timezone.