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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/// Structure representing a duration in time
/// This can be used to add or subtract times from
/// an `Instant` object.
///
/// Duration is represented natively in microseconds.
#[derive(Clone, Copy, Debug)]
pub struct Duration {
pub usec: i64,
}
impl Duration {
/// Create a new duration object
///
/// # Arguments
/// * `usec` - The duration in microseconds
///
/// # Returns
/// A new `Duration` object
pub const fn new(usec: i64) -> Self {
Self { usec }
}
/// Create a new duration object from seconds
///
/// # Arguments
/// * `seconds` - The duration in seconds
///
/// # Returns
/// A new `Duration` object representing the time interval in seconds
pub fn from_seconds(seconds: f64) -> Self {
Self {
usec: (seconds * 1_000_000.0) as i64,
}
}
/// Create a new duration object from minutes
///
/// # Arguments
/// * `minutes` - The duration in minutes
///
/// # Returns
/// A new `Duration` object representing the time interval in minutes
pub fn from_minutes(minutes: f64) -> Self {
Self {
usec: (minutes * 60_000_000.0) as i64,
}
}
/// Create a new duration object from microseconds
///
/// # Arguments
/// * `usec` - The duration in microseconds
///
/// # Returns
/// A new `Duration` object representing the time interval in microseconds
pub const fn from_microseconds(usec: i64) -> Self {
Self { usec }
}
/// Create a new duration object from milliseconds
///
/// # Arguments
/// * `milliseconds` - The duration in milliseconds
///
/// # Returns
/// A new `Duration` object representing the time interval in milliseconds
pub fn from_milliseconds(milliseconds: f64) -> Self {
Self {
usec: (milliseconds * 1_000.0) as i64,
}
}
/// Create a new duration object from days
///
/// # Arguments
/// * `days` - The duration in days
///
/// # Returns
/// A new `Duration` object representing the time interval in days
pub fn from_days(days: f64) -> Self {
Self {
usec: (days * 86_400_000_000.0) as i64,
}
}
/// Create a new duration object from hours
///
/// # Arguments
/// * `hours` - The duration in hours
///
/// # Returns
/// A new `Duration` object representing the time interval in hours
pub fn from_hours(hours: f64) -> Self {
Self {
usec: (hours * 3_600_000_000.0) as i64,
}
}
/// A zero-duration object
/// # Returns
/// A `Duration` object representing zero time
pub const fn zero() -> Self {
Self { usec: 0 }
}
/// Represent duration as days
///
/// # Returns
/// The duration in days
pub fn as_days(&self) -> f64 {
self.usec as f64 / 86_400_000_000.0
}
/// Represent duration as seconds
///
/// # Returns
/// The duration in seconds
pub fn as_seconds(&self) -> f64 {
self.usec as f64 / 1_000_000.0
}
/// Represent duration as hours
///
/// # Returns
/// The duration in hours
pub fn as_hours(&self) -> f64 {
self.usec as f64 / 3_600_000_000.0
}
/// Represent duration as minutes
///
/// # Returns
/// The duration in minutes
pub fn as_minutes(&self) -> f64 {
self.usec as f64 / 60_000_000.0
}
/// Represent duration as microseconds
///
/// # Returns
/// The duration in microseconds
pub const fn as_microseconds(&self) -> i64 {
self.usec
}
}
impl std::fmt::Display for Duration {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if self.usec < 1_000_000 {
return write!(f, "{} usec", self.usec);
} else {
let mut rem = self.usec;
if rem >= 86_400_000_000 {
let days = rem / 86_400_000_000;
write!(f, "{} days ", days)?;
rem -= days * 86_400_000_000;
}
if rem >= 3_600_000_000 {
let hours = rem / 3_600_000_000;
write!(f, "{} hours ", hours)?;
rem -= hours * 3_600_000_000;
}
if rem >= 60_000_000 {
let minutes = rem / 60_000_000;
write!(f, "{} minutes ", minutes)?;
rem -= minutes * 60_000_000;
}
write!(
f,
"{:.6} seconds",
rem as f64 / 1_000_000.0
)?;
}
Ok(())
}
}