Skip to content

Duration::as_msecs(), as_microsecs(), as_nanosecs() #50243

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
Boscop opened this issue Apr 26, 2018 · 3 comments
Closed

Duration::as_msecs(), as_microsecs(), as_nanosecs() #50243

Boscop opened this issue Apr 26, 2018 · 3 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@Boscop
Copy link

Boscop commented Apr 26, 2018

I often find myself needing this extension to std's Duration, why isn't something like this part of std? :)

pub trait DurationExt {
	fn as_msecs(&self) -> u64;
	fn as_microsecs(&self) -> u64;
	fn as_nanosecs(&self) -> u64;
}

impl DurationExt for Duration {
	fn as_msecs(&self) -> u64 {
		self.as_secs() * 1_000 + (self.subsec_nanos() as f64 / 1_000_000.0).round() as u64
	}
	fn as_microsecs(&self) -> u64 {
		self.as_secs() * 1_000_000 + (self.subsec_nanos() as f64 / 1_000.0).round() as u64
	}
	fn as_nanosecs(&self) -> u64 {
		self.as_secs() * 1_000_000_000 + self.subsec_nanos() as u64
	}
}
@hellow554
Copy link
Contributor

hellow554 commented Apr 26, 2018

You may get a problem with this.
Because you can use a u64 for seconds you can overflow as_msecs by using u64::max_value() and then you would cause an overflow/panic/destroy of the universe.
I guess, that's why there is subsec_millis to avoid this problem (which was btw. merged 8 days ago :) )

@pietroalbini pietroalbini added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Apr 26, 2018
@est31
Copy link
Member

est31 commented Apr 26, 2018

#50202 #50167

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants