parsers/date: Add DateTime::from_utc helper

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2022-09-10 17:48:17 +02:00
parent 1613a1c667
commit 86d3e5fbab
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2

View file

@ -5,7 +5,7 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use crate::util::error::Error;
use chrono::{DateTime as ChronoDateTime, FixedOffset};
use chrono::{DateTime as ChronoDateTime, FixedOffset, Utc};
use minidom::{IntoAttributeValue, Node};
use std::str::FromStr;
@ -16,6 +16,11 @@ use std::str::FromStr;
pub struct DateTime(pub ChronoDateTime<FixedOffset>);
impl DateTime {
/// Create a [`DateTime`] from a [`ChronoDateTime<Utc>`]
pub fn from_utc(dt: ChronoDateTime<Utc>) -> DateTime {
DateTime(dt.with_timezone(&FixedOffset::east(0)))
}
/// Retrieves the associated timezone.
pub fn timezone(&self) -> FixedOffset {
self.0.timezone()
@ -55,7 +60,7 @@ impl From<DateTime> for Node {
#[cfg(test)]
mod tests {
use super::*;
use chrono::{Datelike, Timelike};
use chrono::{Datelike, Timelike, TimeZone};
// DateTimes size doesnt depend on the architecture.
#[test]
@ -134,4 +139,11 @@ mod tests {
let attr = date.into_attribute_value();
assert_eq!(attr, Some(String::from("2017-05-21T20:19:55+01:00")));
}
#[test]
fn test_from_utc() {
let date: DateTime = DateTime::from_utc(Utc.ymd(2022, 9, 10).and_hms(0, 0, 0));
let attr = date.into_attribute_value();
assert_eq!(attr, Some(String::from("2022-09-10T00:00:00+00:00")));
}
}