Skip to content

Commit 3641daf

Browse files
Rollup merge of #163138 - sorairolake:feature/more-duration-constants, r=clarfonthey
feat(time): add `Duration::{WEEK, DAY, HOUR, MINUTE}` - Tracking issue: #57391 This pull request adds associated constants for representing one week, one day, one hour, or one minute to `Duration`. In the same way as #162195 and #162199, the exact number of seconds corresponding to one week, one day, one hour, and one minute are clearly specified in the documentation. Documentation tests are used to verify the correctness of these descriptions. I initially proposed this as an ACP. However, the `duration_constants` feature is still unstable, and the ACP simply adds associated constants for units larger than the second to the feature, I determined that it could be incorporated into the feature. By making `Duration::WEEK` and `Duration::DAY` dependent on the `duration_constructors` feature, these associated constants can be removed if `Duration::from_weeks` and `Duration::from_days` are not stabilized. Therefore, even if the `duration_constructors` feature is not stabilized, the `duration_constants` feature can be stabilized. ```rust impl Duration { pub const WEEK: Duration = Duration::from_weeks(1); pub const DAY: Duration = Duration::from_days(1); pub const HOUR: Duration = Duration::from_hours(1); pub const MINUTE: Duration = Duration::from_mins(1); } ``` - See also: rust-lang/libs-team#869
2 parents beaba52 + 99ad525 commit 3641daf

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

‎library/core/src/time.rs‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,86 @@ impl Duration {
136136
#[unstable(feature = "duration_constants", issue = "57391")]
137137
pub const NANOSECOND: Duration = Duration::from_nanos(1);
138138

139+
/// The duration of one week.
140+
///
141+
/// For this constant, one week is defined as 7 days, or 604,800 seconds.
142+
///
143+
/// # Examples
144+
///
145+
/// ```
146+
/// #![feature(duration_constants)]
147+
/// #![feature(duration_constructors)]
148+
/// use std::time::Duration;
149+
///
150+
/// let week = Duration::WEEK;
151+
///
152+
/// assert_eq!(week, Duration::from_weeks(1));
153+
/// assert_eq!(week.as_secs(), 604_800);
154+
/// assert_eq!(week.subsec_nanos(), 0);
155+
/// ```
156+
#[unstable(feature = "duration_constants", issue = "57391")]
157+
// Also, #[unstable(feature = "duration_constructors", issue = "120301")]
158+
pub const WEEK: Duration = Duration::from_weeks(1);
159+
160+
/// The duration of one day.
161+
///
162+
/// For this constant, one day is defined as 24 hours, or 86,400 seconds.
163+
///
164+
/// # Examples
165+
///
166+
/// ```
167+
/// #![feature(duration_constants)]
168+
/// #![feature(duration_constructors)]
169+
/// use std::time::Duration;
170+
///
171+
/// let day = Duration::DAY;
172+
///
173+
/// assert_eq!(day, Duration::from_days(1));
174+
/// assert_eq!(day.as_secs(), 86400);
175+
/// assert_eq!(day.subsec_nanos(), 0);
176+
/// ```
177+
#[unstable(feature = "duration_constants", issue = "57391")]
178+
// Also, #[unstable(feature = "duration_constructors", issue = "120301")]
179+
pub const DAY: Duration = Duration::from_days(1);
180+
181+
/// The duration of one hour.
182+
///
183+
/// For this constant, one hour is defined as 60 minutes, or 3,600 seconds.
184+
///
185+
/// # Examples
186+
///
187+
/// ```
188+
/// #![feature(duration_constants)]
189+
/// use std::time::Duration;
190+
///
191+
/// let hour = Duration::HOUR;
192+
///
193+
/// assert_eq!(hour, Duration::from_hours(1));
194+
/// assert_eq!(hour.as_secs(), 3600);
195+
/// assert_eq!(hour.subsec_nanos(), 0);
196+
/// ```
197+
#[unstable(feature = "duration_constants", issue = "57391")]
198+
pub const HOUR: Duration = Duration::from_hours(1);
199+
200+
/// The duration of one minute.
201+
///
202+
/// For this constant, one minute is defined as 60 seconds.
203+
///
204+
/// # Examples
205+
///
206+
/// ```
207+
/// #![feature(duration_constants)]
208+
/// use std::time::Duration;
209+
///
210+
/// let minute = Duration::MINUTE;
211+
///
212+
/// assert_eq!(minute, Duration::from_mins(1));
213+
/// assert_eq!(minute.as_secs(), 60);
214+
/// assert_eq!(minute.subsec_nanos(), 0);
215+
/// ```
216+
#[unstable(feature = "duration_constants", issue = "57391")]
217+
pub const MINUTE: Duration = Duration::from_mins(1);
218+
139219
/// A duration of zero time.
140220
///
141221
/// # Examples

0 commit comments

Comments
 (0)