made every type `Clone`able; added `and_*` constructors to `DateZ`.

it allows for, say, `DateZ::from_ymd(2014,1,2).and_hms(3,4,5)`.
This commit is contained in:
Kang Seonghoon 2014-07-29 15:33:46 +09:00
parent f77c8435c2
commit c7c3052892
4 changed files with 92 additions and 12 deletions

View File

@ -9,6 +9,8 @@
use std::{fmt, num}; use std::{fmt, num};
use num::Integer; use num::Integer;
use duration::Duration; use duration::Duration;
use time::TimeZ;
use datetime::DateTimeZ;
use self::internals::{DateImpl, Of, Mdf, YearFlags}; use self::internals::{DateImpl, Of, Mdf, YearFlags};
@ -19,7 +21,7 @@ static MIN_YEAR: i32 = internals::MIN_YEAR as i32;
/// ///
/// The order of the days of week depends on the context. /// 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. /// One should prefer `*_from_monday` or `*_from_sunday` methods to get the correct result.
#[deriving(PartialEq, Eq, FromPrimitive, Show)] #[deriving(PartialEq, Eq, Clone, FromPrimitive, Show)]
pub enum Weekday { pub enum Weekday {
/// Monday. /// Monday.
Mon = 0, Mon = 0,
@ -96,7 +98,7 @@ impl Weekday {
/// Returns a DOW number starting from Monday = 0. /// Returns a DOW number starting from Monday = 0.
#[inline] #[inline]
pub fn ndays_from_monday(&self) -> u32 { pub fn num_days_from_monday(&self) -> u32 {
match *self { match *self {
Mon => 0, Mon => 0,
Tue => 1, Tue => 1,
@ -110,7 +112,7 @@ impl Weekday {
/// Returns a DOW number starting from Sunday = 0. /// Returns a DOW number starting from Sunday = 0.
#[inline] #[inline]
pub fn ndays_from_sunday(&self) -> u32 { pub fn num_days_from_sunday(&self) -> u32 {
match *self { match *self {
Mon => 1, Mon => 1,
Tue => 2, Tue => 2,
@ -219,7 +221,7 @@ pub trait Datelike {
/// ISO 8601 calendar date without timezone. /// ISO 8601 calendar date without timezone.
/// Allows for every proleptic Gregorian date from Jan 1, 262145 BCE to Dec 31, 262143 CE. /// Allows for every proleptic Gregorian date from Jan 1, 262145 BCE to Dec 31, 262143 CE.
/// Also supports the conversion from ISO 8601 ordinal and week date. /// Also supports the conversion from ISO 8601 ordinal and week date.
#[deriving(PartialEq, Eq, PartialOrd, Ord, Hash)] #[deriving(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
pub struct DateZ { pub struct DateZ {
ymdf: DateImpl, // (year << 13) | of ymdf: DateImpl, // (year << 13) | of
} }
@ -329,6 +331,84 @@ impl DateZ {
} }
} }
/// Makes a new `DateTimeZ` from the current date and given `TimeZ`.
#[inline]
pub fn and_time(&self, time: TimeZ) -> DateTimeZ {
DateTimeZ::new(self.clone(), time)
}
/// Makes a new `DateTimeZ` from the current date, hour, minute and second.
///
/// Fails on invalid hour, minute and/or second.
#[inline]
pub fn and_hms(&self, hour: u32, min: u32, sec: u32) -> DateTimeZ {
self.and_hms_opt(hour, min, sec).expect("invalid time")
}
/// Makes a new `DateTimeZ` from the current date, hour, minute and second.
///
/// Returns `None` on invalid hour, minute and/or second.
#[inline]
pub fn and_hms_opt(&self, hour: u32, min: u32, sec: u32) -> Option<DateTimeZ> {
TimeZ::from_hms_opt(hour, min, sec).map(|time| self.and_time(time))
}
/// Makes a new `DateTimeZ` from the current date, hour, minute, second and millisecond.
/// The millisecond part can exceed 1,000 in order to represent the leap second.
///
/// Fails on invalid hour, minute, second and/or millisecond.
#[inline]
pub fn and_hms_milli(&self, hour: u32, min: u32, sec: u32, milli: u32) -> DateTimeZ {
self.and_hms_milli_opt(hour, min, sec, milli).expect("invalid time")
}
/// Makes a new `DateTimeZ` from the current date, hour, minute, second and millisecond.
/// The millisecond part can exceed 1,000 in order to represent the leap second.
///
/// 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<DateTimeZ> {
TimeZ::from_hms_milli_opt(hour, min, sec, milli).map(|time| self.and_time(time))
}
/// Makes a new `DateTimeZ` from the current date, hour, minute, second and microsecond.
/// The microsecond part can exceed 1,000,000 in order to represent the leap second.
///
/// Fails on invalid hour, minute, second and/or microsecond.
#[inline]
pub fn and_hms_micro(&self, hour: u32, min: u32, sec: u32, micro: u32) -> DateTimeZ {
self.and_hms_micro_opt(hour, min, sec, micro).expect("invalid time")
}
/// Makes a new `DateTimeZ` from the current date, hour, minute, second and microsecond.
/// 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.
#[inline]
pub fn and_hms_micro_opt(&self, hour: u32, min: u32, sec: u32,
micro: u32) -> Option<DateTimeZ> {
TimeZ::from_hms_micro_opt(hour, min, sec, micro).map(|time| self.and_time(time))
}
/// Makes a new `DateTimeZ` 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.
///
/// Fails on invalid hour, minute, second and/or nanosecond.
#[inline]
pub fn and_hms_nano(&self, hour: u32, min: u32, sec: u32, nano: u32) -> DateTimeZ {
self.and_hms_nano_opt(hour, min, sec, nano).expect("invalid time")
}
/// Makes a new `DateTimeZ` 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.
///
/// 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<DateTimeZ> {
TimeZ::from_hms_nano_opt(hour, min, sec, nano).map(|time| self.and_time(time))
}
/// Returns the packed month-day-flags. /// Returns the packed month-day-flags.
#[inline] #[inline]
fn mdf(&self) -> Mdf { fn mdf(&self) -> Mdf {

View File

@ -11,8 +11,8 @@ use duration::Duration;
use time::{Timelike, TimeZ}; use time::{Timelike, TimeZ};
use date::{Datelike, DateZ, Weekday}; use date::{Datelike, DateZ, Weekday};
/// ISO 8601 combined date and time. /// ISO 8601 combined date and time without timezone.
#[deriving(PartialEq, Eq, PartialOrd, Ord, Hash)] #[deriving(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
pub struct DateTimeZ { pub struct DateTimeZ {
date: DateZ, date: DateZ,
time: TimeZ, time: TimeZ,
@ -93,19 +93,19 @@ impl DateTimeZ {
} }
} }
/// Retrieves a `Date` component. /// Retrieves a date component.
#[inline] #[inline]
pub fn date(&self) -> DateZ { pub fn date(&self) -> DateZ {
self.date self.date
} }
/// Retrieves a `Time` component. /// Retrieves a time component.
#[inline] #[inline]
pub fn time(&self) -> TimeZ { pub fn time(&self) -> TimeZ {
self.time self.time
} }
/// Returns the number of non-leap seconds since January 1, 1970 0:00:00. /// Returns the number of non-leap seconds since January 1, 1970 0:00:00 UTC.
/// Note that this does *not* account for the timezone! /// Note that this does *not* account for the timezone!
#[inline] #[inline]
pub fn num_seconds_from_unix_epoch(&self) -> i64 { pub fn num_seconds_from_unix_epoch(&self) -> i64 {
@ -255,7 +255,7 @@ mod tests {
} }
#[test] #[test]
fn test_datetime_nseconds_from_unix_epoch() { fn test_datetime_num_seconds_from_unix_epoch() {
let to_timestamp = let to_timestamp =
|y,m,d,h,n,s| DateTimeZ::from_ymdhms(y,m,d,h,n,s).num_seconds_from_unix_epoch(); |y,m,d,h,n,s| DateTimeZ::from_ymdhms(y,m,d,h,n,s).num_seconds_from_unix_epoch();
assert_eq!(to_timestamp(1969, 12, 31, 23, 59, 59), -1); assert_eq!(to_timestamp(1969, 12, 31, 23, 59, 59), -1);

View File

@ -25,7 +25,7 @@ macro_rules! try_opt(
/// ISO 8601 time duration with nanosecond precision. /// ISO 8601 time duration with nanosecond precision.
/// This also allows for the negative duration; see individual methods for details. /// This also allows for the negative duration; see individual methods for details.
#[deriving(PartialEq, Eq, PartialOrd, Ord)] #[deriving(PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct Duration { pub struct Duration {
days: i32, days: i32,
secs: u32, secs: u32,

View File

@ -64,7 +64,7 @@ pub trait Timelike {
/// ISO 8601 time without timezone. /// ISO 8601 time without timezone.
/// Allows for the nanosecond precision and optional leap second representation. /// Allows for the nanosecond precision and optional leap second representation.
#[deriving(PartialEq, Eq, PartialOrd, Ord, Hash)] #[deriving(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
pub struct TimeZ { pub struct TimeZ {
secs: u32, secs: u32,
frac: u32, frac: u32,