mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
parsers/date: Add DateTime::from_utc helper
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
1613a1c667
commit
86d3e5fbab
1 changed files with 14 additions and 2 deletions
|
@ -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};
|
||||
|
||||
// DateTime’s size doesn’t 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")));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue