Expand description
Utilies to convert back and forth between chrono’s data structures and the hl7-parser ones
All implementations here are implemented as TryFrom and From traits
between the TimeStamp struct and various chrono types. This allows for
easy conversion between the two types. The TryFrom implementations will
return an error if the conversion is not possible, such as if the date or
time components are invalid. The From implementations will always succeed
and will set missing components to zero or the epoch if necessary.
View the TimeStamp struct’s documentation for more information on exactly
which traits are implemented.
§Examples
use hl7_parser::datetime::{TimeStamp, TimeStampOffset};
use chrono::{DateTime, FixedOffset, NaiveDate, NaiveDateTime, Utc, Datelike, Timelike};
let ts = TimeStamp {
year: 2023,
month: Some(3),
day: Some(12),
hour: Some(19),
minute: Some(59),
second: Some(5),
microsecond: Some(1234),
offset: Some(TimeStampOffset {
hours: -7,
minutes: 0,
})
};
let datetime: DateTime<FixedOffset> = ts.try_into().unwrap();
assert_eq!(datetime.year(), 2023);
assert_eq!(datetime.month(), 3);
assert_eq!(datetime.day(), 12);
assert_eq!(datetime.hour(), 19);
assert_eq!(datetime.minute(), 59);
assert_eq!(datetime.second(), 5);
assert_eq!(datetime.nanosecond(), 1234 * 1000);
assert_eq!(datetime.offset().local_minus_utc() / 3600, -7);
assert_eq!(datetime.offset().local_minus_utc() % 3600, 0);use hl7_parser::datetime::{TimeStamp, TimeStampOffset};
use chrono::{DateTime, Utc, NaiveDate, TimeZone};
let datetime = Utc.from_utc_datetime(
&NaiveDate::from_ymd_opt(2023, 3, 12).unwrap()
.and_hms_opt(19, 59, 5).unwrap(),
);
let ts: TimeStamp = datetime.into();
assert_eq!(ts.year, 2023);
assert_eq!(ts.month, Some(3));
assert_eq!(ts.day, Some(12));
assert_eq!(ts.hour, Some(19));
assert_eq!(ts.minute, Some(59));
assert_eq!(ts.second, Some(5));
assert_eq!(ts.microsecond, Some(0));
assert_eq!(ts.offset, Some(TimeStampOffset {
hours: 0,
minutes: 0,
}));