diff --git a/src/date.rs b/src/date.rs index 06a8e54..90f761c 100644 --- a/src/date.rs +++ b/src/date.rs @@ -348,7 +348,6 @@ mod tests { use duration::Duration; use naive::date::NaiveDate; - use naive::time::NaiveTime; use naive::datetime::NaiveDateTime; use offset::{TimeZone, Offset, LocalResult}; @@ -366,15 +365,11 @@ mod tests { fn offset_from_local_date(&self, _local: &NaiveDate) -> LocalResult { LocalResult::Single(OneYear) } - fn offset_from_local_time(&self, _local: &NaiveTime) -> LocalResult { - LocalResult::Single(OneYear) - } fn offset_from_local_datetime(&self, _local: &NaiveDateTime) -> LocalResult { LocalResult::Single(OneYear) } fn offset_from_utc_date(&self, _utc: &NaiveDate) -> OneYear { OneYear } - fn offset_from_utc_time(&self, _utc: &NaiveTime) -> OneYear { OneYear } fn offset_from_utc_datetime(&self, _utc: &NaiveDateTime) -> OneYear { OneYear } } diff --git a/src/datetime.rs b/src/datetime.rs index 3d54146..537dad4 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -16,8 +16,8 @@ use offset::utc::UTC; use offset::local::Local; use offset::fixed::FixedOffset; use duration::Duration; +use naive::time::NaiveTime; use naive::datetime::NaiveDateTime; -use time::Time; use date::Date; use format::{Item, Numeric, Pad, Fixed}; use format::{parse, Parsed, ParseError, ParseResult, DelayedFormat, StrftimeItems}; @@ -46,9 +46,10 @@ impl DateTime { } /// Retrieves a time component. + /// Unlike `date`, this is not associated to the time zone. #[inline] - pub fn time(&self) -> Time { - Time::from_utc(self.datetime.time().clone(), self.offset.clone()) + pub fn time(&self) -> NaiveTime { + self.datetime.time().clone() } /// Returns the number of non-leap seconds since January 1, 1970 0:00:00 UTC diff --git a/src/lib.rs b/src/lib.rs index 6ef0708..a87736b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -214,7 +214,8 @@ assert_eq!(Local::today(), Local::now().date()); assert_eq!(UTC.ymd(2014, 11, 28).weekday(), Weekday::Fri); assert_eq!(UTC.ymd_opt(2014, 11, 31), LocalResult::None); -assert_eq!(UTC.hms_milli(7, 8, 9, 10).format("%H%M%S").to_string(), "070809"); +assert_eq!(UTC.ymd(2014, 11, 28).and_hms_milli(7, 8, 9, 10).format("%H%M%S").to_string(), + "070809"); ~~~~ `DateTime` has two methods, `date` and `time`, @@ -268,7 +269,6 @@ pub use naive::date::NaiveDate; pub use naive::time::NaiveTime; pub use naive::datetime::NaiveDateTime; pub use date::Date; -pub use time::Time; pub use datetime::DateTime; pub use format::{ParseError, ParseResult}; @@ -296,7 +296,6 @@ pub mod naive { pub mod datetime; } pub mod date; -pub mod time; pub mod datetime; pub mod format; diff --git a/src/offset/fixed.rs b/src/offset/fixed.rs index e6528b5..3fba9aa 100644 --- a/src/offset/fixed.rs +++ b/src/offset/fixed.rs @@ -11,7 +11,6 @@ use std::fmt; use div::div_mod_floor; use duration::Duration; use naive::date::NaiveDate; -use naive::time::NaiveTime; use naive::datetime::NaiveDateTime; use super::{TimeZone, Offset, LocalResult}; @@ -71,15 +70,11 @@ impl TimeZone for FixedOffset { fn offset_from_local_date(&self, _local: &NaiveDate) -> LocalResult { LocalResult::Single(self.clone()) } - fn offset_from_local_time(&self, _local: &NaiveTime) -> LocalResult { - LocalResult::Single(self.clone()) - } fn offset_from_local_datetime(&self, _local: &NaiveDateTime) -> LocalResult { LocalResult::Single(self.clone()) } fn offset_from_utc_date(&self, _utc: &NaiveDate) -> FixedOffset { self.clone() } - fn offset_from_utc_time(&self, _utc: &NaiveTime) -> FixedOffset { self.clone() } fn offset_from_utc_datetime(&self, _utc: &NaiveDateTime) -> FixedOffset { self.clone() } } diff --git a/src/offset/local.rs b/src/offset/local.rs index 23e51aa..a4ca05d 100644 --- a/src/offset/local.rs +++ b/src/offset/local.rs @@ -14,7 +14,6 @@ use naive::date::NaiveDate; use naive::time::NaiveTime; use naive::datetime::NaiveDateTime; use date::Date; -use time::Time; use datetime::DateTime; use super::{TimeZone, LocalResult}; use super::fixed::FixedOffset; @@ -79,9 +78,6 @@ impl TimeZone for Local { fn offset_from_local_date(&self, local: &NaiveDate) -> LocalResult { self.from_local_date(local).map(|&: date| *date.offset()) } - fn offset_from_local_time(&self, local: &NaiveTime) -> LocalResult { - self.from_local_time(local).map(|&: time| *time.offset()) - } fn offset_from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult { self.from_local_datetime(local).map(|&: datetime| *datetime.offset()) } @@ -89,9 +85,6 @@ impl TimeZone for Local { fn offset_from_utc_date(&self, utc: &NaiveDate) -> FixedOffset { *self.from_utc_date(utc).offset() } - fn offset_from_utc_time(&self, utc: &NaiveTime) -> FixedOffset { - *self.from_utc_time(utc).offset() - } fn offset_from_utc_datetime(&self, utc: &NaiveDateTime) -> FixedOffset { *self.from_utc_datetime(utc).offset() } @@ -100,9 +93,6 @@ impl TimeZone for Local { fn from_local_date(&self, local: &NaiveDate) -> LocalResult> { self.from_local_datetime(&local.and_hms(0, 0, 0)).map(|datetime| datetime.date()) } - fn from_local_time(&self, _local: &NaiveTime) -> LocalResult> { - LocalResult::None // we have no information about this time - } fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult> { let timespec = datetime_to_timespec(local); LocalResult::Single(tm_to_datetime(stdtime::at(timespec))) @@ -111,9 +101,6 @@ impl TimeZone for Local { fn from_utc_date(&self, utc: &NaiveDate) -> Date { self.from_utc_datetime(&utc.and_hms(0, 0, 0)).date() } - fn from_utc_time(&self, _utc: &NaiveTime) -> Time { - unimplemented!() // we have no information about this time - } fn from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime { let timespec = datetime_to_timespec(utc); tm_to_datetime(stdtime::at_utc(timespec)) diff --git a/src/offset/mod.rs b/src/offset/mod.rs index 7ab17b7..509b2e0 100644 --- a/src/offset/mod.rs +++ b/src/offset/mod.rs @@ -28,7 +28,6 @@ use naive::date::NaiveDate; use naive::time::NaiveTime; use naive::datetime::NaiveDateTime; use date::Date; -use time::Time; use datetime::DateTime; use format::{parse, Parsed, ParseResult, StrftimeItems}; @@ -247,80 +246,6 @@ pub trait TimeZone: Sized { } } - /// Makes a new `Time` from hour, minute, second and the current time zone. - /// - /// Fails on invalid hour, minute and/or second. - fn hms(&self, hour: u32, min: u32, sec: u32) -> Time { - self.hms_opt(hour, min, sec).unwrap() - } - - /// Makes a new `Time` from hour, minute, second and the current time zone. - /// - /// Returns `None` on invalid hour, minute and/or second. - fn hms_opt(&self, hour: u32, min: u32, sec: u32) -> LocalResult> { - match NaiveTime::from_hms_opt(hour, min, sec) { - Some(t) => self.from_local_time(&t), - None => LocalResult::None, - } - } - - /// Makes a new `Time` from hour, minute, second, millisecond and the current time zone. - /// The millisecond part can exceed 1,000 in order to represent the leap second. - /// - /// Fails on invalid hour, minute, second and/or millisecond. - fn hms_milli(&self, hour: u32, min: u32, sec: u32, milli: u32) -> Time { - self.hms_milli_opt(hour, min, sec, milli).unwrap() - } - - /// Makes a new `Time` from hour, minute, second, millisecond and the current time zone. - /// The millisecond part can exceed 1,000 in order to represent the leap second. - /// - /// Returns `None` on invalid hour, minute, second and/or millisecond. - fn hms_milli_opt(&self, hour: u32, min: u32, sec: u32, milli: u32) -> LocalResult> { - match NaiveTime::from_hms_milli_opt(hour, min, sec, milli) { - Some(t) => self.from_local_time(&t), - None => LocalResult::None, - } - } - - /// Makes a new `Time` from hour, minute, second, microsecond and the current time zone. - /// The microsecond part can exceed 1,000,000 in order to represent the leap second. - /// - /// Fails on invalid hour, minute, second and/or microsecond. - fn hms_micro(&self, hour: u32, min: u32, sec: u32, micro: u32) -> Time { - self.hms_micro_opt(hour, min, sec, micro).unwrap() - } - - /// Makes a new `Time` from hour, minute, second, microsecond and the current time zone. - /// The microsecond part can exceed 1,000,000 in order to represent the leap second. - /// - /// Returns `None` on invalid hour, minute, second and/or microsecond. - fn hms_micro_opt(&self, hour: u32, min: u32, sec: u32, micro: u32) -> LocalResult> { - match NaiveTime::from_hms_micro_opt(hour, min, sec, micro) { - Some(t) => self.from_local_time(&t), - None => LocalResult::None, - } - } - - /// Makes a new `Time` from hour, minute, second, nanosecond and the current time zone. - /// The nanosecond part can exceed 1,000,000,000 in order to represent the leap second. - /// - /// Fails on invalid hour, minute, second and/or nanosecond. - fn hms_nano(&self, hour: u32, min: u32, sec: u32, nano: u32) -> Time { - self.hms_nano_opt(hour, min, sec, nano).unwrap() - } - - /// Makes a new `Time` from hour, minute, second, nanosecond and the current time zone. - /// The nanosecond part can exceed 1,000,000,000 in order to represent the leap second. - /// - /// Returns `None` on invalid hour, minute, second and/or nanosecond. - fn hms_nano_opt(&self, hour: u32, min: u32, sec: u32, nano: u32) -> LocalResult> { - match NaiveTime::from_hms_nano_opt(hour, min, sec, nano) { - Some(t) => self.from_local_time(&t), - None => LocalResult::None, - } - } - /// Makes a new `DateTime` from the number of non-leap seconds /// since January 1, 1970 0:00:00 UTC (aka "UNIX timestamp") /// and the number of nanoseconds since the last whole non-leap second. @@ -363,9 +288,6 @@ pub trait TimeZone: Sized { /// Creates the offset(s) for given local `NaiveDate` if possible. fn offset_from_local_date(&self, local: &NaiveDate) -> LocalResult; - /// Creates the offset(s) for given local `NaiveTime` if possible. - fn offset_from_local_time(&self, local: &NaiveTime) -> LocalResult; - /// Creates the offset(s) for given local `NaiveDateTime` if possible. fn offset_from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult; @@ -376,13 +298,6 @@ pub trait TimeZone: Sized { }) } - /// Converts the local `NaiveTime` to the timezone-aware `Time` if possible. - fn from_local_time(&self, local: &NaiveTime) -> LocalResult> { - self.offset_from_local_time(local).map(|offset| { - Time::from_utc(*local - offset.local_minus_utc(), offset) - }) - } - /// Converts the local `NaiveDateTime` to the timezone-aware `DateTime` if possible. fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult> { self.offset_from_local_datetime(local).map(|offset| { @@ -393,9 +308,6 @@ pub trait TimeZone: Sized { /// Creates the offset for given UTC `NaiveDate`. This cannot fail. fn offset_from_utc_date(&self, utc: &NaiveDate) -> Self::Offset; - /// Creates the offset for given UTC `NaiveTime`. This cannot fail. - fn offset_from_utc_time(&self, utc: &NaiveTime) -> Self::Offset; - /// Creates the offset for given UTC `NaiveDateTime`. This cannot fail. fn offset_from_utc_datetime(&self, utc: &NaiveDateTime) -> Self::Offset; @@ -405,12 +317,6 @@ pub trait TimeZone: Sized { Date::from_utc(utc.clone(), self.offset_from_utc_date(utc)) } - /// Converts the UTC `NaiveTime` to the local time. - /// The UTC is continuous and thus this cannot fail (but can give the duplicate local time). - fn from_utc_time(&self, utc: &NaiveTime) -> Time { - Time::from_utc(utc.clone(), self.offset_from_utc_time(utc)) - } - /// Converts the UTC `NaiveDateTime` to the local time. /// The UTC is continuous and thus this cannot fail (but can give the duplicate local time). fn from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime { diff --git a/src/offset/utc.rs b/src/offset/utc.rs index a61b1a3..f7e4424 100644 --- a/src/offset/utc.rs +++ b/src/offset/utc.rs @@ -11,7 +11,6 @@ use stdtime; use duration::Duration; use naive::date::NaiveDate; -use naive::time::NaiveTime; use naive::datetime::NaiveDateTime; use date::Date; use datetime::DateTime; @@ -42,15 +41,11 @@ impl TimeZone for UTC { fn offset_from_local_date(&self, _local: &NaiveDate) -> LocalResult { LocalResult::Single(UTC) } - fn offset_from_local_time(&self, _local: &NaiveTime) -> LocalResult { - LocalResult::Single(UTC) - } fn offset_from_local_datetime(&self, _local: &NaiveDateTime) -> LocalResult { LocalResult::Single(UTC) } fn offset_from_utc_date(&self, _utc: &NaiveDate) -> UTC { UTC } - fn offset_from_utc_time(&self, _utc: &NaiveTime) -> UTC { UTC } fn offset_from_utc_datetime(&self, _utc: &NaiveDateTime) -> UTC { UTC} } diff --git a/src/time.rs b/src/time.rs deleted file mode 100644 index 36f4ad6..0000000 --- a/src/time.rs +++ /dev/null @@ -1,173 +0,0 @@ -// This is a part of rust-chrono. -// Copyright (c) 2014-2015, Kang Seonghoon. -// See README.md and LICENSE.txt for details. - -/*! - * ISO 8601 time with time zone. - */ - -use std::{fmt, hash}; -use std::cmp::Ordering; -use std::ops::{Add, Sub}; - -use Timelike; -use offset::{TimeZone, Offset}; -use duration::Duration; -use naive::time::NaiveTime; -use format::{Item, DelayedFormat, StrftimeItems}; - -/// ISO 8601 time with timezone. -#[derive(Clone)] -pub struct Time { - time: NaiveTime, - offset: Tz::Offset, -} - -impl Time { - /// Makes a new `Time` with given *UTC* time and offset. - /// The local time should be constructed via the `TimeZone` trait. - // - // note: this constructor is purposedly not named to `new` to discourage the direct usage. - #[inline] - pub fn from_utc(time: NaiveTime, offset: Tz::Offset) -> Time { - Time { time: time, offset: offset } - } - - /// Retrieves an associated offset from UTC. - #[inline] - pub fn offset<'a>(&'a self) -> &'a Tz::Offset { - &self.offset - } - - /// Retrieves an associated time zone. - #[inline] - pub fn timezone(&self) -> Tz { - TimeZone::from_offset(&self.offset) - } - - /// Changes the associated time zone. - /// This does not change the actual `Time` (but will change the string representation). - #[inline] - pub fn with_timezone(&self, tz: &Tz2) -> Time { - tz.from_utc_time(&self.time) - } - - /// Returns a view to the naive UTC time. - #[inline] - pub fn naive_utc(&self) -> NaiveTime { - self.time - } - - /// Returns a view to the naive local time. - #[inline] - pub fn naive_local(&self) -> NaiveTime { - self.time + self.offset.local_minus_utc() - } -} - -/// Maps the local time to other time with given conversion function. -fn map_local(t: &Time, mut f: F) -> Option> - where F: FnMut(NaiveTime) -> Option { - f(t.naive_local()).and_then(|time| t.timezone().from_local_time(&time).single()) -} - -impl Time where Tz::Offset: fmt::Display { - /// Formats the time with the specified formatting items. - #[inline] - pub fn format_with_items<'a, I>(&'a self, items: I) -> DelayedFormat<'a, I> - where I: Iterator> + Clone { - DelayedFormat::new_with_offset(None, Some(self.naive_local()), &self.offset, items) - } - - /// Formats the time with the specified format string. - /// See the `format::strftime` module on the supported escape sequences. - #[inline] - pub fn format<'a>(&'a self, fmt: &'a str) -> DelayedFormat<'a, StrftimeItems<'a>> { - self.format_with_items(StrftimeItems::new(fmt)) - } -} - -impl Timelike for Time { - #[inline] fn hour(&self) -> u32 { self.naive_local().hour() } - #[inline] fn minute(&self) -> u32 { self.naive_local().minute() } - #[inline] fn second(&self) -> u32 { self.naive_local().second() } - #[inline] fn nanosecond(&self) -> u32 { self.naive_local().nanosecond() } - - #[inline] - fn with_hour(&self, hour: u32) -> Option> { - map_local(self, |time| time.with_hour(hour)) - } - - #[inline] - fn with_minute(&self, min: u32) -> Option> { - map_local(self, |time| time.with_minute(min)) - } - - #[inline] - fn with_second(&self, sec: u32) -> Option> { - map_local(self, |time| time.with_second(sec)) - } - - #[inline] - fn with_nanosecond(&self, nano: u32) -> Option> { - map_local(self, |time| time.with_nanosecond(nano)) - } - - #[inline] - fn num_seconds_from_midnight(&self) -> u32 { self.naive_local().num_seconds_from_midnight() } -} - -impl PartialEq> for Time { - fn eq(&self, other: &Time) -> bool { self.time == other.time } -} - -impl Eq for Time { -} - -impl PartialOrd for Time { - fn partial_cmp(&self, other: &Time) -> Option { - self.time.partial_cmp(&other.time) - } -} - -impl Ord for Time { - fn cmp(&self, other: &Time) -> Ordering { self.time.cmp(&other.time) } -} - -impl hash::Hash for Time { - fn hash(&self, state: &mut H) { self.time.hash(state) } -} - -impl Add for Time { - type Output = Time; - - fn add(self, rhs: Duration) -> Time { - Time { time: self.time + rhs, offset: self.offset } - } -} - -impl Sub> for Time { - type Output = Duration; - - fn sub(self, rhs: Time) -> Duration { self.time - rhs.time } -} - -impl Sub for Time { - type Output = Time; - - #[inline] - fn sub(self, rhs: Duration) -> Time { self.add(-rhs) } -} - -impl fmt::Debug for Time { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{:?}{:?}", self.naive_local(), self.offset) - } -} - -impl fmt::Display for Time where Tz::Offset: fmt::Display { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}{}", self.naive_local(), self.offset) - } -} -