use std::cmp;
use std::convert;
use std::fmt;
use DateTime;
impl DateTime {
pub fn to_utc(&self) -> Result<DateTime, glib::BoolError> {
if !self.has_time() {
return Ok(self.clone());
}
assert!(self.has_year() && self.has_month() && self.has_day() && self.has_time());
if self.has_second() {
self.to_g_date_time()
.and_then(|d| {
d.to_utc()
.ok_or_else(|| glib_bool_error!("Can't convert datetime to UTC"))
})
.and_then(|d| DateTime::from_g_date_time(&d))
} else {
DateTime::new(
self.get_time_zone_offset(),
self.get_year(),
self.get_month(),
self.get_day(),
self.get_hour(),
self.get_minute(),
0f64,
)
.and_then(|d| d.to_g_date_time())
.and_then(|d| {
d.to_utc()
.ok_or_else(|| glib_bool_error!("Can't convert datetime to UTC"))
})
.and_then(|d| {
DateTime::new(
0f32,
d.get_year(),
d.get_month(),
d.get_day_of_month(),
d.get_hour(),
d.get_minute(),
-1f64,
)
})
}
}
}
impl cmp::PartialOrd for DateTime {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
#[inline]
fn get_cmp(delta: i32) -> Option<cmp::Ordering> {
skip_assert_initialized!();
Some(delta.cmp(&0))
}
if !(self.has_year() && other.has_year()) {
return None;
}
let (self_norm, other_norm) = if self.has_time() && other.has_time() {
(self.to_utc().ok()?, other.to_utc().ok()?)
} else {
(self.clone(), other.clone())
};
let year_delta = self_norm.get_year() - other_norm.get_year();
if year_delta != 0 {
return get_cmp(year_delta);
}
if !self.has_month() && !other.has_month() {
return get_cmp(year_delta);
}
if !(self.has_month() && other.has_month()) {
return None;
}
let month_delta = self_norm.get_month() - other_norm.get_month();
if month_delta != 0 {
return get_cmp(month_delta);
}
if !self.has_day() && !other.has_day() {
return Some(cmp::Ordering::Equal);
}
if !(self.has_day() && other.has_day()) {
return None;
}
let day_delta = self_norm.get_day() - other_norm.get_day();
if day_delta != 0 {
return get_cmp(day_delta);
}
if !self.has_time() && !other.has_time() {
return Some(cmp::Ordering::Equal);
}
if !(self.has_time() && other.has_time()) {
return None;
}
let hour_delta = self_norm.get_hour() - other_norm.get_hour();
if hour_delta != 0 {
return get_cmp(hour_delta);
}
let minute_delta = self_norm.get_minute() - other_norm.get_minute();
if minute_delta != 0 {
return get_cmp(minute_delta);
}
if !self.has_second() && !other.has_second() {
return Some(cmp::Ordering::Equal);
}
if !(self.has_second() && other.has_second()) {
return None;
}
let second_delta = self_norm.get_second() - other_norm.get_second();
if second_delta != 0 {
return get_cmp(second_delta);
}
get_cmp(self_norm.get_microsecond() - other_norm.get_microsecond())
}
}
impl cmp::PartialEq for DateTime {
fn eq(&self, other: &Self) -> bool {
self.partial_cmp(other)
.map_or_else(|| false, |cmp| cmp == cmp::Ordering::Equal)
}
}
impl fmt::Debug for DateTime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut debug_struct = f.debug_struct("DateTime");
if self.has_year() {
debug_struct.field("year", &self.get_year());
}
if self.has_month() {
debug_struct.field("month", &self.get_month());
}
if self.has_day() {
debug_struct.field("day", &self.get_day());
}
if self.has_time() {
debug_struct.field("hour", &self.get_hour());
debug_struct.field("minute", &self.get_minute());
if self.has_second() {
debug_struct.field("second", &self.get_second());
debug_struct.field("microsecond", &self.get_microsecond());
}
debug_struct.field("tz_offset", &self.get_time_zone_offset());
}
debug_struct.finish()
}
}
impl fmt::Display for DateTime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(
self.to_iso8601_string()
.unwrap_or_else(|_| "None".into())
.as_str(),
)
}
}
impl<'a> convert::TryFrom<&'a glib::DateTime> for DateTime {
type Error = glib::BoolError;
fn try_from(v: &'a glib::DateTime) -> Result<DateTime, glib::BoolError> {
skip_assert_initialized!();
DateTime::from_g_date_time(v)
}
}
impl convert::TryFrom<glib::DateTime> for DateTime {
type Error = glib::BoolError;
fn try_from(v: glib::DateTime) -> Result<DateTime, glib::BoolError> {
skip_assert_initialized!();
DateTime::from_g_date_time(&v)
}
}
impl<'a> convert::TryFrom<&'a DateTime> for glib::DateTime {
type Error = glib::BoolError;
fn try_from(v: &'a DateTime) -> Result<glib::DateTime, glib::BoolError> {
skip_assert_initialized!();
v.to_g_date_time()
}
}
impl convert::TryFrom<DateTime> for glib::DateTime {
type Error = glib::BoolError;
fn try_from(v: DateTime) -> Result<glib::DateTime, glib::BoolError> {
skip_assert_initialized!();
v.to_g_date_time()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[allow(clippy::cognitive_complexity)]
#[test]
fn test_to_utc() {
::init().unwrap();
let utc_date_time = DateTime::new(2f32, 2019, 8, 20, 20, 9, 42.123_456f64)
.unwrap()
.to_utc()
.unwrap();
assert_eq!(utc_date_time.get_year(), 2019);
assert_eq!(utc_date_time.get_month(), 8);
assert_eq!(utc_date_time.get_day(), 20);
assert_eq!(utc_date_time.get_hour(), 18);
assert_eq!(utc_date_time.get_minute(), 9);
assert_eq!(utc_date_time.get_second(), 42);
assert_eq!(utc_date_time.get_microsecond(), 123_456);
let utc_date_time = DateTime::new(2f32, 2019, 1, 1, 0, 0, 42.123_456f64)
.unwrap()
.to_utc()
.unwrap();
assert_eq!(utc_date_time.get_year(), 2018);
assert_eq!(utc_date_time.get_month(), 12);
assert_eq!(utc_date_time.get_day(), 31);
assert_eq!(utc_date_time.get_hour(), 22);
assert_eq!(utc_date_time.get_minute(), 0);
assert_eq!(utc_date_time.get_second(), 42);
assert_eq!(utc_date_time.get_microsecond(), 123_456);
let utc_date_time = DateTime::new_ymd(2019, 1, 1).unwrap().to_utc().unwrap();
assert_eq!(utc_date_time.get_year(), 2019);
assert_eq!(utc_date_time.get_month(), 1);
assert_eq!(utc_date_time.get_day(), 1);
assert!(!utc_date_time.has_time());
assert!(!utc_date_time.has_second());
let utc_date_time = DateTime::new(2f32, 2018, 5, 28, 16, 6, -1f64)
.unwrap()
.to_utc()
.unwrap();
assert_eq!(utc_date_time.get_year(), 2018);
assert_eq!(utc_date_time.get_month(), 5);
assert_eq!(utc_date_time.get_day(), 28);
assert_eq!(utc_date_time.get_hour(), 14);
assert_eq!(utc_date_time.get_minute(), 6);
assert!(!utc_date_time.has_second());
}
#[test]
fn test_partial_ord() {
::init().unwrap();
assert!(
DateTime::new(2f32, 2020, 8, 20, 19, 43, 42.123_456f64).unwrap()
> DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_456f64).unwrap()
);
assert!(
DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_456f64).unwrap()
< DateTime::new(2f32, 2019, 9, 19, 19, 43, 42.123_456f64).unwrap()
);
assert!(
DateTime::new(2f32, 2019, 8, 21, 19, 43, 42.123_456f64).unwrap()
> DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_456f64).unwrap()
);
assert!(
DateTime::new(2f32, 2019, 8, 20, 19, 44, 42.123_456f64).unwrap()
> DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_456f64).unwrap()
);
assert!(
DateTime::new(2f32, 2019, 8, 20, 19, 43, 44.123_456f64).unwrap()
> DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_456f64).unwrap()
);
assert!(
DateTime::new(2f32, 2019, 8, 20, 19, 43, 43.123_456f64).unwrap()
> DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_456f64).unwrap()
);
assert!(
DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_457f64).unwrap()
> DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_456f64).unwrap()
);
assert!(
DateTime::new(1f32, 2019, 8, 20, 19, 43, 42.123_456f64).unwrap()
> DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_456f64).unwrap()
);
assert!(
DateTime::new(2f32, 2019, 1, 1, 0, 0, 0f64).unwrap()
< DateTime::new(1f32, 2018, 12, 31, 23, 59, 0f64).unwrap()
);
assert!(DateTime::new_ymd(2020, 8, 20).unwrap() > DateTime::new_ymd(2019, 8, 20).unwrap());
assert!(DateTime::new_ymd(2019, 9, 20).unwrap() > DateTime::new_ymd(2019, 8, 20).unwrap());
assert!(DateTime::new_ymd(2019, 8, 21).unwrap() > DateTime::new_ymd(2019, 8, 20).unwrap());
assert!(DateTime::new_ym(2020, 8).unwrap() > DateTime::new_ym(2019, 8).unwrap());
assert!(DateTime::new_ym(2019, 9).unwrap() > DateTime::new_ym(2019, 8).unwrap());
assert!(DateTime::new_ym(2019, 9).unwrap() > DateTime::new_ymd(2019, 8, 20).unwrap());
assert!(DateTime::new_y(2020).unwrap() > DateTime::new_y(2019).unwrap());
assert!(DateTime::new_ym(2020, 1).unwrap() > DateTime::new_y(2019).unwrap());
assert!(
DateTime::new(2f32, 2019, 8, 20, 19, 43, 44.123_456f64).unwrap()
< DateTime::new_ymd(2020, 8, 20).unwrap()
);
assert!(
DateTime::new_ymd(2020, 8, 20).unwrap()
> DateTime::new(2f32, 2019, 8, 20, 19, 43, 44.123_456f64).unwrap()
);
assert!(
DateTime::new_ymd(2020, 1, 1).unwrap()
> DateTime::new(-2f32, 2019, 12, 31, 23, 59, 0f64).unwrap()
);
assert!(DateTime::new(2f32, 2019, 8, 20, 19, 43, 44.123_456f64)
.unwrap()
.partial_cmp(&DateTime::new_ymd(2019, 8, 20).unwrap())
.is_none());
assert!(DateTime::new_ymd(2019, 8, 20)
.unwrap()
.partial_cmp(&DateTime::new(2f32, 2019, 8, 20, 19, 43, 44.123_456f64).unwrap())
.is_none());
assert!(DateTime::new_ym(2019, 1)
.unwrap()
.partial_cmp(&DateTime::new_y(2019).unwrap())
.is_none());
}
#[test]
fn test_eq() {
::init().unwrap();
assert_eq!(
DateTime::new(2f32, 2018, 5, 28, 16, 6, 42.123_456f64).unwrap(),
DateTime::new(2f32, 2018, 5, 28, 16, 6, 42.123_456f64).unwrap()
);
assert_eq!(
DateTime::new(2f32, 2018, 5, 28, 16, 6, 0f64).unwrap(),
DateTime::new(2f32, 2018, 5, 28, 16, 6, 0f64).unwrap()
);
assert_eq!(
DateTime::new(2f32, 2018, 5, 28, 16, 6, -1f64).unwrap(),
DateTime::new(2f32, 2018, 5, 28, 16, 6, -1f64).unwrap()
);
assert_eq!(
DateTime::new_ymd(2018, 5, 28).unwrap(),
DateTime::new_ymd(2018, 5, 28).unwrap()
);
assert_ne!(
DateTime::new_ymd(2018, 5, 28).unwrap(),
DateTime::new(2f32, 2018, 5, 28, 16, 6, -1f64).unwrap()
);
assert_ne!(
DateTime::new(2f32, 2018, 5, 28, 16, 6, -1f64).unwrap(),
DateTime::new_ym(2018, 5).unwrap()
);
assert_ne!(
DateTime::new(2f32, 2018, 5, 28, 16, 6, -1f64).unwrap(),
DateTime::new_y(2018).unwrap()
);
}
}