chrono/src/time.rs

141 lines
4.0 KiB
Rust
Raw Normal View History

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
/*!
* ISO 8601 time with timezone.
2014-03-28 11:38:11 +00:00
*/
2014-07-29 06:41:07 +00:00
use std::{fmt, hash};
use std::cmp::Ordering;
use std::ops::{Add, Sub};
use Timelike;
2014-07-29 06:41:07 +00:00
use offset::Offset;
2014-03-29 15:12:50 +00:00
use duration::Duration;
use naive::time::NaiveTime;
use format::DelayedFormat;
2014-03-28 11:38:11 +00:00
2014-07-29 06:41:07 +00:00
/// ISO 8601 time with timezone.
#[derive(Clone)]
2014-07-29 06:41:07 +00:00
pub struct Time<Off> {
time: NaiveTime,
2014-07-29 06:41:07 +00:00
offset: Off,
}
impl<Off:Offset> Time<Off> {
/// Makes a new `Time` with given *UTC* time and offset.
/// The local time should be constructed via the `Offset` trait.
#[inline]
pub fn from_utc(time: NaiveTime, offset: Off) -> Time<Off> {
2014-07-29 06:41:07 +00:00
Time { time: time, offset: offset }
}
/// Retrieves an associated offset.
#[inline]
pub fn offset<'a>(&'a self) -> &'a Off {
&self.offset
}
/// Changes the associated offset.
/// This does not change the actual `Time` (but will change the string representation).
#[inline]
pub fn with_offset<Off2:Offset>(&self, offset: Off2) -> Time<Off2> {
Time::from_utc(self.time, offset)
}
/// Formats the time 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(None, Some(self.local()), &self.offset, fmt)
}
2014-07-29 06:41:07 +00:00
/// Returns a view to the local time.
fn local(&self) -> NaiveTime {
2014-07-29 06:41:07 +00:00
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() }
}
2014-12-13 11:29:06 +00:00
impl<Off:Offset, Off2:Offset> PartialEq<Time<Off2>> for Time<Off> {
fn eq(&self, other: &Time<Off2>) -> bool { self.time == other.time }
2014-07-29 06:41:07 +00:00
}
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 }
2014-07-29 06:41:07 +00:00
}
}
impl<Off:Offset> Add<Time<Off>,Time<Off>> for Duration {
#[inline]
fn add(self, rhs: Time<Off>) -> Time<Off> { rhs.add(self) }
2014-07-29 06:41:07 +00:00
}
impl<Off:Offset, Off2:Offset> Sub<Time<Off2>,Duration> for Time<Off> {
fn sub(self, rhs: Time<Off2>) -> Duration { self.time - rhs.time }
2014-07-29 06:41:07 +00:00
}
impl<Off:Offset> Sub<Duration,Time<Off>> for Time<Off> {
#[inline]
fn sub(self, rhs: Duration) -> Time<Off> { self.add(-rhs) }
}
2014-07-29 06:41:07 +00:00
impl<Off:Offset> fmt::Show for Time<Off> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}{}", self.local(), self.offset)
}
}