From 14a44aef2b6fc9771b748d70e6598f95c91057b6 Mon Sep 17 00:00:00 2001 From: Jisoo Park Date: Fri, 24 Apr 2015 16:07:34 +0900 Subject: [PATCH] Language update --- Cargo.toml | 2 +- src/format/parsed.rs | 2 +- src/lib.rs | 35 ++++++++++++++++++++++++++++++++++- src/naive/date.rs | 13 ++++++++----- src/naive/datetime.rs | 2 +- 5 files changed, 45 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ba0ea56..ae2f0c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,4 +16,4 @@ name = "chrono" [dependencies] time = "*" - +num = "*" diff --git a/src/format/parsed.rs b/src/format/parsed.rs index 4825b6b..f3ffe3c 100644 --- a/src/format/parsed.rs +++ b/src/format/parsed.rs @@ -7,7 +7,7 @@ * They can be constructed incrementally while being checked for consistency. */ -use std::num::{Int, ToPrimitive}; +use num::traits::ToPrimitive; use {Datelike, Timelike}; use Weekday; diff --git a/src/lib.rs b/src/lib.rs index 6c24fc5..6d5f831 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -273,6 +273,7 @@ Advanced time zone handling is not yet supported (but is planned in 0.3). #![deny(missing_docs)] extern crate time as stdtime; +extern crate num; pub use duration::Duration; pub use offset::{TimeZone, Offset, LocalResult}; @@ -317,7 +318,7 @@ pub mod format; /// /// 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. -#[derive(PartialEq, Eq, Copy, Clone, FromPrimitive, Debug)] +#[derive(PartialEq, Eq, Copy, Clone, Debug)] pub enum Weekday { /// Monday. Mon = 0, @@ -421,6 +422,38 @@ impl Weekday { } } +impl num::traits::FromPrimitive for Weekday { + + #[inline] + fn from_i64(n: i64) -> Option { + 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 { + 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. pub trait Datelike { /// Returns the year number. diff --git a/src/naive/date.rs b/src/naive/date.rs index 746384c..6531fd9 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -7,8 +7,8 @@ */ use std::{str, fmt, hash}; -use std::num::{Int, ToPrimitive}; use std::ops::{Add, Sub}; +use num::traits::ToPrimitive; use {Weekday, Datelike}; use div::div_mod_floor; @@ -601,7 +601,9 @@ mod tests { use {Datelike, Weekday}; use duration::Duration; 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] fn test_date_from_ymd() { @@ -1034,7 +1036,8 @@ mod tests { */ #[allow(dead_code)] // some internal methods have been left for consistency mod internals { - use std::{i32, num, fmt}; + use std::{i32, fmt}; + use num::traits::FromPrimitive; use Weekday; use div::{div_rem, mod_floor}; @@ -1351,7 +1354,7 @@ mod internals { #[inline] pub fn weekday(&self) -> Weekday { let Of(of) = *self; - num::from_u32(((of >> 4) + (of & 0b111)) % 7).unwrap() + Weekday::from_u32(((of >> 4) + (of & 0b111)) % 7).unwrap() } #[inline] @@ -1359,7 +1362,7 @@ mod internals { // week ordinal = ordinal + delta let Of(of) = *self; 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] diff --git a/src/naive/datetime.rs b/src/naive/datetime.rs index 3a5d5eb..acecc13 100644 --- a/src/naive/datetime.rs +++ b/src/naive/datetime.rs @@ -7,8 +7,8 @@ */ use std::{str, fmt, hash}; -use std::num::{Int, ToPrimitive}; use std::ops::{Add, Sub}; +use num::traits::ToPrimitive; use {Weekday, Timelike, Datelike}; use div::div_mod_floor;