Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clock example doesn't get the correct local time under unix system #2421

Merged
merged 3 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions examples/clock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ publish = false
[dependencies]
iced.workspace = true
iced.features = ["canvas", "tokio", "debug"]

time = { version = "0.3", features = ["local-offset"] }
chrono = "0.4"
tracing-subscriber = "0.3"
22 changes: 10 additions & 12 deletions examples/clock/src/main.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -18,13 +19,13 @@ pub fn main() -> iced::Result {
}

struct Clock {
now: time::OffsetDateTime,
now: chrono::DateTime<chrono::Local>,
clock: Cache,
}

#[derive(Debug, Clone, Copy)]
enum Message {
Tick(time::OffsetDateTime),
Tick(chrono::DateTime<chrono::Local>),
}

impl Clock {
Expand Down Expand Up @@ -54,25 +55,20 @@ 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()),
)
})
time::every(time::Duration::from_millis(500))
.map(|_| Message::Tick(chrono::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()
}
}

impl Default for Clock {
fn default() -> Self {
Self {
now: time::OffsetDateTime::now_local()
.unwrap_or_else(|_| time::OffsetDateTime::now_utc()),
now: chrono::offset::Local::now(),
clock: Cache::default(),
}
}
Expand All @@ -89,6 +85,8 @@ impl<Message> canvas::Program<Message> for Clock {
bounds: Rectangle,
_cursor: mouse::Cursor,
) -> Vec<Geometry> {
use chrono::Timelike;

let clock = self.clock.draw(renderer, bounds.size(), |frame| {
let palette = theme.extended_palette();

Expand Down Expand Up @@ -169,7 +167,7 @@ impl<Message> canvas::Program<Message> 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)
Expand Down
Loading