// This is a part of rust-chrono. // Copyright (c) 2014, Kang Seonghoon. // See README.md and LICENSE.txt for details. /*! * ISO 8601 calendar date with timezone. */ use std::{fmt, num, hash}; use {Weekday, Datelike}; use duration::Duration; use offset::{Offset, UTC}; use naive; use naive::date::NaiveDate; use naive::time::NaiveTime; use datetime::DateTime; use format::DelayedFormat; /// ISO 8601 calendar date with timezone. #[deriving(Clone)] pub struct Date { date: NaiveDate, offset: Off, } /// The minimum possible `Date`. pub const MIN: Date = Date { date: naive::date::MIN, offset: UTC }; /// The maximum possible `Date`. pub const MAX: Date = Date { date: naive::date::MAX, offset: UTC }; impl Date { /// Makes a new `Date` with given *UTC* date and offset. /// The local date should be constructed via the `Offset` trait. #[inline] pub fn from_utc(date: NaiveDate, offset: Off) -> Date { Date { date: date, offset: offset } } /// Makes a new `NaiveDateTime` from the current date and given `NaiveTime`. /// The offset in the current date is preserved. /// /// Fails on invalid datetime. #[inline] pub fn and_time(&self, time: NaiveTime) -> Option> { self.offset.from_local_datetime(&self.date.and_time(time)).single() } /// Makes a new `NaiveDateTime` from the current date, hour, minute and second. /// The offset in the current date is preserved. /// /// Fails on invalid hour, minute and/or second. #[inline] pub fn and_hms(&self, hour: u32, min: u32, sec: u32) -> DateTime { self.and_hms_opt(hour, min, sec).expect("invalid time") } /// Makes a new `NaiveDateTime` from the current date, hour, minute and second. /// The offset in the current date is preserved. /// /// Returns `None` on invalid hour, minute and/or second. #[inline] pub fn and_hms_opt(&self, hour: u32, min: u32, sec: u32) -> Option> { NaiveTime::from_hms_opt(hour, min, sec).and_then(|time| self.and_time(time)) } /// Makes a new `NaiveDateTime` from the current date, hour, minute, second and millisecond. /// The millisecond part can exceed 1,000 in order to represent the leap second. /// The offset in the current date is preserved. /// /// Fails on invalid hour, minute, second and/or millisecond. #[inline] pub fn and_hms_milli(&self, hour: u32, min: u32, sec: u32, milli: u32) -> DateTime { self.and_hms_milli_opt(hour, min, sec, milli).expect("invalid time") } /// Makes a new `NaiveDateTime` from the current date, hour, minute, second and millisecond. /// The millisecond part can exceed 1,000 in order to represent the leap second. /// The offset in the current date is preserved. /// /// Returns `None` on invalid hour, minute, second and/or millisecond. #[inline] pub fn and_hms_milli_opt(&self, hour: u32, min: u32, sec: u32, milli: u32) -> Option> { NaiveTime::from_hms_milli_opt(hour, min, sec, milli).and_then(|time| self.and_time(time)) } /// Makes a new `NaiveDateTime` from the current date, hour, minute, second and microsecond. /// The microsecond part can exceed 1,000,000 in order to represent the leap second. /// The offset in the current date is preserved. /// /// Fails on invalid hour, minute, second and/or microsecond. #[inline] pub fn and_hms_micro(&self, hour: u32, min: u32, sec: u32, micro: u32) -> DateTime { self.and_hms_micro_opt(hour, min, sec, micro).expect("invalid time") } /// Makes a new `NaiveDateTime` from the current date, hour, minute, second and microsecond. /// The microsecond part can exceed 1,000,000 in order to represent the leap second. /// The offset in the current date is preserved. /// /// Returns `None` on invalid hour, minute, second and/or microsecond. #[inline] pub fn and_hms_micro_opt(&self, hour: u32, min: u32, sec: u32, micro: u32) -> Option> { NaiveTime::from_hms_micro_opt(hour, min, sec, micro).and_then(|time| self.and_time(time)) } /// Makes a new `NaiveDateTime` from the current date, hour, minute, second and nanosecond. /// The nanosecond part can exceed 1,000,000,000 in order to represent the leap second. /// The offset in the current date is preserved. /// /// Fails on invalid hour, minute, second and/or nanosecond. #[inline] pub fn and_hms_nano(&self, hour: u32, min: u32, sec: u32, nano: u32) -> DateTime { self.and_hms_nano_opt(hour, min, sec, nano).expect("invalid time") } /// Makes a new `NaiveDateTime` from the current date, hour, minute, second and nanosecond. /// The nanosecond part can exceed 1,000,000,000 in order to represent the leap second. /// The offset in the current date is preserved. /// /// Returns `None` on invalid hour, minute, second and/or nanosecond. #[inline] pub fn and_hms_nano_opt(&self, hour: u32, min: u32, sec: u32, nano: u32) -> Option> { NaiveTime::from_hms_nano_opt(hour, min, sec, nano).and_then(|time| self.and_time(time)) } /// Makes a new `Date` for the next date. /// /// Fails when `self` is the last representable date. #[inline] pub fn succ(&self) -> Date { self.succ_opt().expect("out of bound") } /// Makes a new `Date` for the next date. /// /// Returns `None` when `self` is the last representable date. #[inline] pub fn succ_opt(&self) -> Option> { self.date.succ_opt().map(|date| Date::from_utc(date, self.offset.clone())) } /// Makes a new `Date` for the prior date. /// /// Fails when `self` is the first representable date. #[inline] pub fn pred(&self) -> Date { self.pred_opt().expect("out of bound") } /// Makes a new `Date` for the prior date. /// /// Returns `None` when `self` is the first representable date. #[inline] pub fn pred_opt(&self) -> Option> { self.date.pred_opt().map(|date| Date::from_utc(date, self.offset.clone())) } /// Retrieves an associated offset. #[inline] pub fn offset<'a>(&'a self) -> &'a Off { &self.offset } /// Formats the date in the specified format string. /// See the `format` module on the supported escape sequences. #[inline] pub fn format<'a>(&'a self, fmt: &'a str) -> DelayedFormat<'a> { DelayedFormat::new_with_offset(Some(self.local()), None, &self.offset, fmt) } /// Returns a view to the local date. fn local(&self) -> NaiveDate { self.offset.to_local_date(&self.date) } } impl Datelike for Date { #[inline] fn year(&self) -> i32 { self.local().year() } #[inline] fn month(&self) -> u32 { self.local().month() } #[inline] fn month0(&self) -> u32 { self.local().month0() } #[inline] fn day(&self) -> u32 { self.local().day() } #[inline] fn day0(&self) -> u32 { self.local().day0() } #[inline] fn ordinal(&self) -> u32 { self.local().ordinal() } #[inline] fn ordinal0(&self) -> u32 { self.local().ordinal0() } #[inline] fn weekday(&self) -> Weekday { self.local().weekday() } #[inline] fn isoweekdate(&self) -> (i32, u32, Weekday) { self.local().isoweekdate() } #[inline] fn with_year(&self, year: i32) -> Option> { self.local().with_year(year) .and_then(|date| self.offset.from_local_date(&date).single()) } #[inline] fn with_month(&self, month: u32) -> Option> { self.local().with_month(month) .and_then(|date| self.offset.from_local_date(&date).single()) } #[inline] fn with_month0(&self, month0: u32) -> Option> { self.local().with_month0(month0) .and_then(|date| self.offset.from_local_date(&date).single()) } #[inline] fn with_day(&self, day: u32) -> Option> { self.local().with_day(day) .and_then(|date| self.offset.from_local_date(&date).single()) } #[inline] fn with_day0(&self, day0: u32) -> Option> { self.local().with_day0(day0) .and_then(|date| self.offset.from_local_date(&date).single()) } #[inline] fn with_ordinal(&self, ordinal: u32) -> Option> { self.local().with_ordinal(ordinal) .and_then(|date| self.offset.from_local_date(&date).single()) } #[inline] fn with_ordinal0(&self, ordinal0: u32) -> Option> { self.local().with_ordinal0(ordinal0) .and_then(|date| self.offset.from_local_date(&date).single()) } } impl num::Bounded for Date { #[inline] fn min_value() -> Date { MIN } #[inline] fn max_value() -> Date { MAX } } impl PartialEq for Date { fn eq(&self, other: &Date) -> bool { self.date == other.date } } impl Eq for Date { } impl Equiv> for Date { fn equiv(&self, other: &Date) -> bool { self.date == other.date } } impl PartialOrd for Date { fn partial_cmp(&self, other: &Date) -> Option { self.date.partial_cmp(&other.date) } } impl Ord for Date { fn cmp(&self, other: &Date) -> Ordering { self.date.cmp(&other.date) } } impl hash::Hash for Date { fn hash(&self, state: &mut hash::sip::SipState) { self.date.hash(state) } } impl Add> for Date { fn add(&self, rhs: &Duration) -> Date { Date { date: self.date + *rhs, offset: self.offset.clone() } } } /* // Rust issue #7590, the current coherence checker can't handle multiple Add impls impl Add,Date> for Duration { #[inline] fn add(&self, rhs: &Date) -> Date { rhs.add(self) } } */ impl Sub,Duration> for Date { fn sub(&self, rhs: &Date) -> Duration { self.date - rhs.date } } impl fmt::Show for Date { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}{}", self.local(), self.offset) } }