2018-06-06 16:36:54 -07:00
|
|
|
//! The `timing` module provides std::time utility functions.
|
2019-11-21 12:05:31 -08:00
|
|
|
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
2018-05-07 16:49:15 -07:00
|
|
|
|
2019-04-18 22:31:25 -07:00
|
|
|
pub fn duration_as_ns(d: &Duration) -> u64 {
|
|
|
|
d.as_secs() * 1_000_000_000 + u64::from(d.subsec_nanos())
|
|
|
|
}
|
|
|
|
|
2018-06-22 10:44:31 -07:00
|
|
|
pub fn duration_as_us(d: &Duration) -> u64 {
|
2018-07-11 13:40:46 -07:00
|
|
|
(d.as_secs() * 1000 * 1000) + (u64::from(d.subsec_nanos()) / 1_000)
|
2018-06-22 10:44:31 -07:00
|
|
|
}
|
|
|
|
|
2018-05-07 16:49:15 -07:00
|
|
|
pub fn duration_as_ms(d: &Duration) -> u64 {
|
2018-07-11 13:40:46 -07:00
|
|
|
(d.as_secs() * 1000) + (u64::from(d.subsec_nanos()) / 1_000_000)
|
2018-05-07 16:49:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn duration_as_s(d: &Duration) -> f32 {
|
2018-07-11 13:40:46 -07:00
|
|
|
d.as_secs() as f32 + (d.subsec_nanos() as f32 / 1_000_000_000.0)
|
2018-05-07 16:49:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn timestamp() -> u64 {
|
2018-05-11 11:38:52 -07:00
|
|
|
let now = SystemTime::now()
|
|
|
|
.duration_since(UNIX_EPOCH)
|
|
|
|
.expect("create timestamp in timing");
|
2018-07-11 13:40:46 -07:00
|
|
|
duration_as_ms(&now)
|
2018-05-07 16:49:15 -07:00
|
|
|
}
|
2019-11-21 12:05:31 -08:00
|
|
|
|
|
|
|
pub const SECONDS_PER_YEAR: f64 = (365.242_199 * 24.0 * 60.0 * 60.0);
|
|
|
|
|
|
|
|
/// from years to slots
|
|
|
|
pub fn years_as_slots(years: f64, tick_duration: &Duration, ticks_per_slot: u64) -> f64 {
|
|
|
|
// slots is years * slots/year
|
|
|
|
years *
|
|
|
|
// slots/year is seconds/year ...
|
|
|
|
SECONDS_PER_YEAR
|
|
|
|
// * (ns/s)/(ns/tick) / ticks/slot = 1/s/1/tick = ticks/s
|
|
|
|
*(1_000_000_000.0 / duration_as_ns(tick_duration) as f64)
|
|
|
|
// / ticks/slot
|
|
|
|
/ ticks_per_slot as f64
|
|
|
|
}
|
|
|
|
|
2019-12-06 13:38:49 -08:00
|
|
|
/// From slots per year to slot duration
|
2019-11-25 23:40:36 -08:00
|
|
|
pub fn slot_duration_from_slots_per_year(slots_per_year: f64) -> Duration {
|
|
|
|
// Regarding division by zero potential below: for some reason, if Rust stores an `inf` f64 and
|
|
|
|
// then converts it to a u64 on use, it always returns 0, as opposed to std::u64::MAX or any
|
|
|
|
// other huge value
|
|
|
|
let slot_in_ns = (SECONDS_PER_YEAR * 1_000_000_000.0) / slots_per_year;
|
|
|
|
Duration::from_nanos(slot_in_ns as u64)
|
|
|
|
}
|
|
|
|
|
2019-11-21 12:05:31 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_years_as_slots() {
|
2019-11-25 23:40:36 -08:00
|
|
|
let tick_duration = Duration::from_micros(1000 * 1000 / 160);
|
2019-11-21 12:05:31 -08:00
|
|
|
|
|
|
|
// interestingly large numbers with 160 ticks/second
|
|
|
|
assert_eq!(years_as_slots(0.0, &tick_duration, 4) as u64, 0);
|
|
|
|
assert_eq!(
|
|
|
|
years_as_slots(1.0 / 12f64, &tick_duration, 4) as u64,
|
2019-11-25 23:40:36 -08:00
|
|
|
105_189_753
|
2019-11-21 12:05:31 -08:00
|
|
|
);
|
2019-11-25 23:40:36 -08:00
|
|
|
assert_eq!(years_as_slots(1.0, &tick_duration, 4) as u64, 1_262_277_039);
|
2019-11-21 12:05:31 -08:00
|
|
|
|
2019-11-25 23:40:36 -08:00
|
|
|
let tick_duration = Duration::from_micros(1000 * 1000);
|
2019-11-21 12:05:31 -08:00
|
|
|
// one second in years with one tick per second + one tick per slot
|
|
|
|
assert_eq!(
|
|
|
|
years_as_slots(1.0 / SECONDS_PER_YEAR, &tick_duration, 1),
|
|
|
|
1.0
|
|
|
|
);
|
|
|
|
}
|
2019-11-25 23:40:36 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_slot_duration_from_slots_per_year() {
|
|
|
|
let slots_per_year = 1_262_277_039.0;
|
|
|
|
let ticks_per_slot = 4;
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
slot_duration_from_slots_per_year(slots_per_year),
|
|
|
|
Duration::from_micros(1000 * 1000 / 160) * ticks_per_slot
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
slot_duration_from_slots_per_year(0.0),
|
|
|
|
Duration::from_micros(0) * ticks_per_slot
|
|
|
|
);
|
|
|
|
|
|
|
|
let slots_per_year = SECONDS_PER_YEAR;
|
|
|
|
let ticks_per_slot = 1;
|
|
|
|
assert_eq!(
|
|
|
|
slot_duration_from_slots_per_year(slots_per_year),
|
|
|
|
Duration::from_millis(1000) * ticks_per_slot
|
|
|
|
);
|
|
|
|
}
|
2019-11-21 12:05:31 -08:00
|
|
|
}
|