diff options
| author | Steven Fackler <sfackler@gmail.com> | 2015-07-05 23:20:00 -0700 |
|---|---|---|
| committer | Steven Fackler <sfackler@gmail.com> | 2015-08-10 20:04:18 -0400 |
| commit | 999bdeca88a06938ac1e1c608091d3afe4d7e173 (patch) | |
| tree | 57274f8e644a2823864cd2d10f938719b0ec9fa2 /src/libstd/time | |
| parent | af32c015aa6fa33cbbc2986e7f72e5c83f242c35 (diff) | |
| download | rust-999bdeca88a06938ac1e1c608091d3afe4d7e173.tar.gz rust-999bdeca88a06938ac1e1c608091d3afe4d7e173.zip | |
Stabilize the Duration API
This commit stabilizes the `std::time` module and the `Duration` type. `Duration::span` remains unstable, and the `Display` implementation for `Duration` has been removed as it is still being reworked and all trait implementations for stable types are de facto stable. This is a [breaking-change] to those using `Duration`'s `Display` implementation.
Diffstat (limited to 'src/libstd/time')
| -rw-r--r-- | src/libstd/time/duration.rs | 67 | ||||
| -rw-r--r-- | src/libstd/time/mod.rs | 2 |
2 files changed, 21 insertions, 48 deletions
diff --git a/src/libstd/time/duration.rs b/src/libstd/time/duration.rs index 28eaed40da8..ee287484256 100644 --- a/src/libstd/time/duration.rs +++ b/src/libstd/time/duration.rs @@ -8,14 +8,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -//! Temporal quantification - -#![unstable(feature = "duration", reason = "recently added API per RFC 1040")] - #[cfg(stage0)] use prelude::v1::*; -use fmt; use ops::{Add, Sub, Mul, Div}; use sys::time::SteadyTime; @@ -43,11 +38,12 @@ const MILLIS_PER_SEC: u64 = 1_000; /// let five_seconds = Duration::new(5, 0); /// let five_seconds_and_five_nanos = five_seconds + Duration::new(0, 5); /// -/// assert_eq!(five_seconds_and_five_nanos.secs(), 5); -/// assert_eq!(five_seconds_and_five_nanos.extra_nanos(), 5); +/// assert_eq!(five_seconds_and_five_nanos.as_secs(), 5); +/// assert_eq!(five_seconds_and_five_nanos.subsec_nanos(), 5); /// /// let ten_millis = Duration::from_millis(10); /// ``` +#[stable(feature = "duration", since = "1.3.0")] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)] pub struct Duration { secs: u64, @@ -60,6 +56,7 @@ impl Duration { /// /// If the nanoseconds is greater than 1 billion (the number of nanoseconds /// in a second), then it will carry over into the seconds provided. + #[stable(feature = "duration", since = "1.3.0")] pub fn new(secs: u64, nanos: u32) -> Duration { let secs = secs + (nanos / NANOS_PER_SEC) as u64; let nanos = nanos % NANOS_PER_SEC; @@ -79,11 +76,13 @@ impl Duration { } /// Creates a new `Duration` from the specified number of seconds. + #[stable(feature = "duration", since = "1.3.0")] pub fn from_secs(secs: u64) -> Duration { Duration { secs: secs, nanos: 0 } } /// Creates a new `Duration` from the specified number of milliseconds. + #[stable(feature = "duration", since = "1.3.0")] pub fn from_millis(millis: u64) -> Duration { let secs = millis / MILLIS_PER_SEC; let nanos = ((millis % MILLIS_PER_SEC) as u32) * NANOS_PER_MILLI; @@ -94,14 +93,16 @@ impl Duration { /// /// The extra precision represented by this duration is ignored (e.g. extra /// nanoseconds are not represented in the returned value). - pub fn secs(&self) -> u64 { self.secs } + #[stable(feature = "duration", since = "1.3.0")] + pub fn as_secs(&self) -> u64 { self.secs } /// Returns the nanosecond precision represented by this duration. /// /// This method does **not** return the length of the duration when /// represented by nanoseconds. The returned number always represents a /// fractional portion of a second (e.g. it is less than one billion). - pub fn extra_nanos(&self) -> u32 { self.nanos } + #[stable(feature = "duration", since = "1.3.0")] + pub fn subsec_nanos(&self) -> u32 { self.nanos } } impl Add for Duration { @@ -167,20 +168,6 @@ impl Div<u32> for Duration { } } -impl fmt::Display for Duration { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match (self.secs, self.nanos) { - (s, 0) => write!(f, "{}s", s), - (0, n) if n % NANOS_PER_MILLI == 0 => write!(f, "{}ms", - n / NANOS_PER_MILLI), - (0, n) if n % 1_000 == 0 => write!(f, "{}µs", n / 1_000), - (0, n) => write!(f, "{}ns", n), - (s, n) => write!(f, "{}.{}s", s, - format!("{:09}", n).trim_right_matches('0')) - } - } -} - #[cfg(test)] mod tests { use prelude::v1::*; @@ -198,20 +185,20 @@ mod tests { #[test] fn secs() { - assert_eq!(Duration::new(0, 0).secs(), 0); - assert_eq!(Duration::from_secs(1).secs(), 1); - assert_eq!(Duration::from_millis(999).secs(), 0); - assert_eq!(Duration::from_millis(1001).secs(), 1); + assert_eq!(Duration::new(0, 0).as_secs(), 0); + assert_eq!(Duration::from_secs(1).as_secs(), 1); + assert_eq!(Duration::from_millis(999).as_secs(), 0); + assert_eq!(Duration::from_millis(1001).as_secs(), 1); } #[test] fn nanos() { - assert_eq!(Duration::new(0, 0).extra_nanos(), 0); - assert_eq!(Duration::new(0, 5).extra_nanos(), 5); - assert_eq!(Duration::new(0, 1_000_000_001).extra_nanos(), 1); - assert_eq!(Duration::from_secs(1).extra_nanos(), 0); - assert_eq!(Duration::from_millis(999).extra_nanos(), 999 * 1_000_000); - assert_eq!(Duration::from_millis(1001).extra_nanos(), 1 * 1_000_000); + assert_eq!(Duration::new(0, 0).subsec_nanos(), 0); + assert_eq!(Duration::new(0, 5).subsec_nanos(), 5); + assert_eq!(Duration::new(0, 1_000_000_001).subsec_nanos(), 1); + assert_eq!(Duration::from_secs(1).subsec_nanos(), 0); + assert_eq!(Duration::from_millis(999).subsec_nanos(), 999 * 1_000_000); + assert_eq!(Duration::from_millis(1001).subsec_nanos(), 1 * 1_000_000); } #[test] @@ -258,18 +245,4 @@ mod tests { assert_eq!(Duration::new(99, 999_999_000) / 100, Duration::new(0, 999_999_990)); } - - #[test] - fn display() { - assert_eq!(Duration::new(0, 2).to_string(), "2ns"); - assert_eq!(Duration::new(0, 2_000_000).to_string(), "2ms"); - assert_eq!(Duration::new(2, 0).to_string(), "2s"); - assert_eq!(Duration::new(2, 2).to_string(), "2.000000002s"); - assert_eq!(Duration::new(2, 2_000_000).to_string(), - "2.002s"); - assert_eq!(Duration::new(0, 2_000_002).to_string(), - "2000002ns"); - assert_eq!(Duration::new(2, 2_000_002).to_string(), - "2.002000002s"); - } } diff --git a/src/libstd/time/mod.rs b/src/libstd/time/mod.rs index d535b195519..29df882a042 100644 --- a/src/libstd/time/mod.rs +++ b/src/libstd/time/mod.rs @@ -10,7 +10,7 @@ //! Temporal quantification. -#![unstable(feature = "time")] +#![stable(feature = "time", since = "1.3.0")] pub use self::duration::Duration; |
