Skip to content

Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.

Calendar: all-day and floating events stay on their day when the zone changes

Yashwanth Kamireddib9ca0d0c6485
diff --git a/crates/quince-core/src/calendar.rs b/crates/quince-core/src/calendar.rs
index 6f9e430..6a38db0 100644
--- a/crates/quince-core/src/calendar.rs
+++ b/crates/quince-core/src/calendar.rs
@@ -53,7 +53,88 @@ fn now() -> i64 {
.map_or(0, |d| i64::try_from(d.as_secs()).unwrap_or(i64::MAX))
}
+/// The columns every event is read with, `floating` last; see [`Library::event_from_row`].
+pub(crate) const EVENT_COLUMNS: &str =
+ "guid, calendar_guid, title, location, starts_at, ends_at, all_day,
+ timezone, url, notes, etag, floating";
+
+/// Whether an event is a day on the calendar or a time on the clock rather than an instant:
+/// all-day, or timed with no zone (Calendar's "floating"). Kept as the wall-clock time, it is
+/// read in whatever zone the computer is in when it is shown.
+#[must_use]
+pub fn floats(e: &Event) -> bool {
+ e.all_day || e.timezone.is_none()
+}
+
+/// An instant as the wall-clock time it is in `zone`, counted as if that time were UTC.
+#[must_use]
+pub fn wall_clock(seconds: i64, zone: &jiff::tz::TimeZone) -> i64 {
+ jiff::Timestamp::from_second(seconds).map_or(seconds, |t| {
+ t.to_zoned(zone.clone())
+ .datetime()
+ .to_zoned(jiff::tz::TimeZone::UTC)
+ .map_or(seconds, |z| z.timestamp().as_second())
+ })
+}
+
+/// [`wall_clock`] turned back: the instant that wall-clock time is in `zone`. A time the
+/// clocks skip is read as the hour after, as Calendar reads it.
+#[must_use]
+pub fn instant_of(wall: i64, zone: &jiff::tz::TimeZone) -> i64 {
+ jiff::Timestamp::from_second(wall).map_or(wall, |t| {
+ t.to_zoned(jiff::tz::TimeZone::UTC)
+ .datetime()
+ .to_zoned(zone.clone())
+ .map_or(wall, |z| z.timestamp().as_second())
+ })
+}
+
+/// [`EVENT_COLUMNS`], each as `table.column`.
+fn prefixed(table: &str) -> String {
+ EVENT_COLUMNS
+ .split(',')
+ .map(|c| format!("{table}{}", c.trim()))
+ .collect::<Vec<_>>()
+ .join(", ")
+}
+
+/// Furthest any zone's clock is from UTC, and then some: the margin a window over stored
+/// wall-clock times is widened by before each is read in the zone and judged exactly.
+const WIDEST_OFFSET: i64 = 86_400;
+
impl Library {
+ /// The zone floating events are read and stored in now.
+ pub(crate) fn local_zone(&self) -> jiff::tz::TimeZone {
+ self.zone.clone().unwrap_or_else(jiff::tz::TimeZone::system)
+ }
+
+ /// Read floating events as if this computer were in `zone`. Tests only, and a check.
+ pub fn set_zone(&mut self, zone: Option<jiff::tz::TimeZone>) {
+ self.zone = zone;
+ }
+
+ /// An event as stored, with a floating one's wall-clock times read in this zone now.
+ pub(crate) fn event_from_row(
+ row: &rusqlite::Row<'_>,
+ zone: &jiff::tz::TimeZone,
+ ) -> rusqlite::Result<Event> {
+ let floating = row.get::<_, i64>(11)? != 0;
+ let read = |at: i64| if floating { instant_of(at, zone) } else { at };
+ Ok(Event {
+ guid: row.get(0)?,
+ calendar_guid: row.get(1)?,
+ title: row.get(2)?,
+ location: row.get(3)?,
+ starts_at: read(row.get(4)?),
+ ends_at: read(row.get(5)?),
+ all_day: row.get::<_, i64>(6)? != 0,
+ timezone: row.get(7)?,
+ url: row.get(8)?,
+ notes: row.get(9)?,
+ etag: row.get(10)?,
+ })
+ }
+
/// Replace the set of calendars, dropping any Apple no longer reports.
///
/// Removal cascades to their events, which is what should happen when a calendar is
@@ -95,10 +176,9 @@ impl Library {
if keep.is_empty() {
tx.execute("DELETE FROM calendars", [])?;
} else {
- let placeholders = vec!["?"; keep.len()].join(",");
tx.execute(
- &format!("DELETE FROM calendars WHERE guid NOT IN ({placeholders})"),
- rusqlite::params_from_iter(keep),
+ "DELETE FROM calendars WHERE guid NOT IN (SELECT value FROM json_each(?1))",
+ params![crate::id_list(&keep)],
)?;
}
}
@@ -108,12 +188,13 @@ impl Library {
/// Upsert events. Existing rows for the same guid are updated in place.
pub fn put_events(&mut self, events: &[Event]) -> Result<()> {
+ let zone = self.local_zone();
let tx = self.conn.savepoint()?;
{
let mut stmt = tx.prepare(
"INSERT INTO events (guid, calendar_guid, title, location, starts_at, ends_at,
- all_day, timezone, url, notes, etag, updated_at)
- VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12)
+ all_day, timezone, url, notes, etag, updated_at, floating)
+ VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13)
ON CONFLICT(guid) DO UPDATE SET
calendar_guid = excluded.calendar_guid,
title = excluded.title,
@@ -125,23 +206,27 @@ impl Library {
url = excluded.url,
notes = excluded.notes,
etag = excluded.etag,
- updated_at = excluded.updated_at",
+ updated_at = excluded.updated_at,
+ floating = excluded.floating",
)?;
let stamp = now();
for e in events {
+ let floating = floats(e);
+ let keep = |at: i64| if floating { wall_clock(at, &zone) } else { at };
stmt.execute(params![
e.guid,
e.calendar_guid,
e.title,
e.location,
- e.starts_at,
- e.ends_at,
+ keep(e.starts_at),
+ keep(e.ends_at),
i64::from(e.all_day),
e.timezone,
e.url,
e.notes,
e.etag,
- stamp
+ stamp,
+ i64::from(floating)
])?;
}
}
@@ -174,30 +259,29 @@ impl Library {
/// Overlap rather than containment: a meeting that started before the window but is
/// still running belongs in today's view.
pub fn events_between(&self, from: i64, to: i64) -> Result<Vec<Event>> {
- let mut stmt = self.conn.prepare(
- "SELECT e.guid, e.calendar_guid, e.title, e.location, e.starts_at, e.ends_at,
- e.all_day, e.timezone, e.url, e.notes, e.etag
- FROM events e
+ // A floating event's stored times are its wall clock: the window is widened by the
+ // furthest a zone is from UTC to find it, and each judged exactly once read.
+ let mut stmt = self.conn.prepare(&format!(
+ "SELECT {} FROM events e
JOIN calendars c ON c.guid = e.calendar_guid
- WHERE c.enabled = 1 AND e.starts_at < ?2 AND e.ends_at > ?1
- ORDER BY e.starts_at, e.title",
- )?;
- let rows = stmt.query_map(params![from, to], |row| {
- Ok(Event {
- guid: row.get(0)?,
- calendar_guid: row.get(1)?,
- title: row.get(2)?,
- location: row.get(3)?,
- starts_at: row.get(4)?,
- ends_at: row.get(5)?,
- all_day: row.get::<_, i64>(6)? != 0,
- timezone: row.get(7)?,
- url: row.get(8)?,
- notes: row.get(9)?,
- etag: row.get(10)?,
- })
+ WHERE c.enabled = 1
+ AND ((e.floating = 0 AND e.starts_at < ?2 AND e.ends_at > ?1)
+ OR (e.floating = 1 AND e.starts_at < ?2 + ?3 AND e.ends_at > ?1 - ?3))",
+ prefixed("e.")
+ ))?;
+ let zone = self.local_zone();
+ let rows = stmt.query_map(params![from, to, WIDEST_OFFSET], |row| {
+ Self::event_from_row(row, &zone)
})?;
- Ok(rows.collect::<rusqlite::Result<Vec<_>>>()?)
+ let mut found = Vec::new();
+ for e in rows {
+ let e = e?;
+ if e.starts_at < to && e.ends_at > from {
+ found.push(e);
+ }
+ }
+ found.sort_by(|a, b| (a.starts_at, &a.title).cmp(&(b.starts_at, &b.title)));
+ Ok(found)
}
/// Events whose title, place or notes hold every word of `query`, accent- and
@@ -215,26 +299,11 @@ impl Library {
if words.is_empty() {
return Ok(Vec::new());
}
- let mut stmt = self.conn.prepare(
- "SELECT guid, calendar_guid, title, location, starts_at, ends_at, all_day,
- timezone, url, notes, etag
- FROM events",
- )?;
- let rows = stmt.query_map([], |row| {
- Ok(Event {
- guid: row.get(0)?,
- calendar_guid: row.get(1)?,
- title: row.get(2)?,
- location: row.get(3)?,
- starts_at: row.get(4)?,
- ends_at: row.get(5)?,
- all_day: row.get::<_, i64>(6)? != 0,
- timezone: row.get(7)?,
- url: row.get(8)?,
- notes: row.get(9)?,
- etag: row.get(10)?,
- })
- })?;
+ let mut stmt = self
+ .conn
+ .prepare(&format!("SELECT {EVENT_COLUMNS} FROM events"))?;
+ let zone = self.local_zone();
+ let rows = stmt.query_map([], |row| Self::event_from_row(row, &zone))?;
let mut found = Vec::new();
for e in rows {
let e = e?;
@@ -261,37 +330,43 @@ impl Library {
/// One event by its guid, if the library still holds it.
pub fn event(&self, guid: &str) -> Result<Option<Event>> {
- let mut stmt = self.conn.prepare(
- "SELECT guid, calendar_guid, title, location, starts_at, ends_at, all_day,
- timezone, url, notes, etag
- FROM events WHERE guid = ?1",
- )?;
- let mut rows = stmt.query_map(params![guid], |row| {
- Ok(Event {
- guid: row.get(0)?,
- calendar_guid: row.get(1)?,
- title: row.get(2)?,
- location: row.get(3)?,
- starts_at: row.get(4)?,
- ends_at: row.get(5)?,
- all_day: row.get::<_, i64>(6)? != 0,
- timezone: row.get(7)?,
- url: row.get(8)?,
- notes: row.get(9)?,
- etag: row.get(10)?,
- })
- })?;
+ let mut stmt = self.conn.prepare(&format!(
+ "SELECT {EVENT_COLUMNS} FROM events WHERE guid = ?1"
+ ))?;
+ let zone = self.local_zone();
+ let mut rows = stmt.query_map(params![guid], |row| Self::event_from_row(row, &zone))?;
rows.next().transpose().map_err(Into::into)
}
/// Every stored event starting in `[from, to)`, as (guid, calendar), whatever its
/// calendar's switch — what a pass over that window has to account for.
pub fn events_starting_between(&self, from: i64, to: i64) -> Result<Vec<(String, String)>> {
+ // Widened for floating events, then judged on each one's start as read in this zone
+ // now: a pass that prunes what Apple did not return over a window must not reach
+ // past it.
let mut stmt = self.conn.prepare(
- "SELECT guid, calendar_guid FROM events WHERE starts_at >= ?1 AND starts_at < ?2",
+ "SELECT guid, calendar_guid, starts_at, floating FROM events
+ WHERE (floating = 0 AND starts_at >= ?1 AND starts_at < ?2)
+ OR (floating = 1 AND starts_at >= ?1 - ?3 AND starts_at < ?2 + ?3)",
)?;
- let rows = stmt.query_map(params![from, to], |row| Ok((row.get(0)?, row.get(1)?)))?;
- Ok(rows.collect::<rusqlite::Result<Vec<_>>>()?)
+ let zone = self.local_zone();
+ let rows = stmt.query_map(params![from, to, WIDEST_OFFSET], |row| {
+ let at: i64 = row.get(2)?;
+ let at = if row.get::<_, i64>(3)? != 0 {
+ instant_of(at, &zone)
+ } else {
+ at
+ };
+ Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?, at))
+ })?;
+ let mut found = Vec::new();
+ for row in rows {
+ let (guid, calendar, at) = row?;
+ if (from..to).contains(&at) {
+ found.push((guid, calendar));
+ }
+ }
+ Ok(found)
}
/// Drop events Apple no longer has.
@@ -376,6 +451,179 @@ mod tests {
lib
}
+ fn zone(name: &str) -> jiff::tz::TimeZone {
+ jiff::tz::TimeZone::get(name).unwrap()
+ }
+
+ /// `y-m-d h:m` in `zone`, as an instant.
+ fn at(name: &str, y: i16, m: i8, d: i8, h: i8, min: i8) -> i64 {
+ jiff::civil::DateTime::new(y, m, d, h, min, 0, 0)
+ .unwrap()
+ .to_zoned(zone(name))
+ .unwrap()
+ .timestamp()
+ .as_second()
+ }
+
+ fn wall(name: &str, seconds: i64) -> String {
+ jiff::Timestamp::from_second(seconds)
+ .unwrap()
+ .to_zoned(zone(name))
+ .strftime("%Y-%m-%d %H:%M")
+ .to_string()
+ }
+
+ fn all_day(guid: &str, starts_at: i64, ends_at: i64) -> Event {
+ Event {
+ all_day: true,
+ timezone: None,
+ ..event(guid, "cal-1", starts_at, ends_at)
+ }
+ }
+
+ /// Flown from London to New York: a birthday stays on its day, and a floating 9:00 at
+ /// 9:00, read in the zone the computer is in now. Kept as instants, both drew on the day
+ /// before until the next full pass, and an edit wrote that day back to Apple.
+ #[test]
+ fn all_day_and_floating_events_stay_put_when_the_zone_changes() {
+ let mut lib = seeded();
+ lib.set_zone(Some(zone("Europe/London")));
+ let birthday = all_day(
+ "B",
+ at("Europe/London", 2026, 11, 2, 0, 0),
+ at("Europe/London", 2026, 11, 3, 0, 0),
+ );
+ let floating = Event {
+ timezone: None,
+ ..event(
+ "F",
+ "cal-1",
+ at("Europe/London", 2026, 11, 2, 9, 0),
+ at("Europe/London", 2026, 11, 2, 10, 0),
+ )
+ };
+ let timed = Event {
+ timezone: Some("Europe/London".into()),
+ ..event(
+ "T",
+ "cal-1",
+ at("Europe/London", 2026, 11, 2, 9, 0),
+ at("Europe/London", 2026, 11, 2, 10, 0),
+ )
+ };
+ lib.put_events(&[birthday, floating, timed.clone()])
+ .unwrap();
+
+ lib.set_zone(Some(zone("America/New_York")));
+ let b = lib.event("B").unwrap().unwrap();
+ assert_eq!(wall("America/New_York", b.starts_at), "2026-11-02 00:00");
+ assert_eq!(wall("America/New_York", b.ends_at), "2026-11-03 00:00");
+ let f = lib.event("F").unwrap().unwrap();
+ assert_eq!(wall("America/New_York", f.starts_at), "2026-11-02 09:00");
+ // A timed event with a zone is the instant it always was: 4 AM in New York.
+ assert_eq!(lib.event("T").unwrap().unwrap().starts_at, timed.starts_at);
+
+ // The day's window in New York finds the birthday, the day before's does not.
+ let day = |d: i8| {
+ lib.events_between(
+ at("America/New_York", 2026, 11, d, 0, 0),
+ at("America/New_York", 2026, 11, d + 1, 0, 0),
+ )
+ .unwrap()
+ .into_iter()
+ .map(|e| e.guid)
+ .filter(|g| g == "B")
+ .count()
+ };
+ assert_eq!(day(2), 1);
+ assert_eq!(day(1), 0, "never on the day before");
+ // A pass over a window prunes only what starts in it, read in this zone.
+ let starting = lib
+ .events_starting_between(
+ at("America/New_York", 2026, 11, 1, 0, 0),
+ at("America/New_York", 2026, 11, 2, 0, 0),
+ )
+ .unwrap();
+ assert!(starting.iter().all(|(g, _)| g != "B" && g != "F"));
+
+ // Edited there, it is stored as the day it shows: back home it is still that day.
+ let mut edited = lib.event("B").unwrap().unwrap();
+ edited.title = "Birthday".into();
+ lib.put_events(&[edited]).unwrap();
+ lib.set_zone(Some(zone("Europe/London")));
+ let b = lib.event("B").unwrap().unwrap();
+ assert_eq!(wall("Europe/London", b.starts_at), "2026-11-02 00:00");
+ }
+
+ /// A floating 9:00 stays 9:00 across a change of the clocks, and on the day itself.
+ #[test]
+ fn a_floating_time_holds_across_the_clocks_changing() {
+ let mut lib = seeded();
+ lib.set_zone(Some(zone("America/New_York")));
+ let before = at("America/New_York", 2026, 3, 7, 9, 0);
+ let on_the_day = at("America/New_York", 2026, 3, 8, 9, 0);
+ let mk = |g: &str, t: i64| Event {
+ timezone: None,
+ ..event(g, "cal-1", t, t + 3600)
+ };
+ lib.put_events(&[mk("A", before), mk("B", on_the_day)])
+ .unwrap();
+ lib.set_zone(Some(zone("Europe/Berlin")));
+ for g in ["A", "B"] {
+ let e = lib.event(g).unwrap().unwrap();
+ assert!(wall("Europe/Berlin", e.starts_at).ends_with("09:00"), "{g}");
+ }
+ assert_eq!(
+ wall("Europe/Berlin", lib.event("B").unwrap().unwrap().starts_at),
+ "2026-03-08 09:00"
+ );
+ }
+
+ /// Events stored before the change are turned over in place, in the zone they were
+ /// stored in, so nothing moves by being migrated.
+ #[test]
+ fn events_stored_before_are_kept_as_they_were() {
+ let dir = tempfile::tempdir().unwrap();
+ let path = dir.path().join("library.db");
+ let system = jiff::tz::TimeZone::system();
+ let zone_then = if system.iana_name().is_some() {
+ system
+ } else {
+ jiff::tz::TimeZone::UTC
+ };
+ let midnight = jiff::civil::date(2026, 12, 25)
+ .to_zoned(zone_then.clone())
+ .unwrap()
+ .timestamp()
+ .as_second();
+ {
+ let mut conn = rusqlite::Connection::open(&path).unwrap();
+ let before = &crate::schema::MIGRATIONS[..48];
+ crate::migrate_with(&mut conn, before, &[]).unwrap();
+ conn.execute_batch(
+ "INSERT INTO calendars (guid, title, sort_order, enabled, updated_at) VALUES ('cal-1', 'Home', 0, 1, 0);",
+ )
+ .unwrap();
+ conn.execute(
+ "INSERT INTO events (guid, calendar_guid, title, starts_at, ends_at, all_day,
+ timezone, updated_at)
+ VALUES ('X', 'cal-1', 'Christmas', ?1, ?2, 1, NULL, 0),
+ ('T', 'cal-1', 'Call', 1000, 2000, 0, 'Asia/Kolkata', 0)",
+ rusqlite::params![midnight, midnight + 86_400],
+ )
+ .unwrap();
+ }
+ let mut lib = Library::open(dir.path()).unwrap();
+ lib.set_zone(Some(zone_then));
+ let x = lib.event("X").unwrap().unwrap();
+ assert_eq!((x.starts_at, x.ends_at), (midnight, midnight + 86_400));
+ assert_eq!(lib.event("T").unwrap().unwrap().starts_at, 1000);
+ // And from then on it is the day, wherever the computer goes.
+ lib.set_zone(Some(zone("Pacific/Auckland")));
+ let x = lib.event("X").unwrap().unwrap();
+ assert_eq!(wall("Pacific/Auckland", x.starts_at), "2026-12-25 00:00");
+ }
+
#[test]
fn calendars_round_trip_and_stay_ordered() {
let lib = seeded();
diff --git a/crates/quince-core/src/export.rs b/crates/quince-core/src/export.rs
index c0805e0..b1c51a8 100644
--- a/crates/quince-core/src/export.rs
+++ b/crates/quince-core/src/export.rs
@@ -79,26 +79,12 @@ impl Library {
/// Every event the library holds, in every calendar, shown or hidden, by start.
pub fn export_events(&self) -> Result<Vec<Event>> {
- let mut stmt = self.conn.prepare(
- "SELECT guid, calendar_guid, title, location, starts_at, ends_at, all_day,
- timezone, url, notes, etag
- FROM events ORDER BY calendar_guid, starts_at, guid",
- )?;
- let rows = stmt.query_map([], |row| {
- Ok(Event {
- guid: row.get(0)?,
- calendar_guid: row.get(1)?,
- title: row.get(2)?,
- location: row.get(3)?,
- starts_at: row.get(4)?,
- ends_at: row.get(5)?,
- all_day: row.get::<_, i64>(6)? != 0,
- timezone: row.get(7)?,
- url: row.get(8)?,
- notes: row.get(9)?,
- etag: row.get(10)?,
- })
- })?;
+ let mut stmt = self.conn.prepare(&format!(
+ "SELECT {} FROM events ORDER BY calendar_guid, starts_at, guid",
+ crate::calendar::EVENT_COLUMNS
+ ))?;
+ let zone = self.local_zone();
+ let rows = stmt.query_map([], |row| Self::event_from_row(row, &zone))?;
Ok(rows.collect::<rusqlite::Result<Vec<_>>>()?)
}
diff --git a/crates/quince-core/src/schema.rs b/crates/quince-core/src/schema.rs
index 5fc2a8b..dd93d39 100644
--- a/crates/quince-core/src/schema.rs
+++ b/crates/quince-core/src/schema.rs
@@ -848,4 +848,47 @@ pub const MIGRATIONS: &[&str] = &[
r#"
CREATE INDEX IF NOT EXISTS reminder_alarms_by_reminder ON reminder_alarms (reminder_guid);
"#,
+ // 49 — all-day and floating events kept as the day and the time on the clock, not as an
+ // instant. Each was turned into an instant in this computer's zone as it was stored, and
+ // nothing turned it back when the zone changed: flown from London to New York, every
+ // birthday and holiday drew on the day before until the next full pass, and an edit made
+ // meanwhile wrote that wrong day back to Apple. `floating` rows hold their wall-clock
+ // time counted as if in UTC, read in whatever zone the computer is in when shown; the
+ // rows already stored are turned over by `float_events`, in the same transaction.
+ r#"
+ ALTER TABLE events ADD COLUMN floating INTEGER NOT NULL DEFAULT 0;
+ "#,
];
+
+/// Migrations that need more than SQL, run in the same transaction as the one numbered.
+pub const FIXUPS: &[crate::Fixup] = &[(49, float_events)];
+
+/// The all-day and floating events already stored, from instants to wall-clock time: read in
+/// the zone they were stored in — this computer's, by name, or UTC where it had none, as the
+/// sync did — so nothing moves by being turned over.
+fn float_events(tx: &rusqlite::Transaction<'_>) -> crate::Result<()> {
+ let system = jiff::tz::TimeZone::system();
+ let zone = if system.iana_name().is_some() {
+ system
+ } else {
+ jiff::tz::TimeZone::UTC
+ };
+ let rows: Vec<(String, i64, i64)> = {
+ let mut stmt = tx.prepare(
+ "SELECT guid, starts_at, ends_at FROM events
+ WHERE floating = 0 AND (all_day != 0 OR timezone IS NULL)",
+ )?;
+ let rows = stmt.query_map([], |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)))?;
+ rows.collect::<rusqlite::Result<_>>()?
+ };
+ let mut update =
+ tx.prepare("UPDATE events SET starts_at = ?2, ends_at = ?3, floating = 1 WHERE guid = ?1")?;
+ for (guid, starts, ends) in rows {
+ update.execute(rusqlite::params![
+ guid,
+ crate::calendar::wall_clock(starts, &zone),
+ crate::calendar::wall_clock(ends, &zone),
+ ])?;
+ }
+ Ok(())
+}
diff --git a/crates/quince-core/src/search.rs b/crates/quince-core/src/search.rs
index 81bff9e..37d7508 100644
--- a/crates/quince-core/src/search.rs
+++ b/crates/quince-core/src/search.rs
@@ -463,16 +463,22 @@ impl Library {
}),
2 => conn
.prepare_cached(
- "SELECT calendar_guid, title, location, notes, starts_at, all_day
+ "SELECT calendar_guid, title, location, notes, starts_at, all_day, floating
FROM events WHERE guid = ?1",
)?
.query_row(params![key], |r| {
+ let at = r.get::<_, i64>(4)?;
Ok((
r.get::<_, String>(0)?,
r.get::<_, String>(1)?,
r.get::<_, Option<String>>(2)?,
r.get::<_, Option<String>>(3)?,
- r.get::<_, i64>(4)?,
+ // A floating event's stored time is its wall clock: read in this zone.
+ if r.get::<_, i64>(6)? != 0 {
+ crate::calendar::instant_of(at, &self.local_zone())
+ } else {
+ at
+ },
r.get::<_, i64>(5)? != 0,
))
})
diff --git a/crates/quince-icloud/src/calendar.rs b/crates/quince-icloud/src/calendar.rs
index d707b9e..f0493ac 100644
--- a/crates/quince-icloud/src/calendar.rs
+++ b/crates/quince-icloud/src/calendar.rs
@@ -55,9 +55,7 @@ pub(crate) fn parse_apple_date(raw: &Value, timezone: Option<&str>) -> Result<i6
})?;
// An unknown zone is not worth failing a sync over; UTC keeps the event visible.
- let zone = timezone
- .and_then(|name| TimeZone::get(name).ok())
- .unwrap_or(TimeZone::UTC);
+ let zone = zone_named(timezone);
let zoned = civil_dt
.to_zoned(zone)
@@ -70,9 +68,7 @@ pub(crate) fn parse_apple_date(raw: &Value, timezone: Option<&str>) -> Result<i6
/// Writes must speak the same dialect they read, so this is what a completion timestamp or a
/// due date looks like going back.
pub(crate) fn format_apple_date(seconds: i64, timezone: Option<&str>) -> Value {
- let zone = timezone
- .and_then(|name| TimeZone::get(name).ok())
- .unwrap_or(TimeZone::UTC);
+ let zone = zone_named(timezone);
let zoned = jiff::Timestamp::from_second(seconds)
.unwrap_or(jiff::Timestamp::UNIX_EPOCH)
.to_zoned(zone);
@@ -132,13 +128,39 @@ fn collection_from(v: &Value) -> Option<Calendar> {
/// A timed event with no zone floats, as Calendar calls it: 9:00 wherever it is looked at.
/// Read as UTC it was drawn hours off, and an edit wrote that wrong instant back, moving the
/// meeting on every device. So it too is this machine's clock.
+///
+/// A computer whose zone has no IANA name (TZ set to a POSIX rule, a copied `/etc/localtime`)
+/// is still in a zone: its own, named [`SYSTEM_ZONE`] here. Read as UTC instead, every
+/// all-day event west of Greenwich fell a day early, for good.
fn dates_zone(all_day: bool, timezone: Option<&str>) -> Option<String> {
match timezone {
Some(zone) if !all_day => Some(zone.to_string()),
- _ => TimeZone::system().iana_name().map(ToString::to_string),
+ _ => Some(
+ TimeZone::system()
+ .iana_name()
+ .unwrap_or(SYSTEM_ZONE)
+ .to_string(),
+ ),
+ }
+}
+
+/// This computer's own zone, where it has no name to give.
+pub(crate) const SYSTEM_ZONE: &str = "<system>";
+
+/// The zone a name names: [`SYSTEM_ZONE`] this computer's, an unknown one UTC.
+fn zone_named(name: Option<&str>) -> TimeZone {
+ match name {
+ Some(SYSTEM_ZONE) => TimeZone::system(),
+ Some(name) => TimeZone::get(name).unwrap_or(TimeZone::UTC),
+ None => TimeZone::UTC,
}
}
+/// A zone as Apple is told it (`usertz`): by name, or UTC where it has none.
+fn apple_tz(name: Option<&str>) -> &str {
+ name.filter(|n| *n != SYSTEM_ZONE).unwrap_or("UTC")
+}
+
/// The zone to speak to Apple in about this event — the day it is asked about, the day its
/// dates are written in. One rule everywhere: an all-day event saved in this machine's zone
/// but looked up again in UTC (where Apple, keeping no zone on it, left it) was looked for on
@@ -191,9 +213,7 @@ fn event_from(v: &Value) -> Result<Option<Event>> {
/// `YYYY-MM-DD` for the day an instant falls on, in the event's own timezone.
pub(crate) fn day_of(seconds: i64, timezone: Option<&str>) -> String {
- let zone = timezone
- .and_then(|name| TimeZone::get(name).ok())
- .unwrap_or(TimeZone::UTC);
+ let zone = zone_named(timezone);
jiff::Timestamp::from_second(seconds)
.unwrap_or(jiff::Timestamp::UNIX_EPOCH)
.to_zoned(zone)
@@ -989,7 +1009,7 @@ impl<'a> CalendarClient<'a> {
query.push(("lang".into(), "en-us".into()));
query.push((
"usertz".into(),
- event_zone(event).unwrap_or_else(|| "UTC".into()),
+ apple_tz(event_zone(event).as_deref()).to_string(),
));
query.push(("startDate".into(), day.to_string()));
query.push(("endDate".into(), day.to_string()));
@@ -1367,7 +1387,7 @@ impl<'a> CalendarClient<'a> {
query.push(("startDate".into(), from.into()));
query.push(("endDate".into(), to.into()));
query.push(("lang".into(), "en-us".into()));
- query.push(("usertz".into(), tz.unwrap_or("UTC").into()));
+ query.push(("usertz".into(), apple_tz(tz).into()));
if let Some(method) = method_override {
query.push(("methodOverride".into(), method.into()));
}