gstreamer/datetime: new_from_g_date_time() can actually return NULL

See https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/411
This commit is contained in:
Sebastian Dröge 2020-03-22 12:23:16 +02:00
parent ca6bcf36ab
commit 7dd0404927
7 changed files with 166 additions and 105 deletions

View file

@ -1424,10 +1424,34 @@ status = "generate"
# Don't auto-implement traits # Don't auto-implement traits
name = "" name = ""
[[object.function]]
name = "new"
[object.function.return]
nullable = true
nullable_return_is_error = "Can't create DateTime"
[[object.function]]
name = "new_y"
[object.function.return]
nullable = true
nullable_return_is_error = "Can't create DateTime"
[[object.function]]
name = "new_ym"
[object.function.return]
nullable = true
nullable_return_is_error = "Can't create DateTime"
[[object.function]]
name = "new_ymd"
[object.function.return]
nullable = true
nullable_return_is_error = "Can't create DateTime"
[[object.function]] [[object.function]]
name = "new_from_g_date_time" name = "new_from_g_date_time"
[object.function.return] [object.function.return]
nullable = false nullable_return_is_error = "Can't create DateTime from glib::DateTime"
[[object.function]] [[object.function]]
name = "to_g_date_time" name = "to_g_date_time"

View file

@ -27,21 +27,23 @@ impl DateTime {
hour: i32, hour: i32,
minute: i32, minute: i32,
seconds: f64, seconds: f64,
) -> DateTime { ) -> Result<DateTime, glib::BoolError> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
unsafe { unsafe {
from_glib_full(gst_sys::gst_date_time_new( Option::<_>::from_glib_full(gst_sys::gst_date_time_new(
tzoffset, year, month, day, hour, minute, seconds, tzoffset, year, month, day, hour, minute, seconds,
)) ))
.ok_or_else(|| glib_bool_error!("Can't create DateTime"))
} }
} }
pub fn new_from_g_date_time(dt: &glib::DateTime) -> DateTime { pub fn new_from_g_date_time(dt: &glib::DateTime) -> Result<DateTime, glib::BoolError> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
unsafe { unsafe {
from_glib_full(gst_sys::gst_date_time_new_from_g_date_time( Option::<_>::from_glib_full(gst_sys::gst_date_time_new_from_g_date_time(
dt.to_glib_full(), dt.to_glib_full(),
)) ))
.ok_or_else(|| glib_bool_error!("Can't create DateTime from glib::DateTime"))
} }
} }
@ -91,19 +93,28 @@ impl DateTime {
unsafe { from_glib_full(gst_sys::gst_date_time_new_now_utc()) } unsafe { from_glib_full(gst_sys::gst_date_time_new_now_utc()) }
} }
pub fn new_y(year: i32) -> DateTime { pub fn new_y(year: i32) -> Result<DateTime, glib::BoolError> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
unsafe { from_glib_full(gst_sys::gst_date_time_new_y(year)) } unsafe {
Option::<_>::from_glib_full(gst_sys::gst_date_time_new_y(year))
.ok_or_else(|| glib_bool_error!("Can't create DateTime"))
}
} }
pub fn new_ym(year: i32, month: i32) -> DateTime { pub fn new_ym(year: i32, month: i32) -> Result<DateTime, glib::BoolError> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
unsafe { from_glib_full(gst_sys::gst_date_time_new_ym(year, month)) } unsafe {
Option::<_>::from_glib_full(gst_sys::gst_date_time_new_ym(year, month))
.ok_or_else(|| glib_bool_error!("Can't create DateTime"))
}
} }
pub fn new_ymd(year: i32, month: i32, day: i32) -> DateTime { pub fn new_ymd(year: i32, month: i32, day: i32) -> Result<DateTime, glib::BoolError> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
unsafe { from_glib_full(gst_sys::gst_date_time_new_ymd(year, month, day)) } unsafe {
Option::<_>::from_glib_full(gst_sys::gst_date_time_new_ymd(year, month, day))
.ok_or_else(|| glib_bool_error!("Can't create DateTime"))
}
} }
pub fn get_day(&self) -> i32 { pub fn get_day(&self) -> i32 {

View file

@ -26,11 +26,11 @@ impl DateTime {
// however, the resulting instance can't be translated to `glib::DateTime` // however, the resulting instance can't be translated to `glib::DateTime`
if self.has_second() { if self.has_second() {
self.to_g_date_time() self.to_g_date_time()
.expect("DateTime::to_utc: to_g_date_time") .and_then(|d| {
.to_utc() d.to_utc()
.as_ref() .ok_or_else(|| glib_bool_error!("Can't convert datetime to UTC"))
.ok_or_else(|| glib_bool_error!("Can't convert datetime to UTC")) })
.map(DateTime::new_from_g_date_time) .and_then(|d| DateTime::new_from_g_date_time(&d))
} else { } else {
// It would be cheaper to build a `glib::DateTime` direcly, unfortunetaly // It would be cheaper to build a `glib::DateTime` direcly, unfortunetaly
// this would require using `glib::TimeZone::new_offset` which is feature-gated // this would require using `glib::TimeZone::new_offset` which is feature-gated
@ -45,18 +45,19 @@ impl DateTime {
self.get_minute(), self.get_minute(),
0f64, 0f64,
) )
.to_g_date_time() .and_then(|d| d.to_g_date_time())
.expect("DateTime::to_utc: to_g_date_time") .and_then(|d| {
.to_utc() d.to_utc()
.ok_or_else(|| glib_bool_error!("Can't convert datetime to UTC")) .ok_or_else(|| glib_bool_error!("Can't convert datetime to UTC"))
.map(|g_date_time_utc| { })
.and_then(|d| {
DateTime::new( DateTime::new(
0f32, // UTC TZ offset 0f32, // UTC TZ offset
g_date_time_utc.get_year(), d.get_year(),
g_date_time_utc.get_month(), d.get_month(),
g_date_time_utc.get_day_of_month(), d.get_day_of_month(),
g_date_time_utc.get_hour(), d.get_hour(),
g_date_time_utc.get_minute(), d.get_minute(),
-1f64, // No second -1f64, // No second
) )
}) })
@ -240,6 +241,7 @@ mod tests {
// Hour offset // Hour offset
let utc_date_time = DateTime::new(2f32, 2019, 8, 20, 20, 9, 42.123_456f64) let utc_date_time = DateTime::new(2f32, 2019, 8, 20, 20, 9, 42.123_456f64)
.unwrap()
.to_utc() .to_utc()
.unwrap(); .unwrap();
assert_eq!(utc_date_time.get_year(), 2019); assert_eq!(utc_date_time.get_year(), 2019);
@ -252,6 +254,7 @@ mod tests {
// Year, month, day and hour offset // Year, month, day and hour offset
let utc_date_time = DateTime::new(2f32, 2019, 1, 1, 0, 0, 42.123_456f64) let utc_date_time = DateTime::new(2f32, 2019, 1, 1, 0, 0, 42.123_456f64)
.unwrap()
.to_utc() .to_utc()
.unwrap(); .unwrap();
assert_eq!(utc_date_time.get_year(), 2018); assert_eq!(utc_date_time.get_year(), 2018);
@ -263,7 +266,7 @@ mod tests {
assert_eq!(utc_date_time.get_microsecond(), 123_456); assert_eq!(utc_date_time.get_microsecond(), 123_456);
// Date without an hour (which implies no TZ) // Date without an hour (which implies no TZ)
let utc_date_time = DateTime::new_ymd(2019, 1, 1).to_utc().unwrap(); 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_year(), 2019);
assert_eq!(utc_date_time.get_month(), 1); assert_eq!(utc_date_time.get_month(), 1);
assert_eq!(utc_date_time.get_day(), 1); assert_eq!(utc_date_time.get_day(), 1);
@ -272,6 +275,7 @@ mod tests {
// Date without seconds // Date without seconds
let utc_date_time = DateTime::new(2f32, 2018, 5, 28, 16, 6, -1f64) let utc_date_time = DateTime::new(2f32, 2018, 5, 28, 16, 6, -1f64)
.unwrap()
.to_utc() .to_utc()
.unwrap(); .unwrap();
assert_eq!(utc_date_time.get_year(), 2018); assert_eq!(utc_date_time.get_year(), 2018);
@ -288,96 +292,102 @@ mod tests {
// Different years // Different years
assert!( assert!(
DateTime::new(2f32, 2020, 8, 20, 19, 43, 42.123_456f64) DateTime::new(2f32, 2020, 8, 20, 19, 43, 42.123_456f64).unwrap()
> DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_456f64) > DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_456f64).unwrap()
); );
// Different months (order intentionally reversed) // Different months (order intentionally reversed)
assert!( assert!(
DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_456f64) DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_456f64).unwrap()
< DateTime::new(2f32, 2019, 9, 19, 19, 43, 42.123_456f64) < DateTime::new(2f32, 2019, 9, 19, 19, 43, 42.123_456f64).unwrap()
); );
// Different days // Different days
assert!( assert!(
DateTime::new(2f32, 2019, 8, 21, 19, 43, 42.123_456f64) DateTime::new(2f32, 2019, 8, 21, 19, 43, 42.123_456f64).unwrap()
> DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_456f64) > DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_456f64).unwrap()
); );
// Different hours // Different hours
assert!( assert!(
DateTime::new(2f32, 2019, 8, 20, 19, 44, 42.123_456f64) DateTime::new(2f32, 2019, 8, 20, 19, 44, 42.123_456f64).unwrap()
> DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_456f64) > DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_456f64).unwrap()
); );
// Different minutes // Different minutes
assert!( assert!(
DateTime::new(2f32, 2019, 8, 20, 19, 43, 44.123_456f64) DateTime::new(2f32, 2019, 8, 20, 19, 43, 44.123_456f64).unwrap()
> DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_456f64) > DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_456f64).unwrap()
); );
// Different seconds // Different seconds
assert!( assert!(
DateTime::new(2f32, 2019, 8, 20, 19, 43, 43.123_456f64) DateTime::new(2f32, 2019, 8, 20, 19, 43, 43.123_456f64).unwrap()
> DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_456f64) > DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_456f64).unwrap()
); );
// Different micro-seconds // Different micro-seconds
assert!( assert!(
DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_457f64) DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_457f64).unwrap()
> DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_456f64) > DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_456f64).unwrap()
); );
// Different TZ offsets // Different TZ offsets
assert!( assert!(
DateTime::new(1f32, 2019, 8, 20, 19, 43, 42.123_456f64) DateTime::new(1f32, 2019, 8, 20, 19, 43, 42.123_456f64).unwrap()
> DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_456f64) > DateTime::new(2f32, 2019, 8, 20, 19, 43, 42.123_456f64).unwrap()
); );
// TZ offset leading to year, month, day, hour offset // TZ offset leading to year, month, day, hour offset
assert!( assert!(
DateTime::new(2f32, 2019, 1, 1, 0, 0, 0f64) DateTime::new(2f32, 2019, 1, 1, 0, 0, 0f64).unwrap()
< DateTime::new(1f32, 2018, 12, 31, 23, 59, 0f64) < DateTime::new(1f32, 2018, 12, 31, 23, 59, 0f64).unwrap()
); );
// Partially defined `DateTime` // Partially defined `DateTime`
assert!(DateTime::new_ymd(2020, 8, 20) > DateTime::new_ymd(2019, 8, 20)); assert!(DateTime::new_ymd(2020, 8, 20).unwrap() > DateTime::new_ymd(2019, 8, 20).unwrap());
assert!(DateTime::new_ymd(2019, 9, 20) > DateTime::new_ymd(2019, 8, 20)); assert!(DateTime::new_ymd(2019, 9, 20).unwrap() > DateTime::new_ymd(2019, 8, 20).unwrap());
assert!(DateTime::new_ymd(2019, 8, 21) > DateTime::new_ymd(2019, 8, 20)); assert!(DateTime::new_ymd(2019, 8, 21).unwrap() > DateTime::new_ymd(2019, 8, 20).unwrap());
assert!(DateTime::new_ym(2020, 8) > DateTime::new_ym(2019, 8)); assert!(DateTime::new_ym(2020, 8).unwrap() > DateTime::new_ym(2019, 8).unwrap());
assert!(DateTime::new_ym(2019, 9) > DateTime::new_ym(2019, 8)); assert!(DateTime::new_ym(2019, 9).unwrap() > DateTime::new_ym(2019, 8).unwrap());
assert!(DateTime::new_ym(2019, 9) > DateTime::new_ymd(2019, 8, 20)); assert!(DateTime::new_ym(2019, 9).unwrap() > DateTime::new_ymd(2019, 8, 20).unwrap());
assert!(DateTime::new_y(2020) > DateTime::new_y(2019)); assert!(DateTime::new_y(2020).unwrap() > DateTime::new_y(2019).unwrap());
assert!(DateTime::new_ym(2020, 1) > DateTime::new_y(2019)); assert!(DateTime::new_ym(2020, 1).unwrap() > DateTime::new_y(2019).unwrap());
assert!( assert!(
DateTime::new(2f32, 2019, 8, 20, 19, 43, 44.123_456f64) DateTime::new(2f32, 2019, 8, 20, 19, 43, 44.123_456f64).unwrap()
< DateTime::new_ymd(2020, 8, 20) < DateTime::new_ymd(2020, 8, 20).unwrap()
); );
assert!( assert!(
DateTime::new_ymd(2020, 8, 20) DateTime::new_ymd(2020, 8, 20).unwrap()
> DateTime::new(2f32, 2019, 8, 20, 19, 43, 44.123_456f64) > DateTime::new(2f32, 2019, 8, 20, 19, 43, 44.123_456f64).unwrap()
); );
// Comparison occurs on the same TZ when the `DateTime` doesn't have time (note 2) // Comparison occurs on the same TZ when the `DateTime` doesn't have time (note 2)
assert!(DateTime::new_ymd(2020, 1, 1) > DateTime::new(-2f32, 2019, 12, 31, 23, 59, 0f64)); assert!(
DateTime::new_ymd(2020, 1, 1).unwrap()
> DateTime::new(-2f32, 2019, 12, 31, 23, 59, 0f64).unwrap()
);
// In the following cases, the partially defined `DateTime` is a range WRT // In the following cases, the partially defined `DateTime` is a range WRT
// the fully defined `DateTime` and this range includes the fully defined `DateTime`, // the fully defined `DateTime` and this range includes the fully defined `DateTime`,
// but we can't tell if it's before or after and they are not equal (note 1) // but we can't tell if it's before or after and they are not equal (note 1)
assert!(DateTime::new(2f32, 2019, 8, 20, 19, 43, 44.123_456f64) assert!(DateTime::new(2f32, 2019, 8, 20, 19, 43, 44.123_456f64)
.partial_cmp(&DateTime::new_ymd(2019, 8, 20)) .unwrap()
.partial_cmp(&DateTime::new_ymd(2019, 8, 20).unwrap())
.is_none()); .is_none());
assert!(DateTime::new_ymd(2019, 8, 20) assert!(DateTime::new_ymd(2019, 8, 20)
.partial_cmp(&DateTime::new(2f32, 2019, 8, 20, 19, 43, 44.123_456f64)) .unwrap()
.partial_cmp(&DateTime::new(2f32, 2019, 8, 20, 19, 43, 44.123_456f64).unwrap())
.is_none()); .is_none());
assert!(DateTime::new_ym(2019, 1) assert!(DateTime::new_ym(2019, 1)
.partial_cmp(&DateTime::new_y(2019)) .unwrap()
.partial_cmp(&DateTime::new_y(2019).unwrap())
.is_none()); .is_none());
} }
@ -386,41 +396,41 @@ mod tests {
::init().unwrap(); ::init().unwrap();
assert_eq!( assert_eq!(
DateTime::new(2f32, 2018, 5, 28, 16, 6, 42.123_456f64), DateTime::new(2f32, 2018, 5, 28, 16, 6, 42.123_456f64).unwrap(),
DateTime::new(2f32, 2018, 5, 28, 16, 6, 42.123_456f64) DateTime::new(2f32, 2018, 5, 28, 16, 6, 42.123_456f64).unwrap()
); );
assert_eq!( assert_eq!(
DateTime::new(2f32, 2018, 5, 28, 16, 6, 0f64), DateTime::new(2f32, 2018, 5, 28, 16, 6, 0f64).unwrap(),
DateTime::new(2f32, 2018, 5, 28, 16, 6, 0f64) DateTime::new(2f32, 2018, 5, 28, 16, 6, 0f64).unwrap()
); );
assert_eq!( assert_eq!(
DateTime::new(2f32, 2018, 5, 28, 16, 6, -1f64), DateTime::new(2f32, 2018, 5, 28, 16, 6, -1f64).unwrap(),
DateTime::new(2f32, 2018, 5, 28, 16, 6, -1f64) DateTime::new(2f32, 2018, 5, 28, 16, 6, -1f64).unwrap()
); );
assert_eq!( assert_eq!(
DateTime::new_ymd(2018, 5, 28), DateTime::new_ymd(2018, 5, 28).unwrap(),
DateTime::new_ymd(2018, 5, 28) DateTime::new_ymd(2018, 5, 28).unwrap()
); );
// In the following cases, the partially defined `DateTime` is a range WRT // In the following cases, the partially defined `DateTime` is a range WRT
// the fully defined `DateTime` and this range includes the fully defined `DateTime`, // the fully defined `DateTime` and this range includes the fully defined `DateTime`,
// but they are not equal (note 1) // but they are not equal (note 1)
assert_ne!( assert_ne!(
DateTime::new_ymd(2018, 5, 28), DateTime::new_ymd(2018, 5, 28).unwrap(),
DateTime::new(2f32, 2018, 5, 28, 16, 6, -1f64) DateTime::new(2f32, 2018, 5, 28, 16, 6, -1f64).unwrap()
); );
assert_ne!( assert_ne!(
DateTime::new(2f32, 2018, 5, 28, 16, 6, -1f64), DateTime::new(2f32, 2018, 5, 28, 16, 6, -1f64).unwrap(),
DateTime::new_ym(2018, 5) DateTime::new_ym(2018, 5).unwrap()
); );
assert_ne!( assert_ne!(
DateTime::new(2f32, 2018, 5, 28, 16, 6, -1f64), DateTime::new(2f32, 2018, 5, 28, 16, 6, -1f64).unwrap(),
DateTime::new_y(2018) DateTime::new_y(2018).unwrap()
); );
} }
} }

