summaryrefslogtreecommitdiffstats
path: root/examples/clock
diff options
context:
space:
mode:
authorLibravatar Skygrango <skygrango@gmail.com>2024-05-03 13:04:39 +0800
committerLibravatar Skygrango <skygrango@gmail.com>2024-05-03 13:10:46 +0800
commitf9124470b4b6adca19094dd0f5e663efd9471ec3 (patch)
treef3db3654209b1f5ae0a27d2ed3b56d24ab562c60 /examples/clock
parentfe240a93aacd15bd3fa75876054753a53bda9054 (diff)
downloadiced-f9124470b4b6adca19094dd0f5e663efd9471ec3.tar.gz
iced-f9124470b4b6adca19094dd0f5e663efd9471ec3.tar.bz2
iced-f9124470b4b6adca19094dd0f5e663efd9471ec3.zip
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)
Diffstat (limited to 'examples/clock')
-rw-r--r--examples/clock/Cargo.toml3
-rw-r--r--examples/clock/src/main.rs26
2 files changed, 13 insertions, 16 deletions
diff --git a/examples/clock/Cargo.toml b/examples/clock/Cargo.toml
index dc2e5382..2fddc7da 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 d717db36..3ffc9f07 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<time::Local>,
clock: Cache,
}
#[derive(Debug, Clone, Copy)]
enum Message {
- Tick(time::OffsetDateTime),
+ Tick(time::DateTime<time::Local>),
}
impl Clock {
@@ -54,16 +57,12 @@ impl Clock {
}
fn subscription(&self) -> Subscription<Message> {
- 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<Message> canvas::Program<Message> 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());