Skip to content

Commit 7f92744

Browse files
committed
Fix the problem of not getting the correct local time under unix system
There is a long-standing problem (time-rs/time#293) that has not yet been solved by time-rs Switch to chrono as it seemed to solve the problem (chronotope/chrono#677)
1 parent fe240a9 commit 7f92744

2 files changed

Lines changed: 13 additions & 16 deletions

File tree

examples/clock/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ publish = false
88
[dependencies]
99
iced.workspace = true
1010
iced.features = ["canvas", "tokio", "debug"]
11-
12-
time = { version = "0.3", features = ["local-offset"] }
11+
chrono = { version = "0.4", features = [ "clock" ] }
1312
tracing-subscriber = "0.3"

examples/clock/src/main.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ use iced::{
77
Theme, Vector,
88
};
99

10+
use chrono as time;
11+
use time::Timelike;
12+
1013
pub fn main() -> iced::Result {
1114
tracing_subscriber::fmt::init();
1215

@@ -18,13 +21,13 @@ pub fn main() -> iced::Result {
1821
}
1922

2023
struct Clock {
21-
now: time::OffsetDateTime,
24+
now: time::DateTime<time::Local>,
2225
clock: Cache,
2326
}
2427

2528
#[derive(Debug, Clone, Copy)]
2629
enum Message {
27-
Tick(time::OffsetDateTime),
30+
Tick(time::DateTime<time::Local>),
2831
}
2932

3033
impl Clock {
@@ -54,25 +57,20 @@ impl Clock {
5457
}
5558

5659
fn subscription(&self) -> Subscription<Message> {
57-
iced::time::every(std::time::Duration::from_millis(500)).map(|_| {
58-
Message::Tick(
59-
time::OffsetDateTime::now_local()
60-
.unwrap_or_else(|_| time::OffsetDateTime::now_utc()),
61-
)
62-
})
60+
iced::time::every(std::time::Duration::from_millis(500))
61+
.map(|_| Message::Tick(time::offset::Local::now()))
6362
}
6463

6564
fn theme(&self) -> Theme {
66-
Theme::ALL[(self.now.unix_timestamp() as usize / 10) % Theme::ALL.len()]
65+
Theme::ALL[(self.now.timestamp() as usize / 10) % Theme::ALL.len()]
6766
.clone()
6867
}
6968
}
7069

7170
impl Default for Clock {
7271
fn default() -> Self {
7372
Self {
74-
now: time::OffsetDateTime::now_local()
75-
.unwrap_or_else(|_| time::OffsetDateTime::now_utc()),
73+
now: time::offset::Local::now(),
7674
clock: Cache::default(),
7775
}
7876
}
@@ -127,17 +125,17 @@ impl<Message> canvas::Program<Message> for Clock {
127125
frame.translate(Vector::new(center.x, center.y));
128126

129127
frame.with_save(|frame| {
130-
frame.rotate(hand_rotation(self.now.hour(), 12));
128+
frame.rotate(hand_rotation(self.now.hour() as u8, 12));
131129
frame.stroke(&short_hand, wide_stroke());
132130
});
133131

134132
frame.with_save(|frame| {
135-
frame.rotate(hand_rotation(self.now.minute(), 60));
133+
frame.rotate(hand_rotation(self.now.minute() as u8, 60));
136134
frame.stroke(&long_hand, wide_stroke());
137135
});
138136

139137
frame.with_save(|frame| {
140-
let rotation = hand_rotation(self.now.second(), 60);
138+
let rotation = hand_rotation(self.now.second() as u8, 60);
141139

142140
frame.rotate(rotation);
143141
frame.stroke(&long_hand, thin_stroke());

0 commit comments

Comments
 (0)