std: Refactor time module a bit

Put `Duration` in `time::duration`, where the two constants can
be called just `MAX` and `MIN`. Reexport from `time`.
This provides more room for the time module to expand.
This commit is contained in:
Brian Anderson 2014-07-31 17:24:54 -07:00
parent 4475e6a095
commit 77cdaf018c
2 changed files with 30 additions and 15 deletions

View File

@ -44,9 +44,9 @@ pub struct Duration {
}
/// The minimum possible `Duration`.
pub static MIN_DURATION: Duration = Duration { days: MIN_DAYS, secs: 0, nanos: 0 };
pub static MIN: Duration = Duration { days: MIN_DAYS, secs: 0, nanos: 0 };
/// The maximum possible `Duration`.
pub static MAX_DURATION: Duration = Duration { days: MAX_DAYS, secs: SECS_PER_DAY as u32 - 1,
pub static MAX: Duration = Duration { days: MAX_DAYS, secs: SECS_PER_DAY as u32 - 1,
nanos: NANOS_PER_SEC as u32 - 1 };
impl Duration {
@ -274,8 +274,8 @@ impl Duration {
}
impl num::Bounded for Duration {
#[inline] fn min_value() -> Duration { MIN_DURATION }
#[inline] fn max_value() -> Duration { MAX_DURATION }
#[inline] fn min_value() -> Duration { MIN }
#[inline] fn max_value() -> Duration { MAX }
}
impl num::Zero for Duration {
@ -496,7 +496,7 @@ fn div_rem_64(this: i64, other: i64) -> (i64, i64) {
#[cfg(test)]
mod tests {
use super::{Duration, MIN_DAYS, MAX_DAYS, MIN_DURATION, MAX_DURATION};
use super::{Duration, MIN_DAYS, MAX_DAYS, MIN, MAX};
use {i32, i64};
use num::{Zero, CheckedAdd, CheckedSub};
use option::{Some, None};
@ -533,8 +533,8 @@ mod tests {
assert_eq!(Duration::new(-1, -2, -3_004_005).num_days(), -1);
assert_eq!(Duration::days(i32::MAX).num_days(), i32::MAX);
assert_eq!(Duration::days(i32::MIN).num_days(), i32::MIN);
assert_eq!(MAX_DURATION.num_days(), MAX_DAYS);
assert_eq!(MIN_DURATION.num_days(), MIN_DAYS);
assert_eq!(MAX.num_days(), MAX_DAYS);
assert_eq!(MIN.num_days(), MIN_DAYS);
}
#[test]
@ -551,8 +551,8 @@ mod tests {
assert_eq!(Duration::new(-1, -2, -3_004_005).num_seconds(), -86402);
assert_eq!(Duration::seconds(i32::MAX).num_seconds(), i32::MAX as i64);
assert_eq!(Duration::seconds(i32::MIN).num_seconds(), i32::MIN as i64);
assert_eq!(MAX_DURATION.num_seconds(), (MAX_DAYS as i64 + 1) * 86400 - 1);
assert_eq!(MIN_DURATION.num_seconds(), MIN_DAYS as i64 * 86400);
assert_eq!(MAX.num_seconds(), (MAX_DAYS as i64 + 1) * 86400 - 1);
assert_eq!(MIN.num_seconds(), MIN_DAYS as i64 * 86400);
}
#[test]
@ -569,8 +569,8 @@ mod tests {
assert_eq!(Duration::new(-1, -2, -3_004_005).num_milliseconds(), -86402_003);
assert_eq!(Duration::milliseconds(i32::MAX).num_milliseconds(), i32::MAX as i64);
assert_eq!(Duration::milliseconds(i32::MIN).num_milliseconds(), i32::MIN as i64);
assert_eq!(MAX_DURATION.num_milliseconds(), (MAX_DAYS as i64 + 1) * 86400_000 - 1);
assert_eq!(MIN_DURATION.num_milliseconds(), MIN_DAYS as i64 * 86400_000);
assert_eq!(MAX.num_milliseconds(), (MAX_DAYS as i64 + 1) * 86400_000 - 1);
assert_eq!(MIN.num_milliseconds(), MIN_DAYS as i64 * 86400_000);
}
#[test]
@ -587,8 +587,8 @@ mod tests {
assert_eq!(Duration::new(-1, -2, -3_004_005).num_microseconds(), Some(-86402_003_004));
assert_eq!(Duration::microseconds(i32::MAX).num_microseconds(), Some(i32::MAX as i64));
assert_eq!(Duration::microseconds(i32::MIN).num_microseconds(), Some(i32::MIN as i64));
assert_eq!(MAX_DURATION.num_microseconds(), None);
assert_eq!(MIN_DURATION.num_microseconds(), None);
assert_eq!(MAX.num_microseconds(), None);
assert_eq!(MIN.num_microseconds(), None);
// overflow checks
static MICROS_PER_DAY: i64 = 86400_000_000;
@ -610,8 +610,8 @@ mod tests {
assert_eq!(Duration::new(-1, -2, -3_004_005).num_nanoseconds(), Some(-86402_003_004_005));
assert_eq!(Duration::nanoseconds(i32::MAX).num_nanoseconds(), Some(i32::MAX as i64));
assert_eq!(Duration::nanoseconds(i32::MIN).num_nanoseconds(), Some(i32::MIN as i64));
assert_eq!(MAX_DURATION.num_nanoseconds(), None);
assert_eq!(MIN_DURATION.num_nanoseconds(), None);
assert_eq!(MAX.num_nanoseconds(), None);
assert_eq!(MIN.num_nanoseconds(), None);
// overflow checks
static NANOS_PER_DAY: i64 = 86400_000_000_000;

15
src/libstd/time/mod.rs Normal file
View File

@ -0,0 +1,15 @@
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Temporal quantification.
pub use self::duration::Duration;
pub mod duration;