5

Normally in a rust app, I'd get system time with std::time::SystemTime::now(), but that doesn't seem to work in a yew app. Instead the component I'm trying to read the system time from just silently (from the server logs) fails and in the web console I get panicked at 'time not implemented on this platform'.

Which makes sense, the client app can't just grab system time from the OS willy-nilly for security reasons. But does yew provide a way to get this from the browser?

  • 2
    Can't you just use [Javascript bindings](https://rustwasm.github.io/wasm-bindgen/api/js_sys/struct.Date.html)? – HHK Feb 07 '21 at 11:16

2 Answers2

1

https://crates.io/crates/instant looks like it might do the job. But while it defines fn like now() I don't think that it will make crates that rely on std::time to work :-(

Squirrel
  • 1,189
  • 12
  • 15
1

I'm using the chrono crate for this

info!("Time now is {}", chrono::Local::now());

And you can do all sorts of cool stuff with it! For example, if you have a NaiveDateTime in UTC you can convert it into the local user time like this:

use chrono::{Local, NaiveDateTime};

pub fn format_date(date: &NaiveDateTime) -> String {
    Local::from_utc_datetime(&Local, date).format("%Y-%m-%d %H:%M:%S").to_string()
}

With formatting applied it will look like 2023-08-30 02:47:57

Enigo
  • 3,685
  • 5
  • 29
  • 54