How do I get a Duration as a number of milliseconds in Rust
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.
rust
add a comment |
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.
rust
In addition to the other answers, keep an eye on this RFC issue for any future RFCs regarding this.
– robinst
Jan 4 '17 at 1:34
use chrono exercism.io/submissions/1264089798d244e5b8278e588919901c
– rofrol
May 18 '18 at 14:04
See also How can I get the current time in milliseconds?
– Shepmaster
Dec 31 '18 at 14:28
add a comment |
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.
rust
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.
rust
rust
edited Apr 23 '16 at 20:57
Shepmaster
157k14316457
157k14316457
asked Apr 23 '16 at 20:32
Kevin BurkeKevin Burke
20.6k45132236
20.6k45132236
In addition to the other answers, keep an eye on this RFC issue for any future RFCs regarding this.
– robinst
Jan 4 '17 at 1:34
use chrono exercism.io/submissions/1264089798d244e5b8278e588919901c
– rofrol
May 18 '18 at 14:04
See also How can I get the current time in milliseconds?
– Shepmaster
Dec 31 '18 at 14:28
add a comment |
In addition to the other answers, keep an eye on this RFC issue for any future RFCs regarding this.
– robinst
Jan 4 '17 at 1:34
use chrono exercism.io/submissions/1264089798d244e5b8278e588919901c
– rofrol
May 18 '18 at 14:04
See also How can I get the current time in milliseconds?
– Shepmaster
Dec 31 '18 at 14:28
In addition to the other answers, keep an eye on this RFC issue for any future RFCs regarding this.
– robinst
Jan 4 '17 at 1:34
In addition to the other answers, keep an eye on this RFC issue for any future RFCs regarding this.
– robinst
Jan 4 '17 at 1:34
use chrono exercism.io/submissions/1264089798d244e5b8278e588919901c
– rofrol
May 18 '18 at 14:04
use chrono exercism.io/submissions/1264089798d244e5b8278e588919901c
– rofrol
May 18 '18 at 14:04
See also How can I get the current time in milliseconds?
– Shepmaster
Dec 31 '18 at 14:28
See also How can I get the current time in milliseconds?
– Shepmaster
Dec 31 '18 at 14:28
add a comment |
3 Answers
3
active
oldest
votes
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);
7
I'd rather multiply seconds by 1000, then addnanos/1000000. Less risk of overflow.
– llogiq
Apr 24 '16 at 10:22
subsec_nanosdoesn't return the number of elapsed nanoseconds. It represents the precision of the Duration.
– w.brian
Mar 13 '17 at 1:15
2
@w.brian the example in the documentation doc.rust-lang.org/std/time/… 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
add a comment |
Use time::Duration from the time crate on crates.io which provides a num_milliseconds() method.
add a comment |
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:
- How can I get the current time in milliseconds?
What does Rust 2018 have to do with the problem? Why are you usingSystemTimeinstead of directly using aDuration?
– Shepmaster
Dec 31 '18 at 14:25
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36816072%2fhow-do-i-get-a-duration-as-a-number-of-milliseconds-in-rust%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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);
7
I'd rather multiply seconds by 1000, then addnanos/1000000. Less risk of overflow.
– llogiq
Apr 24 '16 at 10:22
subsec_nanosdoesn't return the number of elapsed nanoseconds. It represents the precision of the Duration.
– w.brian
Mar 13 '17 at 1:15
2
@w.brian the example in the documentation doc.rust-lang.org/std/time/… 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
add a comment |
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);
7
I'd rather multiply seconds by 1000, then addnanos/1000000. Less risk of overflow.
– llogiq
Apr 24 '16 at 10:22
subsec_nanosdoesn't return the number of elapsed nanoseconds. It represents the precision of the Duration.
– w.brian
Mar 13 '17 at 1:15
2
@w.brian the example in the documentation doc.rust-lang.org/std/time/… 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
add a comment |
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);
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);
answered Apr 23 '16 at 20:50
Kevin BurkeKevin Burke
20.6k45132236
20.6k45132236
7
I'd rather multiply seconds by 1000, then addnanos/1000000. Less risk of overflow.
– llogiq
Apr 24 '16 at 10:22
subsec_nanosdoesn't return the number of elapsed nanoseconds. It represents the precision of the Duration.
– w.brian
Mar 13 '17 at 1:15
2
@w.brian the example in the documentation doc.rust-lang.org/std/time/… 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
add a comment |
7
I'd rather multiply seconds by 1000, then addnanos/1000000. Less risk of overflow.
– llogiq
Apr 24 '16 at 10:22
subsec_nanosdoesn't return the number of elapsed nanoseconds. It represents the precision of the Duration.
– w.brian
Mar 13 '17 at 1:15
2
@w.brian the example in the documentation doc.rust-lang.org/std/time/… 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
7
7
I'd rather multiply seconds by 1000, then add
nanos/1000000. Less risk of overflow.– llogiq
Apr 24 '16 at 10:22
I'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 1:15
subsec_nanos doesn't return the number of elapsed nanoseconds. It represents the precision of the Duration.– w.brian
Mar 13 '17 at 1:15
2
2
@w.brian the example in the documentation doc.rust-lang.org/std/time/… 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
@w.brian the example in the documentation doc.rust-lang.org/std/time/… 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
add a comment |
Use time::Duration from the time crate on crates.io which provides a num_milliseconds() method.
add a comment |
Use time::Duration from the time crate on crates.io which provides a num_milliseconds() method.
add a comment |
Use time::Duration from the time crate on crates.io which provides a num_milliseconds() method.
Use time::Duration from the time crate on crates.io which provides a num_milliseconds() method.
edited Apr 23 '16 at 21:01
answered Apr 23 '16 at 20:58
aeverisaeveris
335
335
add a comment |
add a comment |
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:
- How can I get the current time in milliseconds?
What does Rust 2018 have to do with the problem? Why are you usingSystemTimeinstead of directly using aDuration?
– Shepmaster
Dec 31 '18 at 14:25
add a comment |
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:
- How can I get the current time in milliseconds?
What does Rust 2018 have to do with the problem? Why are you usingSystemTimeinstead of directly using aDuration?
– Shepmaster
Dec 31 '18 at 14:25
add a comment |
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:
- How can I get the current time in milliseconds?
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:
- How can I get the current time in milliseconds?
edited Jan 1 at 16:39
Shepmaster
157k14316457
157k14316457
answered Dec 31 '18 at 10:49
iceblueiceblue
8916
8916
What does Rust 2018 have to do with the problem? Why are you usingSystemTimeinstead of directly using aDuration?
– Shepmaster
Dec 31 '18 at 14:25
add a comment |
What does Rust 2018 have to do with the problem? Why are you usingSystemTimeinstead of directly using aDuration?
– Shepmaster
Dec 31 '18 at 14:25
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
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
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36816072%2fhow-do-i-get-a-duration-as-a-number-of-milliseconds-in-rust%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown

In addition to the other answers, keep an eye on this RFC issue for any future RFCs regarding this.
– robinst
Jan 4 '17 at 1:34
use chrono exercism.io/submissions/1264089798d244e5b8278e588919901c
– rofrol
May 18 '18 at 14:04
See also How can I get the current time in milliseconds?
– Shepmaster
Dec 31 '18 at 14:28