Skip to content

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

Closed
@Boscop

Description

@Boscop

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
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions