2017-10-31 19:41:45 +00:00
|
|
|
|
// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
2024-06-26 11:12:33 +00:00
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
|
|
use xso::{error::Error, FromXmlText, IntoXmlText};
|
|
|
|
|
|
2018-12-18 14:32:05 +00:00
|
|
|
|
use chrono::{DateTime as ChronoDateTime, FixedOffset};
|
2019-07-24 22:20:38 +00:00
|
|
|
|
use minidom::{IntoAttributeValue, Node};
|
2017-10-31 19:41:45 +00:00
|
|
|
|
|
|
|
|
|
/// Implements the DateTime profile of XEP-0082, which represents a
|
|
|
|
|
/// non-recurring moment in time, with an accuracy of seconds or fraction of
|
|
|
|
|
/// seconds, and includes a timezone.
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2020-10-26 14:17:19 +00:00
|
|
|
|
pub struct DateTime(pub ChronoDateTime<FixedOffset>);
|
2017-10-31 19:41:45 +00:00
|
|
|
|
|
2019-04-22 09:58:13 +00:00
|
|
|
|
impl DateTime {
|
2019-04-22 11:33:29 +00:00
|
|
|
|
/// Retrieves the associated timezone.
|
2019-04-22 09:58:13 +00:00
|
|
|
|
pub fn timezone(&self) -> FixedOffset {
|
|
|
|
|
self.0.timezone()
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-22 11:33:29 +00:00
|
|
|
|
/// Returns a new `DateTime` with a different timezone.
|
2019-07-25 23:54:26 +00:00
|
|
|
|
pub fn with_timezone(&self, tz: FixedOffset) -> DateTime {
|
|
|
|
|
DateTime(self.0.with_timezone(&tz))
|
2019-04-22 09:58:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-22 11:33:29 +00:00
|
|
|
|
/// Formats this `DateTime` with the specified format string.
|
2019-04-22 09:58:13 +00:00
|
|
|
|
pub fn format(&self, fmt: &str) -> String {
|
|
|
|
|
format!("{}", self.0.format(fmt))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-31 19:41:45 +00:00
|
|
|
|
impl FromStr for DateTime {
|
2024-06-21 14:27:43 +00:00
|
|
|
|
type Err = chrono::ParseError;
|
2017-10-31 19:41:45 +00:00
|
|
|
|
|
2024-06-21 14:27:43 +00:00
|
|
|
|
fn from_str(s: &str) -> Result<DateTime, Self::Err> {
|
2017-10-31 19:41:45 +00:00
|
|
|
|
Ok(DateTime(ChronoDateTime::parse_from_rfc3339(s)?))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-26 11:12:33 +00:00
|
|
|
|
impl FromXmlText for DateTime {
|
|
|
|
|
fn from_xml_text(s: String) -> Result<Self, Error> {
|
|
|
|
|
s.parse().map_err(Error::text_parse_error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl IntoXmlText for DateTime {
|
|
|
|
|
fn into_xml_text(self) -> Result<String, Error> {
|
|
|
|
|
Ok(self.0.to_rfc3339())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-31 19:41:45 +00:00
|
|
|
|
impl IntoAttributeValue for DateTime {
|
|
|
|
|
fn into_attribute_value(self) -> Option<String> {
|
|
|
|
|
Some(self.0.to_rfc3339())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-11 13:22:19 +00:00
|
|
|
|
impl From<DateTime> for Node {
|
|
|
|
|
fn from(date: DateTime) -> Node {
|
|
|
|
|
Node::Text(date.0.to_rfc3339())
|
2017-10-31 19:41:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
use chrono::{Datelike, Timelike};
|
|
|
|
|
|
2019-04-22 09:58:13 +00:00
|
|
|
|
// DateTime’s size doesn’t depend on the architecture.
|
2018-10-26 12:26:16 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
|
|
|
|
assert_size!(DateTime, 16);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-31 19:41:45 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_simple() {
|
|
|
|
|
let date: DateTime = "2002-09-10T23:08:25Z".parse().unwrap();
|
|
|
|
|
assert_eq!(date.0.year(), 2002);
|
|
|
|
|
assert_eq!(date.0.month(), 9);
|
|
|
|
|
assert_eq!(date.0.day(), 10);
|
|
|
|
|
assert_eq!(date.0.hour(), 23);
|
|
|
|
|
assert_eq!(date.0.minute(), 08);
|
|
|
|
|
assert_eq!(date.0.second(), 25);
|
|
|
|
|
assert_eq!(date.0.nanosecond(), 0);
|
2022-11-22 17:59:09 +00:00
|
|
|
|
assert_eq!(date.0.timezone(), FixedOffset::east_opt(0).unwrap());
|
2017-10-31 19:41:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid_date() {
|
|
|
|
|
// There is no thirteenth month.
|
|
|
|
|
let error = DateTime::from_str("2017-13-01T12:23:34Z").unwrap_err();
|
2024-06-21 14:27:43 +00:00
|
|
|
|
assert_eq!(error.to_string(), "input is out of range");
|
2017-10-31 19:41:45 +00:00
|
|
|
|
|
|
|
|
|
// Timezone ≥24:00 aren’t allowed.
|
|
|
|
|
let error = DateTime::from_str("2017-05-27T12:11:02+25:00").unwrap_err();
|
2024-06-21 14:27:43 +00:00
|
|
|
|
assert_eq!(error.to_string(), "input is out of range");
|
2017-10-31 19:41:45 +00:00
|
|
|
|
|
|
|
|
|
// Timezone without the : separator aren’t allowed.
|
|
|
|
|
let error = DateTime::from_str("2017-05-27T12:11:02+0100").unwrap_err();
|
2024-06-21 14:27:43 +00:00
|
|
|
|
assert_eq!(error.to_string(), "input contains invalid characters");
|
2017-10-31 19:41:45 +00:00
|
|
|
|
|
|
|
|
|
// No seconds, error message could be improved.
|
|
|
|
|
let error = DateTime::from_str("2017-05-27T12:11+01:00").unwrap_err();
|
2024-06-21 14:27:43 +00:00
|
|
|
|
assert_eq!(error.to_string(), "input contains invalid characters");
|
2017-10-31 19:41:45 +00:00
|
|
|
|
|
|
|
|
|
// TODO: maybe we’ll want to support this one, as per XEP-0082 §4.
|
|
|
|
|
let error = DateTime::from_str("20170527T12:11:02+01:00").unwrap_err();
|
2024-06-21 14:27:43 +00:00
|
|
|
|
assert_eq!(error.to_string(), "input contains invalid characters");
|
2017-10-31 19:41:45 +00:00
|
|
|
|
|
|
|
|
|
// No timezone.
|
|
|
|
|
let error = DateTime::from_str("2017-05-27T12:11:02").unwrap_err();
|
2024-06-21 14:27:43 +00:00
|
|
|
|
assert_eq!(error.to_string(), "premature end of input");
|
2017-10-31 19:41:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_serialise() {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let date =
|
|
|
|
|
DateTime(ChronoDateTime::parse_from_rfc3339("2017-05-21T20:19:55+01:00").unwrap());
|
2017-10-31 19:41:45 +00:00
|
|
|
|
let attr = date.into_attribute_value();
|
|
|
|
|
assert_eq!(attr, Some(String::from("2017-05-21T20:19:55+01:00")));
|
|
|
|
|
}
|
|
|
|
|
}
|