2017-02-05 21:15:57 +00:00
|
|
|
// This is a part of Chrono.
|
2014-04-01 17:14:57 +00:00
|
|
|
// See README.md and LICENSE.txt for details.
|
|
|
|
|
2017-02-05 23:17:01 +00:00
|
|
|
//! ISO 8601 calendar date with time zone.
|
2014-03-28 11:38:11 +00:00
|
|
|
|
2014-11-19 01:59:32 +00:00
|
|
|
use std::{fmt, hash};
|
2015-01-03 22:45:07 +00:00
|
|
|
use std::cmp::Ordering;
|
|
|
|
use std::ops::{Add, Sub};
|
2017-02-05 23:17:01 +00:00
|
|
|
use oldtime::Duration as OldDuration;
|
2014-07-29 07:14:46 +00:00
|
|
|
|
|
|
|
use {Weekday, Datelike};
|
2017-06-21 15:33:17 +00:00
|
|
|
use offset::{TimeZone, Utc};
|
2017-06-21 15:21:24 +00:00
|
|
|
use naive::{self, NaiveDate, NaiveTime, IsoWeek};
|
2017-06-21 05:03:49 +00:00
|
|
|
use DateTime;
|
2015-02-14 03:33:12 +00:00
|
|
|
use format::{Item, DelayedFormat, StrftimeItems};
|
2014-03-28 11:38:11 +00:00
|
|
|
|
2015-01-12 17:03:12 +00:00
|
|
|
/// ISO 8601 calendar date with time zone.
|
2016-02-04 16:23:46 +00:00
|
|
|
///
|
|
|
|
/// This type should be considered ambiguous at best,
|
|
|
|
/// due to the inherent lack of precision required for the time zone resolution.
|
2017-05-04 20:53:53 +00:00
|
|
|
/// For serialization and deserialization uses, it is best to use `NaiveDate` instead.
|
2016-02-04 16:23:46 +00:00
|
|
|
/// There are some guarantees on the usage of `Date<Tz>`:
|
|
|
|
///
|
|
|
|
/// - If properly constructed via `TimeZone::ymd` and others without an error,
|
|
|
|
/// the corresponding local date should exist for at least a moment.
|
|
|
|
/// (It may still have a gap from the offset changes.)
|
|
|
|
///
|
|
|
|
/// - The `TimeZone` is free to assign *any* `Offset` to the local date,
|
|
|
|
/// as long as that offset did occur in given day.
|
|
|
|
/// For example, if `2015-03-08T01:59-08:00` is followed by `2015-03-08T03:00-07:00`,
|
|
|
|
/// it may produce either `2015-03-08-08:00` or `2015-03-08-07:00`
|
|
|
|
/// but *not* `2015-03-08+00:00` and others.
|
|
|
|
///
|
|
|
|
/// - Once constructed as a full `DateTime`,
|
|
|
|
/// `DateTime::date` and other associated methods should return those for the original `Date`.
|
|
|
|
/// For example, if `dt = tz.ymd(y,m,d).hms(h,n,s)` were valid, `dt.date() == tz.ymd(y,m,d)`.
|
|
|
|
///
|
|
|
|
/// - The date is timezone-agnostic up to one day (i.e. practically always),
|
|
|
|
/// so the local date and UTC date should be equal for most cases
|
|
|
|
/// even though the raw calculation between `NaiveDate` and `Duration` may not.
|
2015-01-03 22:45:07 +00:00
|
|
|
#[derive(Clone)]
|
2015-01-12 17:03:12 +00:00
|
|
|
pub struct Date<Tz: TimeZone> {
|
2014-07-29 06:55:40 +00:00
|
|
|
date: NaiveDate,
|
2015-01-12 17:03:12 +00:00
|
|
|
offset: Tz::Offset,
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The minimum possible `Date`.
|
2017-06-21 15:33:17 +00:00
|
|
|
pub const MIN_DATE: Date<Utc> = Date { date: naive::MIN_DATE, offset: Utc };
|
2014-07-29 06:41:07 +00:00
|
|
|
/// The maximum possible `Date`.
|
2017-06-21 15:33:17 +00:00
|
|
|
pub const MAX_DATE: Date<Utc> = Date { date: naive::MAX_DATE, offset: Utc };
|
2014-07-29 06:41:07 +00:00
|
|
|
|
2015-01-12 17:03:12 +00:00
|
|
|
impl<Tz: TimeZone> Date<Tz> {
|
2014-07-29 06:41:07 +00:00
|
|
|
/// Makes a new `Date` with given *UTC* date and offset.
|
2015-01-12 17:03:12 +00:00
|
|
|
/// The local date should be constructed via the `TimeZone` trait.
|
2015-01-12 16:33:53 +00:00
|
|
|
//
|
|
|
|
// note: this constructor is purposedly not named to `new` to discourage the direct usage.
|
2014-07-29 06:41:07 +00:00
|
|
|
#[inline]
|
2015-01-12 17:03:12 +00:00
|
|
|
pub fn from_utc(date: NaiveDate, offset: Tz::Offset) -> Date<Tz> {
|
2014-07-29 06:41:07 +00:00
|
|
|
Date { date: date, offset: offset }
|
|
|
|
}
|
|
|
|
|
2014-11-28 14:53:22 +00:00
|
|
|
/// Makes a new `DateTime` from the current date and given `NaiveTime`.
|
2014-07-29 06:41:07 +00:00
|
|
|
/// The offset in the current date is preserved.
|
|
|
|
///
|
2015-09-11 17:41:38 +00:00
|
|
|
/// Panics on invalid datetime.
|
2014-07-29 06:41:07 +00:00
|
|
|
#[inline]
|
2015-01-12 17:03:12 +00:00
|
|
|
pub fn and_time(&self, time: NaiveTime) -> Option<DateTime<Tz>> {
|
2015-01-12 16:33:53 +00:00
|
|
|
let localdt = self.naive_local().and_time(time);
|
|
|
|
self.timezone().from_local_datetime(&localdt).single()
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
2014-11-28 14:53:22 +00:00
|
|
|
/// Makes a new `DateTime` from the current date, hour, minute and second.
|
2014-07-29 06:41:07 +00:00
|
|
|
/// The offset in the current date is preserved.
|
|
|
|
///
|
2015-09-11 17:41:38 +00:00
|
|
|
/// Panics on invalid hour, minute and/or second.
|
2014-07-29 06:41:07 +00:00
|
|
|
#[inline]
|
2015-01-12 17:03:12 +00:00
|
|
|
pub fn and_hms(&self, hour: u32, min: u32, sec: u32) -> DateTime<Tz> {
|
2014-07-29 06:41:07 +00:00
|
|
|
self.and_hms_opt(hour, min, sec).expect("invalid time")
|
|
|
|
}
|
|
|
|
|
2014-11-28 14:53:22 +00:00
|
|
|
/// Makes a new `DateTime` from the current date, hour, minute and second.
|
2014-07-29 06:41:07 +00:00
|
|
|
/// The offset in the current date is preserved.
|
|
|
|
///
|
|
|
|
/// Returns `None` on invalid hour, minute and/or second.
|
|
|
|
#[inline]
|
2015-01-12 17:03:12 +00:00
|
|
|
pub fn and_hms_opt(&self, hour: u32, min: u32, sec: u32) -> Option<DateTime<Tz>> {
|
2014-07-29 06:55:40 +00:00
|
|
|
NaiveTime::from_hms_opt(hour, min, sec).and_then(|time| self.and_time(time))
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
2014-11-28 14:53:22 +00:00
|
|
|
/// Makes a new `DateTime` from the current date, hour, minute, second and millisecond.
|
2014-07-29 06:41:07 +00:00
|
|
|
/// The millisecond part can exceed 1,000 in order to represent the leap second.
|
|
|
|
/// The offset in the current date is preserved.
|
|
|
|
///
|
2015-09-11 17:41:38 +00:00
|
|
|
/// Panics on invalid hour, minute, second and/or millisecond.
|
2014-07-29 06:41:07 +00:00
|
|
|
#[inline]
|
2015-01-12 17:03:12 +00:00
|
|
|
pub fn and_hms_milli(&self, hour: u32, min: u32, sec: u32, milli: u32) -> DateTime<Tz> {
|
2014-07-29 06:41:07 +00:00
|
|
|
self.and_hms_milli_opt(hour, min, sec, milli).expect("invalid time")
|
|
|
|
}
|
|
|
|
|
2014-11-28 14:53:22 +00:00
|
|
|
/// Makes a new `DateTime` from the current date, hour, minute, second and millisecond.
|
2014-07-29 06:41:07 +00:00
|
|
|
/// 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,
|
2015-01-12 17:03:12 +00:00
|
|
|
milli: u32) -> Option<DateTime<Tz>> {
|
2014-07-29 06:55:40 +00:00
|
|
|
NaiveTime::from_hms_milli_opt(hour, min, sec, milli).and_then(|time| self.and_time(time))
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
2014-11-28 14:53:22 +00:00
|
|
|
/// Makes a new `DateTime` from the current date, hour, minute, second and microsecond.
|
2014-07-29 06:41:07 +00:00
|
|
|
/// The microsecond part can exceed 1,000,000 in order to represent the leap second.
|
|
|
|
/// The offset in the current date is preserved.
|
|
|
|
///
|
2015-09-11 17:41:38 +00:00
|
|
|
/// Panics on invalid hour, minute, second and/or microsecond.
|
2014-07-29 06:41:07 +00:00
|
|
|
#[inline]
|
2015-01-12 17:03:12 +00:00
|
|
|
pub fn and_hms_micro(&self, hour: u32, min: u32, sec: u32, micro: u32) -> DateTime<Tz> {
|
2014-07-29 06:41:07 +00:00
|
|
|
self.and_hms_micro_opt(hour, min, sec, micro).expect("invalid time")
|
|
|
|
}
|
|
|
|
|
2014-11-28 14:53:22 +00:00
|
|
|
/// Makes a new `DateTime` from the current date, hour, minute, second and microsecond.
|
2014-07-29 06:41:07 +00:00
|
|
|
/// 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,
|
2015-01-12 17:03:12 +00:00
|
|
|
micro: u32) -> Option<DateTime<Tz>> {
|
2014-07-29 06:55:40 +00:00
|
|
|
NaiveTime::from_hms_micro_opt(hour, min, sec, micro).and_then(|time| self.and_time(time))
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
2014-11-28 14:53:22 +00:00
|
|
|
/// Makes a new `DateTime` from the current date, hour, minute, second and nanosecond.
|
2014-07-29 06:41:07 +00:00
|
|
|
/// The nanosecond part can exceed 1,000,000,000 in order to represent the leap second.
|
|
|
|
/// The offset in the current date is preserved.
|
|
|
|
///
|
2015-09-11 17:41:38 +00:00
|
|
|
/// Panics on invalid hour, minute, second and/or nanosecond.
|
2014-07-29 06:41:07 +00:00
|
|
|
#[inline]
|
2015-01-12 17:03:12 +00:00
|
|
|
pub fn and_hms_nano(&self, hour: u32, min: u32, sec: u32, nano: u32) -> DateTime<Tz> {
|
2014-07-29 06:41:07 +00:00
|
|
|
self.and_hms_nano_opt(hour, min, sec, nano).expect("invalid time")
|
|
|
|
}
|
|
|
|
|
2014-11-28 14:53:22 +00:00
|
|
|
/// Makes a new `DateTime` from the current date, hour, minute, second and nanosecond.
|
2014-07-29 06:41:07 +00:00
|
|
|
/// 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,
|
2015-01-12 17:03:12 +00:00
|
|
|
nano: u32) -> Option<DateTime<Tz>> {
|
2014-07-29 06:55:40 +00:00
|
|
|
NaiveTime::from_hms_nano_opt(hour, min, sec, nano).and_then(|time| self.and_time(time))
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Makes a new `Date` for the next date.
|
|
|
|
///
|
2015-09-11 17:41:38 +00:00
|
|
|
/// Panics when `self` is the last representable date.
|
2014-07-29 06:41:07 +00:00
|
|
|
#[inline]
|
2015-01-12 17:03:12 +00:00
|
|
|
pub fn succ(&self) -> Date<Tz> {
|
2014-07-29 06:41:07 +00:00
|
|
|
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]
|
2015-01-12 17:03:12 +00:00
|
|
|
pub fn succ_opt(&self) -> Option<Date<Tz>> {
|
2014-07-29 06:41:07 +00:00
|
|
|
self.date.succ_opt().map(|date| Date::from_utc(date, self.offset.clone()))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Makes a new `Date` for the prior date.
|
|
|
|
///
|
2015-09-11 17:41:38 +00:00
|
|
|
/// Panics when `self` is the first representable date.
|
2014-07-29 06:41:07 +00:00
|
|
|
#[inline]
|
2015-01-12 17:03:12 +00:00
|
|
|
pub fn pred(&self) -> Date<Tz> {
|
2014-07-29 06:41:07 +00:00
|
|
|
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]
|
2015-01-12 17:03:12 +00:00
|
|
|
pub fn pred_opt(&self) -> Option<Date<Tz>> {
|
2014-07-29 06:41:07 +00:00
|
|
|
self.date.pred_opt().map(|date| Date::from_utc(date, self.offset.clone()))
|
|
|
|
}
|
|
|
|
|
2015-01-12 17:03:12 +00:00
|
|
|
/// Retrieves an associated offset from UTC.
|
2014-08-29 09:14:08 +00:00
|
|
|
#[inline]
|
2017-06-23 19:55:17 +00:00
|
|
|
pub fn offset(&self) -> &Tz::Offset {
|
2014-08-29 09:14:08 +00:00
|
|
|
&self.offset
|
|
|
|
}
|
|
|
|
|
2015-01-12 17:03:12 +00:00
|
|
|
/// Retrieves an associated time zone.
|
2015-01-12 16:33:53 +00:00
|
|
|
#[inline]
|
2015-01-12 17:03:12 +00:00
|
|
|
pub fn timezone(&self) -> Tz {
|
|
|
|
TimeZone::from_offset(&self.offset)
|
2015-01-12 16:33:53 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 17:03:12 +00:00
|
|
|
/// Changes the associated time zone.
|
2014-11-28 14:53:22 +00:00
|
|
|
/// This does not change the actual `Date` (but will change the string representation).
|
|
|
|
#[inline]
|
2015-01-12 17:03:12 +00:00
|
|
|
pub fn with_timezone<Tz2: TimeZone>(&self, tz: &Tz2) -> Date<Tz2> {
|
2015-01-12 16:33:53 +00:00
|
|
|
tz.from_utc_date(&self.date)
|
2014-11-28 14:53:22 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 17:31:18 +00:00
|
|
|
/// Adds given `Duration` to the current date.
|
|
|
|
///
|
|
|
|
/// Returns `None` when it will result in overflow.
|
|
|
|
#[inline]
|
2017-02-05 23:17:01 +00:00
|
|
|
pub fn checked_add_signed(self, rhs: OldDuration) -> Option<Date<Tz>> {
|
|
|
|
let date = try_opt!(self.date.checked_add_signed(rhs));
|
2015-01-28 17:31:18 +00:00
|
|
|
Some(Date { date: date, offset: self.offset })
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Subtracts given `Duration` from the current date.
|
|
|
|
///
|
|
|
|
/// Returns `None` when it will result in overflow.
|
|
|
|
#[inline]
|
2017-02-05 23:17:01 +00:00
|
|
|
pub fn checked_sub_signed(self, rhs: OldDuration) -> Option<Date<Tz>> {
|
|
|
|
let date = try_opt!(self.date.checked_sub_signed(rhs));
|
2015-01-28 17:31:18 +00:00
|
|
|
Some(Date { date: date, offset: self.offset })
|
|
|
|
}
|
|
|
|
|
2017-02-05 23:17:01 +00:00
|
|
|
/// Subtracts another `Date` from the current date.
|
|
|
|
/// Returns a `Duration` of integral numbers.
|
|
|
|
///
|
|
|
|
/// This does not overflow or underflow at all,
|
|
|
|
/// as all possible output fits in the range of `Duration`.
|
|
|
|
#[inline]
|
|
|
|
pub fn signed_duration_since<Tz2: TimeZone>(self, rhs: Date<Tz2>) -> OldDuration {
|
|
|
|
self.date.signed_duration_since(rhs.date)
|
|
|
|
}
|
|
|
|
|
2015-01-12 16:33:53 +00:00
|
|
|
/// Returns a view to the naive UTC date.
|
|
|
|
#[inline]
|
|
|
|
pub fn naive_utc(&self) -> NaiveDate {
|
|
|
|
self.date
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a view to the naive local date.
|
2017-02-06 18:43:59 +00:00
|
|
|
///
|
|
|
|
/// This is technically same to [`naive_utc`](#method.naive_utc)
|
|
|
|
/// because the offset is restricted to never exceed one day,
|
|
|
|
/// but provided for the consistency.
|
2015-01-12 16:33:53 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn naive_local(&self) -> NaiveDate {
|
2017-02-06 18:43:59 +00:00
|
|
|
self.date
|
2015-01-09 18:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 16:33:53 +00:00
|
|
|
/// Maps the local date to other date with given conversion function.
|
2015-01-12 17:03:12 +00:00
|
|
|
fn map_local<Tz: TimeZone, F>(d: &Date<Tz>, mut f: F) -> Option<Date<Tz>>
|
2015-01-12 16:33:53 +00:00
|
|
|
where F: FnMut(NaiveDate) -> Option<NaiveDate> {
|
|
|
|
f(d.naive_local()).and_then(|date| d.timezone().from_local_date(&date).single())
|
|
|
|
}
|
|
|
|
|
2015-01-12 17:03:12 +00:00
|
|
|
impl<Tz: TimeZone> Date<Tz> where Tz::Offset: fmt::Display {
|
2015-02-14 03:33:12 +00:00
|
|
|
/// Formats the date with the specified formatting items.
|
|
|
|
#[inline]
|
2015-02-21 09:14:09 +00:00
|
|
|
pub fn format_with_items<'a, I>(&self, items: I) -> DelayedFormat<I>
|
2015-02-14 03:33:12 +00:00
|
|
|
where I: Iterator<Item=Item<'a>> + Clone {
|
2015-02-18 16:48:29 +00:00
|
|
|
DelayedFormat::new_with_offset(Some(self.naive_local()), None, &self.offset, items)
|
2015-02-14 03:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Formats the date with the specified format string.
|
2017-06-21 12:58:42 +00:00
|
|
|
/// See the [`format::strftime` module](./format/strftime/index.html)
|
2015-09-06 12:30:09 +00:00
|
|
|
/// on the supported escape sequences.
|
2014-07-31 19:08:19 +00:00
|
|
|
#[inline]
|
2015-02-21 09:14:09 +00:00
|
|
|
pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>> {
|
2015-02-14 03:33:12 +00:00
|
|
|
self.format_with_items(StrftimeItems::new(fmt))
|
2014-07-31 19:08:19 +00:00
|
|
|
}
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 17:03:12 +00:00
|
|
|
impl<Tz: TimeZone> Datelike for Date<Tz> {
|
2015-01-12 16:33:53 +00:00
|
|
|
#[inline] fn year(&self) -> i32 { self.naive_local().year() }
|
|
|
|
#[inline] fn month(&self) -> u32 { self.naive_local().month() }
|
|
|
|
#[inline] fn month0(&self) -> u32 { self.naive_local().month0() }
|
|
|
|
#[inline] fn day(&self) -> u32 { self.naive_local().day() }
|
|
|
|
#[inline] fn day0(&self) -> u32 { self.naive_local().day0() }
|
|
|
|
#[inline] fn ordinal(&self) -> u32 { self.naive_local().ordinal() }
|
|
|
|
#[inline] fn ordinal0(&self) -> u32 { self.naive_local().ordinal0() }
|
|
|
|
#[inline] fn weekday(&self) -> Weekday { self.naive_local().weekday() }
|
2017-06-21 15:21:24 +00:00
|
|
|
#[inline] fn iso_week(&self) -> IsoWeek { self.naive_local().iso_week() }
|
2014-07-29 06:41:07 +00:00
|
|
|
|
|
|
|
#[inline]
|
2015-01-12 17:03:12 +00:00
|
|
|
fn with_year(&self, year: i32) -> Option<Date<Tz>> {
|
2015-01-12 16:33:53 +00:00
|
|
|
map_local(self, |date| date.with_year(year))
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-01-12 17:03:12 +00:00
|
|
|
fn with_month(&self, month: u32) -> Option<Date<Tz>> {
|
2015-01-12 16:33:53 +00:00
|
|
|
map_local(self, |date| date.with_month(month))
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-01-12 17:03:12 +00:00
|
|
|
fn with_month0(&self, month0: u32) -> Option<Date<Tz>> {
|
2015-01-12 16:33:53 +00:00
|
|
|
map_local(self, |date| date.with_month0(month0))
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-01-12 17:03:12 +00:00
|
|
|
fn with_day(&self, day: u32) -> Option<Date<Tz>> {
|
2015-01-12 16:33:53 +00:00
|
|
|
map_local(self, |date| date.with_day(day))
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-01-12 17:03:12 +00:00
|
|
|
fn with_day0(&self, day0: u32) -> Option<Date<Tz>> {
|
2015-01-12 16:33:53 +00:00
|
|
|
map_local(self, |date| date.with_day0(day0))
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-01-12 17:03:12 +00:00
|
|
|
fn with_ordinal(&self, ordinal: u32) -> Option<Date<Tz>> {
|
2015-01-12 16:33:53 +00:00
|
|
|
map_local(self, |date| date.with_ordinal(ordinal))
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-01-12 17:03:12 +00:00
|
|
|
fn with_ordinal0(&self, ordinal0: u32) -> Option<Date<Tz>> {
|
2015-01-12 16:33:53 +00:00
|
|
|
map_local(self, |date| date.with_ordinal0(ordinal0))
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-27 04:08:20 +00:00
|
|
|
// we need them as automatic impls cannot handle associated types
|
|
|
|
impl<Tz: TimeZone> Copy for Date<Tz> where <Tz as TimeZone>::Offset: Copy {}
|
|
|
|
unsafe impl<Tz: TimeZone> Send for Date<Tz> where <Tz as TimeZone>::Offset: Send {}
|
|
|
|
|
2015-01-12 17:03:12 +00:00
|
|
|
impl<Tz: TimeZone, Tz2: TimeZone> PartialEq<Date<Tz2>> for Date<Tz> {
|
|
|
|
fn eq(&self, other: &Date<Tz2>) -> bool { self.date == other.date }
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 17:03:12 +00:00
|
|
|
impl<Tz: TimeZone> Eq for Date<Tz> {
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 17:03:12 +00:00
|
|
|
impl<Tz: TimeZone> PartialOrd for Date<Tz> {
|
|
|
|
fn partial_cmp(&self, other: &Date<Tz>) -> Option<Ordering> {
|
2014-07-29 06:41:07 +00:00
|
|
|
self.date.partial_cmp(&other.date)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 17:03:12 +00:00
|
|
|
impl<Tz: TimeZone> Ord for Date<Tz> {
|
|
|
|
fn cmp(&self, other: &Date<Tz>) -> Ordering { self.date.cmp(&other.date) }
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
2015-02-21 09:14:09 +00:00
|
|
|
impl<Tz: TimeZone> hash::Hash for Date<Tz> {
|
|
|
|
fn hash<H: hash::Hasher>(&self, state: &mut H) { self.date.hash(state) }
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
2017-02-05 23:17:01 +00:00
|
|
|
impl<Tz: TimeZone> Add<OldDuration> for Date<Tz> {
|
2015-01-12 17:03:12 +00:00
|
|
|
type Output = Date<Tz>;
|
2015-01-05 09:31:15 +00:00
|
|
|
|
2015-01-28 17:31:18 +00:00
|
|
|
#[inline]
|
2017-02-05 23:17:01 +00:00
|
|
|
fn add(self, rhs: OldDuration) -> Date<Tz> {
|
|
|
|
self.checked_add_signed(rhs).expect("`Date + Duration` overflowed")
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-05 23:17:01 +00:00
|
|
|
impl<Tz: TimeZone> Sub<OldDuration> for Date<Tz> {
|
2015-01-12 17:03:12 +00:00
|
|
|
type Output = Date<Tz>;
|
2015-01-05 09:31:15 +00:00
|
|
|
|
2014-11-28 14:53:22 +00:00
|
|
|
#[inline]
|
2017-02-05 23:17:01 +00:00
|
|
|
fn sub(self, rhs: OldDuration) -> Date<Tz> {
|
|
|
|
self.checked_sub_signed(rhs).expect("`Date - Duration` overflowed")
|
2015-01-28 17:31:18 +00:00
|
|
|
}
|
2014-11-28 14:53:22 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 17:03:12 +00:00
|
|
|
impl<Tz: TimeZone> fmt::Debug for Date<Tz> {
|
2015-01-09 18:27:24 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-01-12 16:33:53 +00:00
|
|
|
write!(f, "{:?}{:?}", self.naive_local(), self.offset)
|
2015-01-09 18:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 17:03:12 +00:00
|
|
|
impl<Tz: TimeZone> fmt::Display for Date<Tz> where Tz::Offset: fmt::Display {
|
2014-07-29 06:41:07 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-01-12 16:33:53 +00:00
|
|
|
write!(f, "{}{}", self.naive_local(), self.offset)
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|