veilid/veilid-tools/src/timestamp.rs

176 lines
5.0 KiB
Rust
Raw Normal View History

2022-11-26 21:37:23 -05:00
use super::*;
cfg_if! {
if #[cfg(target_arch = "wasm32")] {
use js_sys::Date;
pub fn get_timestamp() -> u64 {
2022-11-27 10:52:07 -05:00
if is_browser() {
2023-09-18 19:49:57 -04:00
(Date::now() * 1000.0f64) as u64
2022-11-26 21:37:23 -05:00
} else {
panic!("WASM requires browser environment");
}
}
2023-06-26 21:29:02 -04:00
pub fn debug_ts(ts: u64) -> String {
if is_browser() {
2023-07-02 17:23:04 -04:00
let now = Date::new_0();
now.set_time(Date::now());
let date = Date::new_0();
2023-06-26 21:29:02 -04:00
date.set_time((ts / 1000u64) as f64);
let show_year = now.get_utc_full_year() != date.get_utc_full_year();
let show_month = show_year || now.get_utc_month() != date.get_utc_month();
let show_date = show_month || now.get_utc_date() != date.get_utc_date();
2023-09-18 19:49:57 -04:00
let s_year = if show_year {
format!("{:04}/",date.get_utc_full_year())
} else {
"".to_owned()
};
let s_month = if show_month {
format!("{:02}/",date.get_utc_month())
} else {
"".to_owned()
};
let s_date = if show_date {
format!("{:02}-",date.get_utc_date())
} else {
"".to_owned()
};
let s_time = format!("{:02}:{:02}:{:02}.{:04}",
date.get_utc_hours(),
date.get_utc_minutes(),
date.get_utc_seconds(),
date.get_utc_milliseconds()
);
2023-06-26 21:29:02 -04:00
format!("{}{}{}{}",
2023-09-18 19:49:57 -04:00
s_year,
s_month,
s_date,
s_time
)
2023-06-26 21:29:02 -04:00
} else {
panic!("WASM requires browser environment");
}
}
2022-11-26 21:37:23 -05:00
} else {
use std::time::{SystemTime, UNIX_EPOCH};
2023-06-26 21:29:02 -04:00
use chrono::{Datelike, Timelike};
2022-11-26 21:37:23 -05:00
pub fn get_timestamp() -> u64 {
match SystemTime::now().duration_since(UNIX_EPOCH) {
Ok(n) => n.as_micros() as u64,
Err(_) => panic!("SystemTime before UNIX_EPOCH!"),
}
}
2023-06-26 21:29:02 -04:00
pub fn debug_ts(ts: u64) -> String {
let now = chrono::DateTime::<chrono::Utc>::from(SystemTime::now());
let date = chrono::DateTime::<chrono::Utc>::from(UNIX_EPOCH + Duration::from_micros(ts));
let show_year = now.year() != date.year();
let show_month = show_year || now.month() != date.month();
let show_date = show_month || now.day() != date.day();
2023-09-17 19:37:02 -04:00
let s_year = if show_year {
format!("{:04}/",date.year())
} else {
"".to_owned()
};
let s_month = if show_month {
format!("{:02}/",date.month())
} else {
"".to_owned()
};
let s_date = if show_date {
format!("{:02}-",date.day())
} else {
"".to_owned()
};
let s_time = format!("{:02}:{:02}:{:02}.{:04}",
date.hour(),
date.minute(),
date.second(),
date.nanosecond()/1_000_000
);
2023-06-26 21:29:02 -04:00
format!("{}{}{}{}",
2023-09-17 19:37:02 -04:00
s_year,
s_month,
s_date,
s_time)
2023-06-26 21:29:02 -04:00
}
2022-11-26 21:37:23 -05:00
}
}
2023-06-26 21:29:02 -04:00
const DAY: u64 = 1_000_000u64 * 60 * 60 * 24;
const HOUR: u64 = 1_000_000u64 * 60 * 60;
const MIN: u64 = 1_000_000u64 * 60;
const SEC: u64 = 1_000_000u64;
const MSEC: u64 = 1_000u64;
pub fn debug_duration(dur: u64) -> String {
let days = dur / DAY;
let dur = dur % DAY;
let hours = dur / HOUR;
let dur = dur % HOUR;
let mins = dur / MIN;
let dur = dur % MIN;
let secs = dur / SEC;
let dur = dur % SEC;
let msecs = dur / MSEC;
format!(
"{}{}{}{}.{:03}s",
2023-06-26 21:29:02 -04:00
if days != 0 {
format!("{}d", days)
} else {
"".to_owned()
},
if hours != 0 {
format!("{}h", hours)
} else {
"".to_owned()
},
if mins != 0 {
format!("{}m", mins)
} else {
"".to_owned()
},
secs,
2023-06-26 21:29:02 -04:00
msecs
)
}
2023-12-08 10:57:58 -05:00
pub fn parse_duration(s: &str) -> Option<u64> {
let mut dur_total: u64 = 0;
let mut dur: u64 = 0;
for c in s.as_bytes() {
match c {
b'0'..=b'9' => {
dur *= 10;
dur += (c - b'0') as u64;
}
b'h' => {
dur *= 3_600_000u64;
dur_total += dur;
dur = 0;
}
b'm' => {
dur *= 60_000u64;
dur_total += dur;
dur = 0;
}
b's' => {
dur *= 1_000u64;
dur_total += dur;
dur = 0;
}
_ => return None,
}
}
dur_total += dur;
Some(dur_total * 1_000u64)
}