//! The module for the TimeType
//!
use chrono::NaiveDateTime;
use std::ops::Add;
use std::ops::Sub;
use result::Result;
use error::KairosErrorKind as KEK;
use error::KairosError as KE;
use error_chain::ChainedError;
/// A Type of Time, currently based on chrono::NaiveDateTime
#[derive(Debug, Clone)]
pub enum TimeType {
Duration(::chrono::Duration),
Moment(NaiveDateTime),
Addition(Box<TimeType>, Box<TimeType>),
Subtraction(Box<TimeType>, Box<TimeType>),
}
impl Add for TimeType {
type Output = TimeType;
fn add(self, rhs: TimeType) -> Self::Output {
TimeType::Addition(Box::new(self), Box::new(rhs))
}
}
impl Sub for TimeType {
type Output = TimeType;
fn sub(self, rhs: TimeType) -> Self::Output {
TimeType::Subtraction(Box::new(self), Box::new(rhs))
}
}
impl TimeType {
pub fn seconds(i: i64) -> TimeType {
TimeType::Duration(::chrono::Duration::seconds(i))
}
pub fn minutes(i: i64) -> TimeType {
TimeType::Duration(::chrono::Duration::minutes(i))
}
pub fn hours(i: i64) -> TimeType {
TimeType::Duration(::chrono::Duration::hours(i))
}
pub fn days(i: i64) -> TimeType {
TimeType::Duration(::chrono::Duration::days(i))
}
pub fn weeks(i: i64) -> TimeType {
TimeType::Duration(::chrono::Duration::weeks(i))
}
pub fn months(i: i64) -> TimeType {
TimeType::Duration(::chrono::Duration::weeks(i * 4))
}
pub fn years(i: i64) -> TimeType {
TimeType::Duration(::chrono::Duration::weeks(i * 4 * 12))
}
pub fn moment(ndt: NaiveDateTime) -> TimeType {
TimeType::Moment(ndt)
}
/// Get the number of seconds, if the TimeType is not a seconds type, zero is returned
pub fn get_seconds(&self) -> i64 {
match *self {
TimeType::Duration(d) => d.num_seconds(),
_ => 0
}
}
/// Get the number of minutes, if the TimeType is not a minutes type, zero is returned
pub fn get_minutes(&self) -> i64 {
match *self {
TimeType::Duration(d) => d.num_minutes(),
_ => 0,
}
}
/// Get the number of hours, if the TimeType is not a hours type, zero is returned
pub fn get_hours(&self) -> i64 {
match *self {
TimeType::Duration(d) => d.num_hours(),
_ => 0,
}
}
/// Get the number of days, if the TimeType is not a days type, zero is returned
pub fn get_days(&self) -> i64 {
match *self {
TimeType::Duration(d) => d.num_days(),
_ => 0,
}
}
/// Get the number of weeks, if the TimeType is not a weeks type, zero is returned
pub fn get_weeks(&self) -> i64 {
match *self {
TimeType::Duration(d) => d.num_weeks(),
_ => 0,
}
}
/// Get the number of months, if the TimeType is not a months type, zero is returned
pub fn get_months(&self) -> i64 {
match *self {
TimeType::Duration(d) => d.num_weeks() / 4,
_ => 0,
}
}
/// Get the number of years, if the TimeType is not a years type, zero is returned
pub fn get_years(&self) -> i64 {
match *self {
Time