Language update

This commit is contained in:
Jisoo Park 2015-04-24 16:07:34 +09:00
parent eced570882
commit 14a44aef2b
5 changed files with 45 additions and 9 deletions

View File

@ -16,4 +16,4 @@ name = "chrono"
[dependencies] [dependencies]
time = "*" time = "*"
num = "*"

View File

@ -7,7 +7,7 @@
* They can be constructed incrementally while being checked for consistency. * They can be constructed incrementally while being checked for consistency.
*/ */
use std::num::{Int, ToPrimitive}; use num::traits::ToPrimitive;
use {Datelike, Timelike}; use {Datelike, Timelike};
use Weekday; use Weekday;

View File

@ -273,6 +273,7 @@ Advanced time zone handling is not yet supported (but is planned in 0.3).
#![deny(missing_docs)] #![deny(missing_docs)]
extern crate time as stdtime; extern crate time as stdtime;
extern crate num;
pub use duration::Duration; pub use duration::Duration;
pub use offset::{TimeZone, Offset, LocalResult}; pub use offset::{TimeZone, Offset, LocalResult};
@ -317,7 +318,7 @@ pub mod format;
/// ///
/// The order of the days of week depends on the context. /// The order of the days of week depends on the context.
/// One should prefer `*_from_monday` or `*_from_sunday` methods to get the correct result. /// One should prefer `*_from_monday` or `*_from_sunday` methods to get the correct result.
#[derive(PartialEq, Eq, Copy, Clone, FromPrimitive, Debug)] #[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum Weekday { pub enum Weekday {
/// Monday. /// Monday.
Mon = 0, Mon = 0,
@ -421,6 +422,38 @@ impl Weekday {
} }
} }
impl num::traits::FromPrimitive for Weekday {
#[inline]
fn from_i64(n: i64) -> Option<Weekday> {
match n {
0 => Some(Weekday::Mon),
1 => Some(Weekday::Tue),
2 => Some(Weekday::Wed),
3 => Some(Weekday::Thu),
4 => Some(Weekday::Fri),
5 => Some(Weekday::Sat),
6 => Some(Weekday::Sun),
_ => None,
}
}
#[inline]
fn from_u64(n: u64) -> Option<Weekday> {
match n {
0 => Some(Weekday::Mon),
1 => Some(Weekday::Tue),
2 => Some(Weekday::Wed),
3 => Some(Weekday::Thu),
4 => Some(Weekday::Fri),
5 => Some(Weekday::Sat),
6 => Some(Weekday::Sun),
_ => None,
}
}
}
/// The common set of methods for date component. /// The common set of methods for date component.
pub trait Datelike { pub trait Datelike {
/// Returns the year number. /// Returns the year number.

View File

@ -7,8 +7,8 @@
*/ */
use std::{str, fmt, hash}; use std::{str, fmt, hash};
use std::num::{Int, ToPrimitive};
use std::ops::{Add, Sub}; use std::ops::{Add, Sub};
use num::traits::ToPrimitive;
use {Weekday, Datelike}; use {Weekday, Datelike};
use div::div_mod_floor; use div::div_mod_floor;
@ -601,7 +601,9 @@ mod tests {
use {Datelike, Weekday}; use {Datelike, Weekday};
use duration::Duration; use duration::Duration;
use std::{i32, u32}; use std::{i32, u32};
use std::iter::{range_inclusive, range_step_inclusive};
// TODO replace with range notion and adapters
use num::iter::{range_inclusive, range_step_inclusive};
#[test] #[test]
fn test_date_from_ymd() { fn test_date_from_ymd() {
@ -1034,7 +1036,8 @@ mod tests {
*/ */
#[allow(dead_code)] // some internal methods have been left for consistency #[allow(dead_code)] // some internal methods have been left for consistency
mod internals { mod internals {
use std::{i32, num, fmt}; use std::{i32, fmt};
use num::traits::FromPrimitive;
use Weekday; use Weekday;
use div::{div_rem, mod_floor}; use div::{div_rem, mod_floor};
@ -1351,7 +1354,7 @@ mod internals {
#[inline] #[inline]
pub fn weekday(&self) -> Weekday { pub fn weekday(&self) -> Weekday {
let Of(of) = *self; let Of(of) = *self;
num::from_u32(((of >> 4) + (of & 0b111)) % 7).unwrap() Weekday::from_u32(((of >> 4) + (of & 0b111)) % 7).unwrap()
} }
#[inline] #[inline]
@ -1359,7 +1362,7 @@ mod internals {
// week ordinal = ordinal + delta // week ordinal = ordinal + delta
let Of(of) = *self; let Of(of) = *self;
let weekord = (of >> 4).wrapping_add(self.flags().isoweek_delta()); let weekord = (of >> 4).wrapping_add(self.flags().isoweek_delta());
(weekord / 7, num::from_u32(weekord % 7).unwrap()) (weekord / 7, Weekday::from_u32(weekord % 7).unwrap())
} }
#[inline] #[inline]

View File

@ -7,8 +7,8 @@
*/ */
use std::{str, fmt, hash}; use std::{str, fmt, hash};
use std::num::{Int, ToPrimitive};
use std::ops::{Add, Sub}; use std::ops::{Add, Sub};
use num::traits::ToPrimitive;
use {Weekday, Timelike, Datelike}; use {Weekday, Timelike, Datelike};
use div::div_mod_floor; use div::div_mod_floor;