Skip to content

Commit a9eff48

Browse files
Add from_nanos_u128 to Duration for handling large time spans
Add unstable attribute Add unstable attribute for `from_nanos_u128` with tracking issue #139201 Co-authored-by: Madhav Madhusoodanan <[email protected]> add feature gate for the new function - add feature gate - remove trailing whitespace Add feature gate at the right place Co-authored-by: Madhav Madhusoodanan <[email protected]> Update time.rs making sure seconds are in u64 Co-authored-by: Madhav Madhusoodanan <[email protected]> Update time.rs keeping seconds as u64 removing the use of non const function within const function Use a u128 example time.rs Earlier for the example I used 2.pow(64). correcting it to use u128 type which can contain the value Synched my fork Dummy commit dummy commit. sync local with fork style: format code with rustfmt Add panic test for Duration::from_nanos_u128 fix: remove non-const .into() call from const function A const function cannot call non-const functions, so we remove the trailing .into() call and use raw literals instead. remove paratheses Add comment on largest input and explain safety assumption Ran Rust fmt again
1 parent b8005bf commit a9eff48

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ dependencies = [
225225
"memchr",
226226
"serde",
227227
"serde_derive",
228-
"winnow 0.7.4",
228+
"winnow 0.7.6",
229229
]
230230

231231
[[package]]
@@ -6415,9 +6415,9 @@ dependencies = [
64156415

64166416
[[package]]
64176417
name = "winnow"
6418-
version = "0.7.4"
6418+
version = "0.7.6"
64196419
source = "registry+https://github.com/rust-lang/crates.io-index"
6420-
checksum = "0e97b544156e9bebe1a0ffbc03484fc1ffe3100cbce3ffb17eac35f7cdd7ab36"
6420+
checksum = "63d3fcd9bba44b03821e7d699eeee959f3126dcc4aa8e4ae18ec617c2a5cea10"
64216421
dependencies = [
64226422
"memchr",
64236423
]

library/core/src/time.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![stable(feature = "duration_core", since = "1.25.0")]
2-
32
//! Temporal quantification.
43
//!
54
//! # Examples:
@@ -308,6 +307,39 @@ impl Duration {
308307
Duration { secs, nanos: subsec_nanos }
309308
}
310309

310+
/// Creates a new Duration from the specified number of nanoseconds.
311+
///
312+
/// # Panics
313+
///
314+
/// Panics if the given number of nanoseconds is greater than what Duration can handle,
315+
/// which is `(u64::MAX * NANOS_PER_SEC) + NANOS_PER_SEC - 1`
316+
/// Use this function if you need to specify time greater than what can fit in u64
317+
/// (around 584 years).
318+
///
319+
/// # Examples
320+
///
321+
/// ```
322+
/// #![feature(duration_from_nanos_u128)]
323+
/// use std::time::Duration;
324+
/// let time_in_nanos = 2u128.pow(64);
325+
/// let duration = Duration::from_nanos_u128(time_in_nanos);
326+
/// ```
327+
#[unstable(feature = "duration_from_nanos_u128", issue = "139201")]
328+
#[must_use]
329+
#[inline]
330+
pub const fn from_nanos_u128(nanos: u128) -> Duration {
331+
const NANOS_PER_SEC: u128 = self::NANOS_PER_SEC as u128;
332+
let secs: u128 = nanos / NANOS_PER_SEC;
333+
if secs > u64::MAX as u128 {
334+
panic!("overflow in duration in Duration::from_nanos_u128");
335+
}
336+
let subsec_nanos = (nanos % NANOS_PER_SEC) as u32;
337+
// SAFETY: x % 1_000_000_000 < 1_000_000_000 also, subsec_nanos >= 0 since u128 >=0 and u32 >=0
338+
let subsec_nanos = unsafe { Nanoseconds::new_unchecked(subsec_nanos) };
339+
340+
Duration { secs: secs as u64, nanos: subsec_nanos }
341+
}
342+
311343
/// Creates a new `Duration` from the specified number of weeks.
312344
///
313345
/// # Panics

0 commit comments

Comments
 (0)