-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(calendar): pass timeZone to events.list to fix all-day event date shift #661
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
jorj-pineda
wants to merge
4
commits into
googleworkspace:main
Choose a base branch
from
jorj-pineda:fix/579-allday-event-date-shift
base: main
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.
+102
−12
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@googleworkspace/cli": patch | ||
| --- | ||
|
|
||
| Fix all-day calendar events showing incorrect dates in `+agenda` by passing the user's timezone to the Calendar API |
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 |
|---|---|---|
|
|
@@ -206,6 +206,49 @@ TIPS: | |
| }) | ||
| } | ||
| } | ||
| /// Extract start/end times from a Google Calendar event. | ||
| /// | ||
| /// All-day events use the `date` field (e.g. `"2026-03-23"`), while timed | ||
| /// events use `dateTime` (RFC 3339). We prefer `date` when present so that | ||
| /// all-day dates are never shifted by a timezone offset. | ||
| /// | ||
| /// Returns `(start, end, all_day)`. | ||
| fn extract_event_times(event: &Value) -> (String, String, bool) { | ||
| let start_obj = event.get("start"); | ||
| let end_obj = event.get("end"); | ||
|
|
||
| // All-day events carry a `date` field; prefer it over `dateTime` when present. | ||
| let all_day = start_obj | ||
| .map(|s| s.get("date").is_some()) | ||
| .unwrap_or(false); | ||
|
|
||
| let start = start_obj | ||
| .and_then(|s| { | ||
| if all_day { | ||
| s.get("date") | ||
| } else { | ||
| s.get("dateTime").or_else(|| s.get("date")) | ||
| } | ||
| }) | ||
| .and_then(|v| v.as_str()) | ||
| .unwrap_or("") | ||
| .to_string(); | ||
|
|
||
| let end = end_obj | ||
| .and_then(|s| { | ||
| if all_day { | ||
| s.get("date") | ||
| } else { | ||
| s.get("dateTime").or_else(|| s.get("date")) | ||
| } | ||
| }) | ||
| .and_then(|v| v.as_str()) | ||
| .unwrap_or("") | ||
| .to_string(); | ||
|
|
||
| (start, end, all_day) | ||
| } | ||
|
|
||
| async fn handle_agenda(matches: &ArgMatches) -> Result<(), GwsError> { | ||
| let cal_scope = "https://www.googleapis.com/auth/calendar.readonly"; | ||
| let token = auth::get_token(&[cal_scope]) | ||
|
|
@@ -254,6 +297,7 @@ async fn handle_agenda(matches: &ArgMatches) -> Result<(), GwsError> { | |
|
|
||
| let time_min = time_min_dt.to_rfc3339(); | ||
| let time_max = time_max_dt.to_rfc3339(); | ||
| let tz_name = tz.to_string(); | ||
|
|
||
| // client already built above for timezone resolution | ||
| let calendar_filter = matches.get_one::<String>("calendar"); | ||
|
|
@@ -325,6 +369,7 @@ async fn handle_agenda(matches: &ArgMatches) -> Result<(), GwsError> { | |
| let token = &token; | ||
| let time_min = &time_min; | ||
| let time_max = &time_max; | ||
| let tz_name = &tz_name; | ||
| async move { | ||
| let events_url = format!( | ||
| "https://www.googleapis.com/calendar/v3/calendars/{}/events", | ||
|
|
@@ -337,6 +382,7 @@ async fn handle_agenda(matches: &ArgMatches) -> Result<(), GwsError> { | |
| .query(&[ | ||
| ("timeMin", time_min.as_str()), | ||
| ("timeMax", time_max.as_str()), | ||
| ("timeZone", tz_name.as_str()), | ||
| ("singleEvents", "true"), | ||
| ("orderBy", "startTime"), | ||
| ("maxResults", "50"), | ||
|
|
@@ -358,18 +404,7 @@ async fn handle_agenda(matches: &ArgMatches) -> Result<(), GwsError> { | |
| let mut events = Vec::new(); | ||
| if let Some(items) = events_json.get("items").and_then(|i| i.as_array()) { | ||
| for event in items { | ||
| let start = event | ||
| .get("start") | ||
| .and_then(|s| s.get("dateTime").or_else(|| s.get("date"))) | ||
| .and_then(|v| v.as_str()) | ||
| .unwrap_or("") | ||
| .to_string(); | ||
| let end = event | ||
| .get("end") | ||
| .and_then(|s| s.get("dateTime").or_else(|| s.get("date"))) | ||
| .and_then(|v| v.as_str()) | ||
| .unwrap_or("") | ||
| .to_string(); | ||
| let (start, end, all_day) = extract_event_times(event); | ||
| let summary = event | ||
| .get("summary") | ||
| .and_then(|v| v.as_str()) | ||
|
|
@@ -384,6 +419,7 @@ async fn handle_agenda(matches: &ArgMatches) -> Result<(), GwsError> { | |
| events.push(json!({ | ||
| "start": start, | ||
| "end": end, | ||
| "allDay": all_day, | ||
| "summary": summary, | ||
| "calendar": cal.summary, | ||
| "location": location, | ||
|
|
@@ -769,4 +805,53 @@ mod tests { | |
| "tomorrow boundary should carry Denver offset, got {tomorrow_rfc}" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn extract_event_times_all_day() { | ||
| let event = json!({ | ||
| "start": { "date": "2026-03-23" }, | ||
| "end": { "date": "2026-03-24" }, | ||
| "summary": "All-day event" | ||
| }); | ||
| let (start, end, all_day) = extract_event_times(&event); | ||
| assert_eq!(start, "2026-03-23"); | ||
| assert_eq!(end, "2026-03-24"); | ||
| assert!(all_day); | ||
| } | ||
|
|
||
| #[test] | ||
| fn extract_event_times_timed() { | ||
| let event = json!({ | ||
| "start": { "dateTime": "2026-03-23T10:00:00+09:00" }, | ||
| "end": { "dateTime": "2026-03-23T11:00:00+09:00" }, | ||
| "summary": "Timed event" | ||
| }); | ||
| let (start, end, all_day) = extract_event_times(&event); | ||
| assert_eq!(start, "2026-03-23T10:00:00+09:00"); | ||
| assert_eq!(end, "2026-03-23T11:00:00+09:00"); | ||
| assert!(!all_day); | ||
| } | ||
|
|
||
| #[test] | ||
| fn extract_event_times_missing_fields() { | ||
| let event = json!({ "summary": "No dates" }); | ||
| let (start, end, all_day) = extract_event_times(&event); | ||
| assert_eq!(start, ""); | ||
| assert_eq!(end, ""); | ||
| assert!(!all_day); | ||
| } | ||
|
|
||
| #[test] | ||
| fn extract_event_times_all_day_prefers_date_over_datetime() { | ||
| // Regression: if both `date` and `dateTime` exist on an all-day | ||
| // event, the bare date must win so no timezone shift occurs. | ||
| let event = json!({ | ||
| "start": { "date": "2026-03-23", "dateTime": "2026-03-21T15:00:00Z" }, | ||
| "end": { "date": "2026-03-24", "dateTime": "2026-03-22T15:00:00Z" }, | ||
| "summary": "Mixed" | ||
| }); | ||
| let (start, _end, all_day) = extract_event_times(&event); | ||
| assert!(all_day); | ||
| assert_eq!(start, "2026-03-23", "bare date must not be shifted"); | ||
| } | ||
|
Comment on lines
+845
to
+856
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. This regression test does not actually test the scenario where both #[test]
fn extract_event_times_all_day_prefers_date_over_datetime() {
// Regression: if both 'date' and 'dateTime' exist on an all-day
// event, the bare date must win so no timezone shift occurs.
let event = json!({
"start": { "date": "2026-03-23", "dateTime": "2026-03-23T10:00:00Z" },
"end": { "date": "2026-03-24", "dateTime": "2026-03-24T10:00:00Z" },
"summary": "Mixed"
});
let (start, end, all_day) = extract_event_times(&event);
assert!(all_day);
assert_eq!(start, "2026-03-23", "bare date must not be shifted");
assert_eq!(end, "2026-03-24");
} |
||
| } | ||
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.
The logic in
extract_event_timesfor detecting all-day events and extracting their values is redundant and inconsistent with the stated goal of preferring thedatefield when bothdateanddateTimeare present.Currently,
all_dayis only true ifdateTimeis absent. If both are present,all_daybecomesfalse, and the extraction logic then prefersdateTime. This contradicts the comment on line 212 and the intent of the regression test on line 845. Additionally, the extraction of theendtime is fragile because it relies on theall_dayflag derived solely from thestartobject.A simpler and more robust approach is to prefer the
datefield if it exists, which correctly identifies all-day events and avoids timezone shifts as intended.