Skip to content

Commit 1af6d58

Browse files
committed
Fix unit conversions in Duration::as_micros and as_millis.
1 parent 8bf107b commit 1af6d58

File tree

1 file changed

+64
-4
lines changed

1 file changed

+64
-4
lines changed

src/time/duration.rs

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ impl Duration {
4141
std::time::Duration::from_micros(micros).into()
4242
}
4343

44+
/// Creates a new `Duration` from the specified number of nanoseconds.
45+
#[must_use]
46+
#[inline]
47+
pub fn from_nanos(nanos: u64) -> Self {
48+
std::time::Duration::from_nanos(nanos).into()
49+
}
50+
4451
/// Creates a new `Duration` from the specified number of seconds represented
4552
/// as `f64`.
4653
///
@@ -78,17 +85,17 @@ impl Duration {
7885
self.0 / 1_000_000_000
7986
}
8087

81-
/// Returns the number of whole microseconds contained by this `Duration`.
88+
/// Returns the number of whole milliseconds contained by this `Duration`.
8289
#[must_use]
8390
#[inline]
84-
pub const fn as_micros(&self) -> u128 {
91+
pub const fn as_millis(&self) -> u128 {
8592
(self.0 / 1_000_000) as u128
8693
}
8794

88-
/// Returns the number of whole milliseconds contained by this `Duration`.
95+
/// Returns the number of whole microseconds contained by this `Duration`.
8996
#[must_use]
9097
#[inline]
91-
pub const fn as_millis(&self) -> u128 {
98+
pub const fn as_micros(&self) -> u128 {
9299
(self.0 / 1_000) as u128
93100
}
94101

@@ -168,3 +175,56 @@ impl IntoFuture for Duration {
168175
crate::task::sleep(self)
169176
}
170177
}
178+
179+
#[cfg(test)]
180+
mod tests {
181+
use super::*;
182+
183+
#[test]
184+
fn test_new_from_as() {
185+
assert_eq!(Duration::new(456, 864209753).as_secs(), 456);
186+
assert_eq!(Duration::new(456, 864209753).as_millis(), 456864);
187+
assert_eq!(Duration::new(456, 864209753).as_micros(), 456864209);
188+
assert_eq!(Duration::new(456, 864209753).as_nanos(), 456864209753);
189+
190+
assert_eq!(Duration::from_secs(9876543210).as_secs(), 9876543210);
191+
assert_eq!(Duration::from_secs(9876543210).as_millis(), 9876543210_000);
192+
assert_eq!(
193+
Duration::from_secs(9876543210).as_micros(),
194+
9876543210_000000
195+
);
196+
assert_eq!(
197+
Duration::from_secs(9876543210).as_nanos(),
198+
9876543210_000000000
199+
);
200+
201+
assert_eq!(Duration::from_millis(9876543210).as_secs(), 9876543);
202+
assert_eq!(Duration::from_millis(9876543210).as_millis(), 9876543210);
203+
assert_eq!(
204+
Duration::from_millis(9876543210).as_micros(),
205+
9876543210_000
206+
);
207+
assert_eq!(
208+
Duration::from_millis(9876543210).as_nanos(),
209+
9876543210_000000
210+
);
211+
212+
assert_eq!(Duration::from_micros(9876543210).as_secs(), 9876);
213+
assert_eq!(Duration::from_micros(9876543210).as_millis(), 9876543);
214+
assert_eq!(Duration::from_micros(9876543210).as_micros(), 9876543210);
215+
assert_eq!(Duration::from_micros(9876543210).as_nanos(), 9876543210_000);
216+
217+
assert_eq!(Duration::from_nanos(9876543210).as_secs(), 9);
218+
assert_eq!(Duration::from_nanos(9876543210).as_millis(), 9876);
219+
assert_eq!(Duration::from_nanos(9876543210).as_micros(), 9876543);
220+
assert_eq!(Duration::from_nanos(9876543210).as_nanos(), 9876543210);
221+
}
222+
223+
#[test]
224+
fn test_from_secs_float() {
225+
assert_eq!(Duration::from_secs_f64(158.9).as_secs(), 158);
226+
assert_eq!(Duration::from_secs_f32(158.9).as_secs(), 158);
227+
assert_eq!(Duration::from_secs_f64(159.1).as_secs(), 159);
228+
assert_eq!(Duration::from_secs_f32(159.1).as_secs(), 159);
229+
}
230+
}

0 commit comments

Comments
 (0)