broxus_util/
time.rs

1#[cfg(all(target_arch = "wasm32", feature = "web"))]
2pub fn now() -> u32 {
3    (js_sys::Date::now() / 1000.0) as u32
4}
5
6pub fn now() -> u32 {
7    use std::time::SystemTime;
8
9    SystemTime::now()
10        .duration_since(SystemTime::UNIX_EPOCH)
11        .expect("shouldn't fail")
12        .as_secs() as u32
13}
14
15#[cfg(all(target_arch = "wasm32", feature = "web"))]
16pub fn now_sec_u64() -> u64 {
17    (js_sys::Date::now() / 1000.0) as u64
18}
19
20#[cfg(not(all(target_arch = "wasm32", feature = "web")))]
21pub fn now_sec_u64() -> u64 {
22    use std::time::SystemTime;
23
24    (SystemTime::now().duration_since(SystemTime::UNIX_EPOCH))
25        .expect("shouldn't fail")
26        .as_secs()
27}
28
29#[cfg(all(target_arch = "wasm32", feature = "web"))]
30pub fn now_ms_f64() -> f64 {
31    js_sys::Date::now()
32}
33
34#[cfg(not(all(target_arch = "wasm32", feature = "web")))]
35pub fn now_ms_f64() -> f64 {
36    use std::time::SystemTime;
37
38    (SystemTime::now().duration_since(SystemTime::UNIX_EPOCH))
39        .expect("shouldn't fail")
40        .as_secs_f64()
41        * 1000.0
42}
43
44#[cfg(all(target_arch = "wasm32", feature = "web"))]
45pub fn now_ms_u64() -> u64 {
46    js_sys::Date::now() as u64
47}
48
49#[cfg(not(all(target_arch = "wasm32", feature = "web")))]
50pub fn now_ms_u64() -> u64 {
51    use std::time::SystemTime;
52
53    let duration =
54        (SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)).expect("shouldn't fail");
55    duration.as_secs() * 1000 + duration.subsec_millis() as u64
56}