use std::{fmt, hash};
use Timelike;
use offset::Offset;
use duration::Duration;
use naive::time::NaiveTime;
use format::DelayedFormat;
#[deriving(Clone)]
pub struct Time<Off> {
time: NaiveTime,
offset: Off,
}
impl<Off:Offset> Time<Off> {
#[inline]
pub fn from_utc(time: NaiveTime, offset: Off) -> Time<Off> {
Time { time: time, offset: offset }
}
#[inline]
pub fn offset<'a>(&'a self) -> &'a Off {
&self.offset
}
#[inline]
pub fn with_offset<Off2:Offset>(&self, offset: Off2) -> Time<Off2> {
Time::from_utc(self.time, offset)
}
#[inline]
pub fn format<'a>(&'a self, fmt: &'a str) -> DelayedFormat<'a> {
DelayedFormat::new_with_offset(None, Some(self.local()), &self.offset, fmt)
}
fn local(&self) -> NaiveTime {
self.offset.to_local_time(&self.time)
}
}
impl<Off:Offset> Timelike for Time<Off> {
#[inline] fn hour(&self) -> u32 { self.local().hour() }
#[inline] fn minute(&self) -> u32 { self.local().minute() }
#[inline] fn second(&self) -> u32 { self.local().second() }
#[inline] fn nanosecond(&self) -> u32 { self.local().nanosecond() }
#[inline]
fn with_hour(&self, hour: u32) -> Option<Time<Off>> {
self.local().with_hour(hour)
.and_then(|time| self.offset.from_local_time(&time).single())
}
#[inline]
fn with_minute(&self, min: u32) -> Option<Time<Off>> {
self.local().with_minute(min)
.and_then(|time| self.offset.from_local_time(&time).single())
}
#[inline]
fn with_second(&self, sec: u32) -> Option<Time<Off>> {
self.local().with_second(sec)
.and_then(|time| self.offset.from_local_time(&time).single())
}
#[inline]
fn with_nanosecond(&self, nano: u32) -> Option<Time<Off>> {
self.local().with_nanosecond(nano)
.and_then(|time| self.offset.from_local_time(&time).single())
}
#[inline]
fn num_seconds_from_midnight(&self) -> u32 { self.local().num_seconds_from_midnight() }
}
impl<Off:Offset, Off2:Offset> PartialEq<Time<Off2>> for Time<Off> {
fn eq(&self, other: &Time<Off2>) -> bool { self.time == other.time }
}
impl<Off:Offset> Eq for Time<Off> {
}
impl<Off:Offset> PartialOrd for Time<Off> {
fn partial_cmp(&self, other: &Time<Off>) -> Option<Ordering> {
self.time.partial_cmp(&other.time)
}
}
impl<Off:Offset> Ord for Time<Off> {
fn cmp(&self, other: &Time<Off>) -> Ordering { self.time.cmp(&other.time) }
}
impl<Off:Offset> hash::Hash for Time<Off> {
fn hash(&self, state: &mut hash::sip::SipState) { self.time.hash(state) }
}
impl<Off:Offset> Add<Duration,Time<Off>> for Time<Off> {
fn add(self, rhs: Duration) -> Time<Off> {
Time { time: self.time + rhs, offset: self.offset }
}
}
impl<Off:Offset> Add<Time<Off>,Time<Off>> for Duration {
#[inline]
fn add(self, rhs: Time<Off>) -> Time<Off> { rhs.add(self) }
}
impl<Off:Offset, Off2:Offset> Sub<Time<Off2>,Duration> for Time<Off> {
fn sub(self, rhs: Time<Off2>) -> Duration { self.time - rhs.time }
}
impl<Off:Offset> Sub<Duration,Time<Off>> for Time<Off> {
#[inline]
fn sub(self, rhs: Duration) -> Time<Off> { self.add(-rhs) }
}
impl<Off:Offset> fmt::Show for Time<Off> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}{}", self.local(), self.offset)
}
}