-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
50cb830
commit 53c24bb
Showing
6 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use seed::{prelude::*, *}; | ||
use time; | ||
|
||
#[derive(Clone)] | ||
pub struct Model { | ||
date_time: time::OffsetDateTime, | ||
duration_from_now: time::Duration, | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub enum Msg { | ||
Update, | ||
} | ||
|
||
fn now() -> time::OffsetDateTime { | ||
let timestamp = js_sys::Date::now(); | ||
let duration = time::Duration::new(timestamp as i64 / 1000, 0); | ||
time::OffsetDateTime::UNIX_EPOCH + duration | ||
} | ||
|
||
pub fn init(timestamp: &i64) -> Model { | ||
let duration = time::Duration::new(*timestamp, 0); | ||
let date_time = time::OffsetDateTime::UNIX_EPOCH + duration; | ||
let duration_from_now = now() - date_time; | ||
Model { | ||
date_time, | ||
duration_from_now, | ||
} | ||
} | ||
|
||
pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) { | ||
match msg { | ||
Msg::Update => { | ||
model.duration_from_now = time::OffsetDateTime::now_utc() - model.date_time; | ||
} | ||
} | ||
} | ||
|
||
pub fn view(model: &Model) -> Node<Msg> { | ||
div![format!("{} ago", model.duration_from_now)] | ||
} |