From f9124470b4b6adca19094dd0f5e663efd9471ec3 Mon Sep 17 00:00:00 2001 From: Skygrango Date: Fri, 3 May 2024 13:04:39 +0800 Subject: [PATCH 1/3] Fix `clock` example doesn't get the correct local time under unix system There is a long-standing problem (https://github.com/time-rs/time/issues/293) that has not yet been solved by time-rs Switch to chrono as it seemed to solve the problem (https://github.com/chronotope/chrono/pull/677) --- examples/clock/Cargo.toml | 3 +-- examples/clock/src/main.rs | 26 ++++++++++++-------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/examples/clock/Cargo.toml b/examples/clock/Cargo.toml index dc2e5382d8..2fddc7da33 100644 --- a/examples/clock/Cargo.toml +++ b/examples/clock/Cargo.toml @@ -8,6 +8,5 @@ publish = false [dependencies] iced.workspace = true iced.features = ["canvas", "tokio", "debug"] - -time = { version = "0.3", features = ["local-offset"] } +chrono = { version = "0.4", features = [ "clock" ] } tracing-subscriber = "0.3" diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index d717db36ce..3ffc9f0776 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -7,6 +7,9 @@ use iced::{ Theme, Vector, }; +use chrono as time; +use time::Timelike; + pub fn main() -> iced::Result { tracing_subscriber::fmt::init(); @@ -18,13 +21,13 @@ pub fn main() -> iced::Result { } struct Clock { - now: time::OffsetDateTime, + now: time::DateTime, clock: Cache, } #[derive(Debug, Clone, Copy)] enum Message { - Tick(time::OffsetDateTime), + Tick(time::DateTime), } impl Clock { @@ -54,16 +57,12 @@ impl Clock { } fn subscription(&self) -> Subscription { - iced::time::every(std::time::Duration::from_millis(500)).map(|_| { - Message::Tick( - time::OffsetDateTime::now_local() - .unwrap_or_else(|_| time::OffsetDateTime::now_utc()), - ) - }) + iced::time::every(std::time::Duration::from_millis(500)) + .map(|_| Message::Tick(time::offset::Local::now())) } fn theme(&self) -> Theme { - Theme::ALL[(self.now.unix_timestamp() as usize / 10) % Theme::ALL.len()] + Theme::ALL[(self.now.timestamp() as usize / 10) % Theme::ALL.len()] .clone() } } @@ -71,8 +70,7 @@ impl Clock { impl Default for Clock { fn default() -> Self { Self { - now: time::OffsetDateTime::now_local() - .unwrap_or_else(|_| time::OffsetDateTime::now_utc()), + now: time::offset::Local::now(), clock: Cache::default(), } } @@ -127,17 +125,17 @@ impl canvas::Program for Clock { frame.translate(Vector::new(center.x, center.y)); frame.with_save(|frame| { - frame.rotate(hand_rotation(self.now.hour(), 12)); + frame.rotate(hand_rotation(self.now.hour() as u8, 12)); frame.stroke(&short_hand, wide_stroke()); }); frame.with_save(|frame| { - frame.rotate(hand_rotation(self.now.minute(), 60)); + frame.rotate(hand_rotation(self.now.minute() as u8, 60)); frame.stroke(&long_hand, wide_stroke()); }); frame.with_save(|frame| { - let rotation = hand_rotation(self.now.second(), 60); + let rotation = hand_rotation(self.now.second() as u8, 60); frame.rotate(rotation); frame.stroke(&long_hand, thin_stroke()); From d265cc133efbe02cab890260dbce16768f3d06dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sat, 18 May 2024 11:29:41 +0200 Subject: [PATCH 2/3] Simplify `clock` example a bit --- examples/clock/src/main.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 3ffc9f0776..7c4685c428 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -1,5 +1,6 @@ use iced::alignment; use iced::mouse; +use iced::time; use iced::widget::canvas::{stroke, Cache, Geometry, LineCap, Path, Stroke}; use iced::widget::{canvas, container}; use iced::{ @@ -7,9 +8,6 @@ use iced::{ Theme, Vector, }; -use chrono as time; -use time::Timelike; - pub fn main() -> iced::Result { tracing_subscriber::fmt::init(); @@ -21,13 +19,13 @@ pub fn main() -> iced::Result { } struct Clock { - now: time::DateTime, + now: chrono::DateTime, clock: Cache, } #[derive(Debug, Clone, Copy)] enum Message { - Tick(time::DateTime), + Tick(chrono::DateTime), } impl Clock { @@ -57,8 +55,8 @@ impl Clock { } fn subscription(&self) -> Subscription { - iced::time::every(std::time::Duration::from_millis(500)) - .map(|_| Message::Tick(time::offset::Local::now())) + time::every(time::Duration::from_millis(500)) + .map(|_| Message::Tick(chrono::offset::Local::now())) } fn theme(&self) -> Theme { @@ -70,7 +68,7 @@ impl Clock { impl Default for Clock { fn default() -> Self { Self { - now: time::offset::Local::now(), + now: chrono::offset::Local::now(), clock: Cache::default(), } } @@ -87,6 +85,8 @@ impl canvas::Program for Clock { bounds: Rectangle, _cursor: mouse::Cursor, ) -> Vec { + use chrono::Timelike; + let clock = self.clock.draw(renderer, bounds.size(), |frame| { let palette = theme.extended_palette(); @@ -125,17 +125,17 @@ impl canvas::Program for Clock { frame.translate(Vector::new(center.x, center.y)); frame.with_save(|frame| { - frame.rotate(hand_rotation(self.now.hour() as u8, 12)); + frame.rotate(hand_rotation(self.now.hour(), 12)); frame.stroke(&short_hand, wide_stroke()); }); frame.with_save(|frame| { - frame.rotate(hand_rotation(self.now.minute() as u8, 60)); + frame.rotate(hand_rotation(self.now.minute(), 60)); frame.stroke(&long_hand, wide_stroke()); }); frame.with_save(|frame| { - let rotation = hand_rotation(self.now.second() as u8, 60); + let rotation = hand_rotation(self.now.second(), 60); frame.rotate(rotation); frame.stroke(&long_hand, thin_stroke()); @@ -167,7 +167,7 @@ impl canvas::Program for Clock { } } -fn hand_rotation(n: u8, total: u8) -> Degrees { +fn hand_rotation(n: u32, total: u32) -> Degrees { let turns = n as f32 / total as f32; Degrees(360.0 * turns) From 4936efc3751b769984bff4344a9fbb198a7c1ea2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sat, 18 May 2024 11:32:26 +0200 Subject: [PATCH 3/3] Remove redundant default `chrono` feature in `clock` example --- examples/clock/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/clock/Cargo.toml b/examples/clock/Cargo.toml index 2fddc7da33..bc6c202b0e 100644 --- a/examples/clock/Cargo.toml +++ b/examples/clock/Cargo.toml @@ -8,5 +8,5 @@ publish = false [dependencies] iced.workspace = true iced.features = ["canvas", "tokio", "debug"] -chrono = { version = "0.4", features = [ "clock" ] } +chrono = "0.4" tracing-subscriber = "0.3"