Skip to content

Commit d993b53

Browse files
authored
Merge pull request #2421 from skygrango/wasm/update-clock-example
Fix `clock` example doesn't get the correct local time under unix system
2 parents fb23e4c + 4936efc commit d993b53

2 files changed

Lines changed: 11 additions & 14 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 = "0.4"
1312
tracing-subscriber = "0.3"

examples/clock/src/main.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use iced::alignment;
22
use iced::mouse;
3+
use iced::time;
34
use iced::widget::canvas::{stroke, Cache, Geometry, LineCap, Path, Stroke};
45
use iced::widget::{canvas, container};
56
use iced::{
@@ -18,13 +19,13 @@ pub fn main() -> iced::Result {
1819
}
1920

2021
struct Clock {
21-
now: time::OffsetDateTime,
22+
now: chrono::DateTime<chrono::Local>,
2223
clock: Cache,
2324
}
2425

2526
#[derive(Debug, Clone, Copy)]
2627
enum Message {
27-
Tick(time::OffsetDateTime),
28+
Tick(chrono::DateTime<chrono::Local>),
2829
}
2930

3031
impl Clock {
@@ -54,25 +55,20 @@ impl Clock {
5455
}
5556

5657
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-
})
58+
time::every(time::Duration::from_millis(500))
59+
.map(|_| Message::Tick(chrono::offset::Local::now()))
6360
}
6461

6562
fn theme(&self) -> Theme {
66-
Theme::ALL[(self.now.unix_timestamp() as usize / 10) % Theme::ALL.len()]
63+
Theme::ALL[(self.now.timestamp() as usize / 10) % Theme::ALL.len()]
6764
.clone()
6865
}
6966
}
7067

7168
impl Default for Clock {
7269
fn default() -> Self {
7370
Self {
74-
now: time::OffsetDateTime::now_local()
75-
.unwrap_or_else(|_| time::OffsetDateTime::now_utc()),
71+
now: chrono::offset::Local::now(),
7672
clock: Cache::default(),
7773
}
7874
}
@@ -89,6 +85,8 @@ impl<Message> canvas::Program<Message> for Clock {
8985
bounds: Rectangle,
9086
_cursor: mouse::Cursor,
9187
) -> Vec<Geometry> {
88+
use chrono::Timelike;
89+
9290
let clock = self.clock.draw(renderer, bounds.size(), |frame| {
9391
let palette = theme.extended_palette();
9492

@@ -169,7 +167,7 @@ impl<Message> canvas::Program<Message> for Clock {
169167
}
170168
}
171169

172-
fn hand_rotation(n: u8, total: u8) -> Degrees {
170+
fn hand_rotation(n: u32, total: u32) -> Degrees {
173171
let turns = n as f32 / total as f32;
174172

175173
Degrees(360.0 * turns)

0 commit comments

Comments
 (0)