View file

@ -135,8 +135,10 @@ impl<'de> Deserialize<'de> for Date {
} }
#[allow(clippy::many_single_char_names)] #[allow(clippy::many_single_char_names)]
impl From<DateTimeVariants> for DateTime { impl TryFrom<DateTimeVariants> for DateTime {
fn from(dt_variant: DateTimeVariants) -> Self { type Error = glib::BoolError;
fn try_from(dt_variant: DateTimeVariants) -> Result<Self, Self::Error> {
match dt_variant { match dt_variant {
DateTimeVariants::Y(y) => DateTime::new_y(y), DateTimeVariants::Y(y) => DateTime::new_y(y),
DateTimeVariants::YM(y, m) => DateTime::new_ym(y, m), DateTimeVariants::YM(y, m) => DateTime::new_ym(y, m),
@ -153,7 +155,8 @@ impl From<DateTimeVariants> for DateTime {
impl<'de> Deserialize<'de> for DateTime { impl<'de> Deserialize<'de> for DateTime {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
DateTimeVariants::deserialize(deserializer).map(|dt_variant| dt_variant.into()) DateTimeVariants::deserialize(deserializer)
.and_then(|dt_variant| dt_variant.try_into().map_err(D::Error::custom))
} }
} }
@ -171,7 +174,7 @@ mod tests {
let mut pretty_config = ron::ser::PrettyConfig::default(); let mut pretty_config = ron::ser::PrettyConfig::default();
pretty_config.new_line = "".to_string(); pretty_config.new_line = "".to_string();
let datetime = DateTime::new(2f32, 2018, 5, 28, 16, 6, 42.123_456f64); let datetime = DateTime::new(2f32, 2018, 5, 28, 16, 6, 42.123_456f64).unwrap();
let res = ron::ser::to_string_pretty(&datetime, pretty_config.clone()); let res = ron::ser::to_string_pretty(&datetime, pretty_config.clone());
assert_eq!( assert_eq!(
Ok("YMDhmsTz(2018, 5, 28, 16, 6, 42.123456, 2)".to_owned()), Ok("YMDhmsTz(2018, 5, 28, 16, 6, 42.123456, 2)".to_owned()),
@ -184,19 +187,19 @@ mod tests {
res res
); );
let datetime = DateTime::new(2f32, 2018, 5, 28, 16, 6, -1f64); let datetime = DateTime::new(2f32, 2018, 5, 28, 16, 6, -1f64).unwrap();
let res = ron::ser::to_string_pretty(&datetime, pretty_config.clone()); let res = ron::ser::to_string_pretty(&datetime, pretty_config.clone());
assert_eq!(Ok("YMDhmTz(2018, 5, 28, 16, 6, 2)".to_owned()), res,); assert_eq!(Ok("YMDhmTz(2018, 5, 28, 16, 6, 2)".to_owned()), res,);
let datetime = DateTime::new_ymd(2018, 5, 28); let datetime = DateTime::new_ymd(2018, 5, 28).unwrap();
let res = ron::ser::to_string_pretty(&datetime, pretty_config.clone()); let res = ron::ser::to_string_pretty(&datetime, pretty_config.clone());
assert_eq!(Ok("YMD(2018, 5, 28)".to_owned()), res); assert_eq!(Ok("YMD(2018, 5, 28)".to_owned()), res);
let datetime = DateTime::new_ym(2018, 5); let datetime = DateTime::new_ym(2018, 5).unwrap();
let res = ron::ser::to_string_pretty(&datetime, pretty_config.clone()); let res = ron::ser::to_string_pretty(&datetime, pretty_config.clone());
assert_eq!(Ok("YM(2018, 5)".to_owned()), res); assert_eq!(Ok("YM(2018, 5)".to_owned()), res);
let datetime = DateTime::new_y(2018); let datetime = DateTime::new_y(2018).unwrap();
let res = ron::ser::to_string_pretty(&datetime, pretty_config); let res = ron::ser::to_string_pretty(&datetime, pretty_config);
assert_eq!(Ok("Y(2018)".to_owned()), res); assert_eq!(Ok("Y(2018)".to_owned()), res);
} }
@ -209,58 +212,61 @@ mod tests {
let datetime_de: DateTime = ron::de::from_str(datetime_ron).unwrap(); let datetime_de: DateTime = ron::de::from_str(datetime_ron).unwrap();
assert_eq!( assert_eq!(
datetime_de, datetime_de,
DateTime::new(2f32, 2018, 5, 28, 16, 6, 42.123_456f64) DateTime::new(2f32, 2018, 5, 28, 16, 6, 42.123_456f64).unwrap()
); );
let datetime_json = r#"{"YMDhmsTz":[2018,5,28,16,6,42.123456,2.0]}"#; let datetime_json = r#"{"YMDhmsTz":[2018,5,28,16,6,42.123456,2.0]}"#;
let datetime_de: DateTime = serde_json::from_str(datetime_json).unwrap(); let datetime_de: DateTime = serde_json::from_str(datetime_json).unwrap();
assert_eq!( assert_eq!(
datetime_de, datetime_de,
DateTime::new(2f32, 2018, 5, 28, 16, 6, 42.123_456f64) DateTime::new(2f32, 2018, 5, 28, 16, 6, 42.123_456f64).unwrap()
); );
let datetime_ron = "YMDhmTz(2018, 5, 28, 16, 6, 2)"; let datetime_ron = "YMDhmTz(2018, 5, 28, 16, 6, 2)";
let datetime_de: DateTime = ron::de::from_str(datetime_ron).unwrap(); let datetime_de: DateTime = ron::de::from_str(datetime_ron).unwrap();
assert_eq!(datetime_de, DateTime::new(2f32, 2018, 5, 28, 16, 6, -1f64)); assert_eq!(
datetime_de,
DateTime::new(2f32, 2018, 5, 28, 16, 6, -1f64).unwrap()
);
let datetime_ron = "YMD(2018, 5, 28)"; let datetime_ron = "YMD(2018, 5, 28)";
let datetime_de: DateTime = ron::de::from_str(datetime_ron).unwrap(); let datetime_de: DateTime = ron::de::from_str(datetime_ron).unwrap();
assert_eq!(datetime_de, DateTime::new_ymd(2018, 5, 28)); assert_eq!(datetime_de, DateTime::new_ymd(2018, 5, 28).unwrap());
let datetime_ron = "YM(2018, 5)"; let datetime_ron = "YM(2018, 5)";
let datetime_de: DateTime = ron::de::from_str(datetime_ron).unwrap(); let datetime_de: DateTime = ron::de::from_str(datetime_ron).unwrap();
assert_eq!(datetime_de, DateTime::new_ym(2018, 5)); assert_eq!(datetime_de, DateTime::new_ym(2018, 5).unwrap());
let datetime_ron = "Y(2018)"; let datetime_ron = "Y(2018)";
let datetime_de: DateTime = ron::de::from_str(datetime_ron).unwrap(); let datetime_de: DateTime = ron::de::from_str(datetime_ron).unwrap();
assert_eq!(datetime_de, DateTime::new_y(2018)); assert_eq!(datetime_de, DateTime::new_y(2018).unwrap());
} }
#[test] #[test]
fn test_serde_roundtrip() { fn test_serde_roundtrip() {
::init().unwrap(); ::init().unwrap();
let datetime = DateTime::new(2f32, 2018, 5, 28, 16, 6, 42.123_456f64); let datetime = DateTime::new(2f32, 2018, 5, 28, 16, 6, 42.123_456f64).unwrap();
let datetime_ser = ron::ser::to_string(&datetime).unwrap(); let datetime_ser = ron::ser::to_string(&datetime).unwrap();
let datetime_de: DateTime = ron::de::from_str(datetime_ser.as_str()).unwrap(); let datetime_de: DateTime = ron::de::from_str(datetime_ser.as_str()).unwrap();
assert_eq!(datetime_de, datetime); assert_eq!(datetime_de, datetime);
let datetime = DateTime::new(2f32, 2018, 5, 28, 16, 6, -1f64); let datetime = DateTime::new(2f32, 2018, 5, 28, 16, 6, -1f64).unwrap();
let datetime_ser = ron::ser::to_string(&datetime).unwrap(); let datetime_ser = ron::ser::to_string(&datetime).unwrap();
let datetime_de: DateTime = ron::de::from_str(datetime_ser.as_str()).unwrap(); let datetime_de: DateTime = ron::de::from_str(datetime_ser.as_str()).unwrap();
assert_eq!(datetime_de, datetime); assert_eq!(datetime_de, datetime);
let datetime = DateTime::new_ymd(2018, 5, 28); let datetime = DateTime::new_ymd(2018, 5, 28).unwrap();
let datetime_ser = ron::ser::to_string(&datetime).unwrap(); let datetime_ser = ron::ser::to_string(&datetime).unwrap();
let datetime_de: DateTime = ron::de::from_str(datetime_ser.as_str()).unwrap(); let datetime_de: DateTime = ron::de::from_str(datetime_ser.as_str()).unwrap();
assert_eq!(datetime_de, datetime); assert_eq!(datetime_de, datetime);
let datetime = DateTime::new_ym(2018, 5); let datetime = DateTime::new_ym(2018, 5).unwrap();
let datetime_ser = ron::ser::to_string(&datetime).unwrap(); let datetime_ser = ron::ser::to_string(&datetime).unwrap();
let datetime_de: DateTime = ron::de::from_str(datetime_ser.as_str()).unwrap(); let datetime_de: DateTime = ron::de::from_str(datetime_ser.as_str()).unwrap();
assert_eq!(datetime_de, datetime); assert_eq!(datetime_de, datetime);
let datetime = DateTime::new_y(2018); let datetime = DateTime::new_y(2018).unwrap();
let datetime_ser = ron::ser::to_string(&datetime).unwrap(); let datetime_ser = ron::ser::to_string(&datetime).unwrap();
let datetime_de: DateTime = ron::de::from_str(datetime_ser.as_str()).unwrap(); let datetime_de: DateTime = ron::de::from_str(datetime_ser.as_str()).unwrap();
assert_eq!(datetime_de, datetime); assert_eq!(datetime_de, datetime);

View file

@ -186,7 +186,7 @@ mod tests {
.field("date", &Date::new_dmy(19, DateMonth::August, 2019)) .field("date", &Date::new_dmy(19, DateMonth::August, 2019))
.field( .field(
"date_time", "date_time",
&DateTime::new(2f32, 2019, 8, 19, 13, 34, 42f64), &DateTime::new(2f32, 2019, 8, 19, 13, 34, 42f64).unwrap(),
) )
.field("array", &Array::new(&[&1, &2])) .field("array", &Array::new(&[&1, &2]))
.build(); .build();
@ -244,7 +244,7 @@ mod tests {
("date", &Date::new_dmy(19, DateMonth::August, 2019)), ("date", &Date::new_dmy(19, DateMonth::August, 2019)),
( (
"date_time", "date_time",
&DateTime::new(2f32, 2019, 8, 19, 13, 34, 42f64) &DateTime::new(2f32, 2019, 8, 19, 13, 34, 42f64).unwrap()
), ),
("fraction", &Fraction::new(1, 2)), ("fraction", &Fraction::new(1, 2)),
("array", &Array::new(&[&1, &2])), ("array", &Array::new(&[&1, &2])),
@ -266,7 +266,7 @@ mod tests {
.field("date", &Date::new_dmy(19, DateMonth::August, 2019)) .field("date", &Date::new_dmy(19, DateMonth::August, 2019))
.field( .field(
"date_time", "date_time",
&DateTime::new(2f32, 2019, 8, 19, 13, 34, 42f64), &DateTime::new(2f32, 2019, 8, 19, 13, 34, 42f64).unwrap(),
) )
.field("array", &Array::new(&[&1, &2])) .field("array", &Array::new(&[&1, &2]))
.build(); .build();

View file

@ -347,7 +347,10 @@ mod tests {
&glib::Date::new_dmy(28, glib::DateMonth::May, 2018), &glib::Date::new_dmy(28, glib::DateMonth::May, 2018),
TagMergeMode::Append, TagMergeMode::Append,
); );
tags.add::<DateTime>(&::DateTime::new_ymd(2018, 5, 28), TagMergeMode::Append); tags.add::<DateTime>(
&::DateTime::new_ymd(2018, 5, 28).unwrap(),
TagMergeMode::Append,
);
let sample = { let sample = {
let mut buffer = Buffer::from_slice(vec![1, 2, 3, 4]); let mut buffer = Buffer::from_slice(vec![1, 2, 3, 4]);
@ -497,7 +500,7 @@ mod tests {
); );
assert_eq!( assert_eq!(
tags.get_index::<DateTime>(0).unwrap().get().unwrap(), tags.get_index::<DateTime>(0).unwrap().get().unwrap(),
::DateTime::new_ymd(2018, 5, 28) ::DateTime::new_ymd(2018, 5, 28).unwrap()
); );
let sample = tags.get_index::<Image>(0).unwrap().get().unwrap(); let sample = tags.get_index::<Image>(0).unwrap().get().unwrap();
let buffer = sample.get_buffer().unwrap(); let buffer = sample.get_buffer().unwrap();
@ -538,7 +541,7 @@ mod tests {
); );
assert_eq!( assert_eq!(
tags.get_index::<DateTime>(0).unwrap().get().unwrap(), tags.get_index::<DateTime>(0).unwrap().get().unwrap(),
::DateTime::new_ymd(2018, 5, 28) ::DateTime::new_ymd(2018, 5, 28).unwrap()
); );
let sample = tags.get_index::<Image>(0).unwrap().get().unwrap(); let sample = tags.get_index::<Image>(0).unwrap().get().unwrap();
let buffer = sample.get_buffer().unwrap(); let buffer = sample.get_buffer().unwrap();
@ -566,7 +569,10 @@ mod tests {
&glib::Date::new_dmy(28, glib::DateMonth::May, 2018), &glib::Date::new_dmy(28, glib::DateMonth::May, 2018),
TagMergeMode::Append, TagMergeMode::Append,
); );
tags.add::<DateTime>(&::DateTime::new_ymd(2018, 5, 28), TagMergeMode::Append); tags.add::<DateTime>(
&::DateTime::new_ymd(2018, 5, 28).unwrap(),
TagMergeMode::Append,
);
let sample = { let sample = {
let mut buffer = Buffer::from_slice(vec![1, 2, 3, 4]); let mut buffer = Buffer::from_slice(vec![1, 2, 3, 4]);
@ -612,7 +618,7 @@ mod tests {
); );
assert_eq!( assert_eq!(
tags.get_index::<DateTime>(0).unwrap().get().unwrap(), tags.get_index::<DateTime>(0).unwrap().get().unwrap(),
::DateTime::new_ymd(2018, 5, 28) ::DateTime::new_ymd(2018, 5, 28).unwrap()
); );
let sample = tags.get_index::<Image>(0).unwrap().get().unwrap(); let sample = tags.get_index::<Image>(0).unwrap().get().unwrap();
let buffer = sample.get_buffer().unwrap(); let buffer = sample.get_buffer().unwrap();

View file

@ -433,7 +433,9 @@ mod tests {
let value_str_none = str_none.to_value(); let value_str_none = str_none.to_value();
let send_value_str_none = value_str_none.try_into_send_value::<String>().unwrap(); let send_value_str_none = value_str_none.try_into_send_value::<String>().unwrap();
let value_date_time = DateTime::new(2f32, 2019, 8, 19, 13, 34, 42f64).to_value(); let value_date_time = DateTime::new(2f32, 2019, 8, 19, 13, 34, 42f64)
.unwrap()
.to_value();
let send_value_date_time = value_date_time.try_into_send_value::<DateTime>().unwrap(); let send_value_date_time = value_date_time.try_into_send_value::<DateTime>().unwrap();
let date_time_none: Option<DateTime> = None; let date_time_none: Option<DateTime> = None;
@ -648,7 +650,7 @@ mod tests {
assert!(slice[2].get::<String>().expect("slice[2]").is_none()); assert!(slice[2].get::<String>().expect("slice[2]").is_none());
assert_eq!( assert_eq!(
DateTime::new(2f32, 2019, 8, 19, 13, 34, 42f64), DateTime::new(2f32, 2019, 8, 19, 13, 34, 42f64).unwrap(),
slice[3].get::<DateTime>().expect("slice[3]").unwrap() slice[3].get::<DateTime>().expect("slice[3]").unwrap()
); );
@ -723,7 +725,9 @@ mod tests {
let str_none: Option<&str> = None; let str_none: Option<&str> = None;
let value_str_none = str_none.to_value(); let value_str_none = str_none.to_value();
let send_value_str_none = value_str_none.try_into_send_value::<String>().unwrap(); let send_value_str_none = value_str_none.try_into_send_value::<String>().unwrap();
let value_date_time = DateTime::new(2f32, 2019, 8, 19, 13, 34, 42f64).to_value(); let value_date_time = DateTime::new(2f32, 2019, 8, 19, 13, 34, 42f64)
.unwrap()
.to_value();
let send_value_date_time = value_date_time.try_into_send_value::<DateTime>().unwrap(); let send_value_date_time = value_date_time.try_into_send_value::<DateTime>().unwrap();
let date_time_none: Option<DateTime> = None; let date_time_none: Option<DateTime> = None;
let value_date_time_none = date_time_none.to_value(); let value_date_time_none = date_time_none.to_value();