1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#![allow(clippy::integer_arithmetic)]
use std::time::{Duration, SystemTime, UNIX_EPOCH};
pub fn duration_as_ns(d: &Duration) -> u64 {
d.as_secs() * 1_000_000_000 + u64::from(d.subsec_nanos())
}
pub fn duration_as_us(d: &Duration) -> u64 {
(d.as_secs() * 1000 * 1000) + (u64::from(d.subsec_nanos()) / 1_000)
}
pub fn duration_as_ms(d: &Duration) -> u64 {
(d.as_secs() * 1000) + (u64::from(d.subsec_nanos()) / 1_000_000)
}
pub fn duration_as_s(d: &Duration) -> f32 {
d.as_secs() as f32 + (d.subsec_nanos() as f32 / 1_000_000_000.0)
}
pub fn timestamp() -> u64 {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("create timestamp in timing");
duration_as_ms(&now)
}
pub const SECONDS_PER_YEAR: f64 = 365.242_199 * 24.0 * 60.0 * 60.0;
pub fn years_as_slots(years: f64, tick_duration: &Duration, ticks_per_slot: u64) -> f64 {
years *
SECONDS_PER_YEAR
*(1_000_000_000.0 / duration_as_ns(tick_duration) as f64)
/ ticks_per_slot as f64
}
pub fn slot_duration_from_slots_per_year(slots_per_year: f64) -> Duration {
let slot_in_ns = if slots_per_year != 0.0 {
(SECONDS_PER_YEAR * 1_000_000_000.0) / slots_per_year
} else {
0.0
};
Duration::from_nanos(slot_in_ns as u64)
}
#[cfg(test)]
mod test {
use super::*;
#[test]
#[allow(clippy::float_cmp)]
fn test_years_as_slots() {
let tick_duration = Duration::from_micros(1000 * 1000 / 160);
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,
105_189_753
);
assert_eq!(years_as_slots(1.0, &tick_duration, 4) as u64, 1_262_277_039);
let tick_duration = Duration::from_micros(1000 * 1000);
assert_eq!(
years_as_slots(1.0 / SECONDS_PER_YEAR, &tick_duration, 1),
1.0
);
}
#[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
);
}
}