Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add std::time::Duration::{from_days, from_hours, from_mins} #47097

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/libstd/time/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,63 @@ impl Duration {
Duration { secs: secs, nanos: nanos }
}

/// Creates a new `Duration` from the specified number of whole days.
///
/// # Examples
///
/// ```
/// #![feature(duration_from_hours)]
/// use std::time::Duration;
///
/// let duration = Duration::from_days(1);
///
/// assert_eq!(86_400, duration.as_secs());
/// assert_eq!(0, duration.subsec_nanos());
/// ```
#[unstable(feature = "duration_from_hours", issue = "47097")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we generally create a separate (non-PR) tracking issue for new features. I think you can leave it like this for now, but if we do merge this, then I think we'll want to create a tracking issue and then update the PR.

Is that right @alexcrichton?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I note that this feature name is duration_from_hours, which is the same as the from_hours method but different from the name duration_from_mins used for the from_mins method.

I suspect you probably just want to use one feature name for all of them (duration_from perhaps?).

#[inline]
pub fn from_days(days: u64) -> Duration {
Duration { secs: 86_400*days, nanos: 0 }
}

/// Creates a new `Duration` from the specified number of whole hours.
///
/// # Examples
///
/// ```
/// #![feature(duration_from_hours)]
/// use std::time::Duration;
///
/// let duration = Duration::from_hours(2);
///
/// assert_eq!(7200, duration.as_secs());
/// assert_eq!(0, duration.subsec_nanos());
/// ```
#[unstable(feature = "duration_from_hours", issue = "47097")]
#[inline]
pub fn from_hours(hours: u64) -> Duration {
Duration { secs: 3600*hours, nanos: 0 }
}

/// Creates a new `Duration` from the specified number of whole minutes.
///
/// # Examples
///
/// ```
/// #![feature(duration_from_mins)]
/// use std::time::Duration;
///
/// let duration = Duration::from_mins(5);
///
/// assert_eq!(300, duration.as_secs());
/// assert_eq!(0, duration.subsec_nanos());
/// ```
#[unstable(feature = "duration_from_mins", issue = "47097")]
#[inline]
pub fn from_mins(mins: u64) -> Duration {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd rather this be called from_minutes.

Duration { secs: 60*mins, nanos: 0 }
}

/// Creates a new `Duration` from the specified number of whole seconds.
///
/// # Examples
Expand Down