2014-04-01 17:14:57 +00:00
|
|
|
// This is a part of rust-chrono.
|
2015-01-04 06:08:19 +00:00
|
|
|
// Copyright (c) 2014-2015, Kang Seonghoon.
|
2014-04-01 17:14:57 +00:00
|
|
|
// See README.md and LICENSE.txt for details.
|
|
|
|
|
2014-03-28 11:38:11 +00:00
|
|
|
/*!
|
2014-07-29 07:14:46 +00:00
|
|
|
* ISO 8601 calendar date with timezone.
|
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};
|
2014-07-29 07:14:46 +00:00
|
|
|
|
|
|
|
use {Weekday, Datelike};
|
2014-03-29 19:52:37 +00:00
|
|
|
use duration::Duration;
|
2014-07-29 06:41:07 +00:00
|
|
|
use offset::{Offset, UTC};
|
2014-07-29 07:14:46 +00:00
|
|
|
use naive;
|
|
|
|
use naive::date::NaiveDate;
|
|
|
|
use naive::time::NaiveTime;
|
|
|
|
use datetime::DateTime;
|
2014-07-31 19:08:19 +00:00
|
|
|
use format::DelayedFormat;
|
2014-03-28 11:38:11 +00:00
|
|
|
|
2014-07-29 06:41:07 +00:00
|
|
|
/// ISO 8601 calendar date with timezone.
|
2015-01-03 22:45:07 +00:00
|
|
|
#[derive(Clone)]
|
2014-07-29 06:41:07 +00:00
|
|
|
pub struct Date<Off> {
|
2014-07-29 06:55:40 +00:00
|
|
|
date: NaiveDate,
|
2014-07-29 06:41:07 +00:00
|
|
|
offset: Off,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The minimum possible `Date`.
|
2014-10-11 15:50:37 +00:00
|
|
|
pub const MIN: Date<UTC> = Date { date: naive::date::MIN, offset: UTC };
|
2014-07-29 06:41:07 +00:00
|
|
|
/// The maximum possible `Date`.
|
2014-10-11 15:50:37 +00:00
|
|
|
pub const MAX: Date<UTC> = Date { date: naive::date::MAX, offset: UTC };
|
2014-07-29 06:41:07 +00:00
|
|
|
|
|
|
|
impl<Off:Offset> Date<Off> {
|
|
|
|
/// Makes a new `Date` with given *UTC* date and offset.
|
|
|
|
/// The local date should be constructed via the `Offset` trait.
|
|
|
|
#[inline]
|
2014-07-29 06:55:40 +00:00
|
|
|
pub fn from_utc(date: NaiveDate, offset: Off) -> Date<Off> {
|
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.
|
|
|
|
///
|
|
|
|
/// Fails on invalid datetime.
|
|
|
|
#[inline]
|
2014-07-29 06:55:40 +00:00
|
|
|
pub fn and_time(&self, time: NaiveTime) -> Option<DateTime<Off>> {
|
2014-12-13 12:53:53 +00:00
|
|
|
let localdt = self.offset.to_local_date(&self.date).and_time(time);
|
|
|
|
self.offset.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.
|
|
|
|
///
|
|
|
|
/// Fails on invalid hour, minute and/or second.
|
|
|
|
#[inline]
|
|
|
|
pub fn and_hms(&self, hour: u32, min: u32, sec: u32) -> DateTime<Off> {
|
|
|
|
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]
|
|
|
|
pub fn and_hms_opt(&self, hour: u32, min: u32, sec: u32) -> Option<DateTime<Off>> {
|
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.
|
|
|
|
///
|
|
|
|
/// 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<Off> {
|
|
|
|
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,
|
|
|
|
milli: u32) -> Option<DateTime<Off>> {
|
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.
|
|
|
|
///
|
|
|
|
/// 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<Off> {
|
|
|
|
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,
|
|
|
|
micro: u32) -> Option<DateTime<Off>> {
|
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.
|
|
|
|
///
|
|
|
|
/// 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<Off> {
|
|
|
|
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,
|
|
|
|
nano: u32) -> Option<DateTime<Off>> {
|
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.
|
|
|
|
///
|
|
|
|
/// Fails when `self` is the last representable date.
|
|
|
|
#[inline]
|
|
|
|
pub fn succ(&self) -> Date<Off> {
|
|
|
|
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<Date<Off>> {
|
|
|
|
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<Off> {
|
|
|
|
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<Date<Off>> {
|
|
|
|
self.date.pred_opt().map(|date| Date::from_utc(date, self.offset.clone()))
|
|
|
|
}
|
|
|
|
|
2014-08-29 09:14:08 +00:00
|
|
|
/// Retrieves an associated offset.
|
|
|
|
#[inline]
|
|
|
|
pub fn offset<'a>(&'a self) -> &'a Off {
|
|
|
|
&self.offset
|
|
|
|
}
|
|
|
|
|
2014-11-28 14:53:22 +00:00
|
|
|
/// Changes the associated offset.
|
|
|
|
/// This does not change the actual `Date` (but will change the string representation).
|
|
|
|
#[inline]
|
|
|
|
pub fn with_offset<Off2:Offset>(&self, offset: Off2) -> Date<Off2> {
|
|
|
|
Date::from_utc(self.date, offset)
|
|
|
|
}
|
|
|
|
|
2014-07-31 19:08:19 +00:00
|
|
|
/// 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)
|
|
|
|
}
|
|
|
|
|
2014-07-29 06:41:07 +00:00
|
|
|
/// Returns a view to the local date.
|
2014-07-29 06:55:40 +00:00
|
|
|
fn local(&self) -> NaiveDate {
|
2014-07-29 06:41:07 +00:00
|
|
|
self.offset.to_local_date(&self.date)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Off:Offset> Datelike for Date<Off> {
|
|
|
|
#[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<Date<Off>> {
|
|
|
|
self.local().with_year(year)
|
|
|
|
.and_then(|date| self.offset.from_local_date(&date).single())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn with_month(&self, month: u32) -> Option<Date<Off>> {
|
|
|
|
self.local().with_month(month)
|
|
|
|
.and_then(|date| self.offset.from_local_date(&date).single())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn with_month0(&self, month0: u32) -> Option<Date<Off>> {
|
|
|
|
self.local().with_month0(month0)
|
|
|
|
.and_then(|date| self.offset.from_local_date(&date).single())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn with_day(&self, day: u32) -> Option<Date<Off>> {
|
|
|
|
self.local().with_day(day)
|
|
|
|
.and_then(|date| self.offset.from_local_date(&date).single())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn with_day0(&self, day0: u32) -> Option<Date<Off>> {
|
|
|
|
self.local().with_day0(day0)
|
|
|
|
.and_then(|date| self.offset.from_local_date(&date).single())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn with_ordinal(&self, ordinal: u32) -> Option<Date<Off>> {
|
|
|
|
self.local().with_ordinal(ordinal)
|
|
|
|
.and_then(|date| self.offset.from_local_date(&date).single())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn with_ordinal0(&self, ordinal0: u32) -> Option<Date<Off>> {
|
|
|
|
self.local().with_ordinal0(ordinal0)
|
|
|
|
.and_then(|date| self.offset.from_local_date(&date).single())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-13 11:29:06 +00:00
|
|
|
impl<Off:Offset, Off2:Offset> PartialEq<Date<Off2>> for Date<Off> {
|
|
|
|
fn eq(&self, other: &Date<Off2>) -> bool { self.date == other.date }
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
2015-01-01 22:11:21 +00:00
|
|
|
impl<Off:Offset> Eq for Date<Off> {
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<Off:Offset> PartialOrd for Date<Off> {
|
|
|
|
fn partial_cmp(&self, other: &Date<Off>) -> Option<Ordering> {
|
|
|
|
self.date.partial_cmp(&other.date)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Off:Offset> Ord for Date<Off> {
|
|
|
|
fn cmp(&self, other: &Date<Off>) -> Ordering { self.date.cmp(&other.date) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Off:Offset> hash::Hash for Date<Off> {
|
|
|
|
fn hash(&self, state: &mut hash::sip::SipState) { self.date.hash(state) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Off:Offset> Add<Duration,Date<Off>> for Date<Off> {
|
2014-12-17 01:28:14 +00:00
|
|
|
fn add(self, rhs: Duration) -> Date<Off> {
|
|
|
|
Date { date: self.date + rhs, offset: self.offset }
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Off:Offset> Add<Date<Off>,Date<Off>> for Duration {
|
|
|
|
#[inline]
|
2014-12-17 01:28:14 +00:00
|
|
|
fn add(self, rhs: Date<Off>) -> Date<Off> { rhs.add(self) }
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<Off:Offset, Off2:Offset> Sub<Date<Off2>,Duration> for Date<Off> {
|
2014-12-17 01:28:14 +00:00
|
|
|
fn sub(self, rhs: Date<Off2>) -> Duration { self.date - rhs.date }
|
2014-07-29 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
2014-11-28 14:53:22 +00:00
|
|
|
impl<Off:Offset> Sub<Duration,Date<Off>> for Date<Off> {
|
|
|
|
#[inline]
|
2014-12-17 01:28:14 +00:00
|
|
|
fn sub(self, rhs: Duration) -> Date<Off> { self.add(-rhs) }
|
2014-11-28 14:53:22 +00:00
|
|
|
}
|
|
|
|
|
2014-07-29 06:41:07 +00:00
|
|
|
impl<Off:Offset> fmt::Show for Date<Off> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}{}", self.local(), self.offset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-13 12:53:53 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2015-01-03 22:45:07 +00:00
|
|
|
use std::borrow::IntoCow;
|
2014-12-13 12:53:53 +00:00
|
|
|
use std::fmt;
|
|
|
|
use std::str::SendStr;
|
|
|
|
|
|
|
|
use duration::Duration;
|
|
|
|
use naive::date::NaiveDate;
|
|
|
|
use naive::time::NaiveTime;
|
|
|
|
use naive::datetime::NaiveDateTime;
|
|
|
|
use super::Date;
|
|
|
|
use time::Time;
|
|
|
|
use datetime::DateTime;
|
|
|
|
use offset::{Offset, LocalResult};
|
|
|
|
|
2015-01-03 22:45:07 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
2014-12-13 12:53:53 +00:00
|
|
|
struct UTC1y; // same to UTC but with an offset of 365 days
|
|
|
|
|
|
|
|
impl Offset for UTC1y {
|
|
|
|
fn name(&self) -> SendStr { "UTC+8760".into_cow() } // yes, no kidding
|
|
|
|
fn local_minus_utc(&self) -> Duration { Duration::zero() }
|
|
|
|
|
|
|
|
fn from_local_date(&self, local: &NaiveDate) -> LocalResult<Date<UTC1y>> {
|
|
|
|
LocalResult::Single(Date::from_utc(*local - Duration::days(365), UTC1y))
|
|
|
|
}
|
|
|
|
fn from_local_time(&self, local: &NaiveTime) -> LocalResult<Time<UTC1y>> {
|
|
|
|
LocalResult::Single(Time::from_utc(local.clone(), UTC1y))
|
|
|
|
}
|
|
|
|
fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<DateTime<UTC1y>> {
|
|
|
|
LocalResult::Single(DateTime::from_utc(*local - Duration::days(365), UTC1y))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_local_date(&self, utc: &NaiveDate) -> NaiveDate { *utc + Duration::days(365) }
|
|
|
|
fn to_local_time(&self, utc: &NaiveTime) -> NaiveTime { utc.clone() }
|
|
|
|
fn to_local_datetime(&self, utc: &NaiveDateTime) -> NaiveDateTime {
|
|
|
|
*utc + Duration::days(365)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Show for UTC1y {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "+8760:00") }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_date_weird_offset() {
|
|
|
|
assert_eq!(UTC1y.ymd(2012, 2, 29).to_string(),
|
|
|
|
"2012-02-29+8760:00".to_string());
|
|
|
|
assert_eq!(UTC1y.ymd(2012, 2, 29).and_hms(5, 6, 7).to_string(),
|
|
|
|
"2012-02-29T05:06:07+8760:00".to_string());
|
|
|
|
assert_eq!(UTC1y.ymd(2012, 3, 4).to_string(),
|
|
|
|
"2012-03-04+8760:00".to_string());
|
|
|
|
assert_eq!(UTC1y.ymd(2012, 3, 4).and_hms(5, 6, 7).to_string(),
|
|
|
|
"2012-03-04T05:06:07+8760:00".to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|