I have a time::Duration
. How can I get the number of milliseconds represented by this duration as an integer? There used to be a num_milliseconds()
function, but it is no longer available.
Asked
Active
Viewed 1.2k times
15

Shepmaster
- 388,571
- 95
- 1,107
- 1,366

Kevin Burke
- 61,194
- 76
- 188
- 305
-
In addition to the other answers, keep an eye on [this RFC issue](https://github.com/rust-lang/rfcs/issues/1545) for any future RFCs regarding this. – robinst Jan 04 '17 at 01:34
-
use chrono http://exercism.io/submissions/1264089798d244e5b8278e588919901c – rofrol May 18 '18 at 14:04
-
See also [How can I get the current time in milliseconds?](https://stackoverflow.com/q/26593387/155423) – Shepmaster Dec 31 '18 at 14:28
-
As of today, I could successfully use num_milliseconds. – Ray Hulha Apr 09 '21 at 23:04
3 Answers
6
Since Rust 1.33.0, there is an as_millis()
function:
use std::time::SystemTime;
fn main() {
let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).expect("get millis error");
println!("now millis: {}", now.as_millis());
}
Since Rust 1.27.0, there is a subsec_millis()
function:
use std::time::SystemTime;
fn main() {
let since_the_epoch = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).expect("get millis error");
let seconds = since_the_epoch.as_secs();
let subsec_millis = since_the_epoch.subsec_millis() as u64;
println!("now millis: {}", seconds * 1000 + subsec_millis);
}
Since Rust 1.8, there is a subsec_nanos
function:
let in_ms = since_the_epoch.as_secs() * 1000 +
since_the_epoch.subsec_nanos() as u64 / 1_000_000;
See also:

Shepmaster
- 388,571
- 95
- 1,107
- 1,366

iceqing
- 955
- 11
- 12
-
What does Rust 2018 have to do with the problem? Why are you using `SystemTime` instead of directly using a `Duration`? – Shepmaster Dec 31 '18 at 14:25
-
New to Rust; what advantage do the solutions above have over the simpler: `duration.as_secs_f64() * 1000f64`? It's much more precise than `duration.as_millis()` (ie. it gives me results like `6.716969ms` rather than just an integer), while being simpler than the other two options. – Venryx Mar 28 '22 at 16:21
4
Here is the solution I came up with, which is to multiply the seconds by a billion, add it to the nanoseconds, then divide by 1e6.
let nanos = timeout_duration.subsec_nanos() as u64;
let ms = (1000*1000*1000 * timeout_duration.as_secs() + nanos)/(1000 * 1000);

Kevin Burke
- 61,194
- 76
- 188
- 305
-
7I'd rather multiply seconds by 1000, then add `nanos/1000000`. Less risk of overflow. – llogiq Apr 24 '16 at 10:22
-
`subsec_nanos` doesn't return the number of elapsed nanoseconds. It represents the precision of the Duration. – w.brian Mar 13 '17 at 01:15
-
2@w.brian the example in the documentation https://doc.rust-lang.org/std/time/struct.Duration.html#method.subsec_nanos supports the facts that the method returns the fractional part of the duration in nanoseconds, so the answer seems correct to me. – poros Jan 30 '18 at 18:32
2
Use time::Duration
from the time crate on crates.io which provides a num_milliseconds()
method.

aeveris
- 39
- 5