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/.
|
|
|
|
|
|
2019-01-13 11:39:51 +00:00
|
|
|
|
use crate::util::error::Error;
|
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};
|
2018-12-18 14:32:05 +00:00
|
|
|
|
use std::str::FromStr;
|
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)]
|
|
|
|
|
pub struct DateTime(ChronoDateTime<FixedOffset>);
|
|
|
|
|
|
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 {
|
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<DateTime, Error> {
|
|
|
|
|
Ok(DateTime(ChronoDateTime::parse_from_rfc3339(s)?))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl IntoAttributeValue for DateTime {
|
|
|
|
|
fn into_attribute_value(self) -> Option<String> {
|
|
|
|
|
Some(self.0.to_rfc3339())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-24 22:20:38 +00:00
|
|
|
|
impl Into<Node> for DateTime {
|
|
|
|
|
fn into(self) -> Node {
|
|
|
|
|
Node::Text(self.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);
|
|
|
|
|
assert_eq!(date.0.timezone(), FixedOffset::east(0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid_date() {
|
|
|
|
|
// There is no thirteenth month.
|
|
|
|
|
let error = DateTime::from_str("2017-13-01T12:23:34Z").unwrap_err();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ChronoParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2020-01-21 22:46:00 +00:00
|
|
|
|
assert_eq!(message.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();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ChronoParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2020-01-21 22:46:00 +00:00
|
|
|
|
assert_eq!(message.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();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ChronoParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2020-01-21 22:46:00 +00:00
|
|
|
|
assert_eq!(message.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();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ChronoParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2020-01-21 22:46:00 +00:00
|
|
|
|
assert_eq!(message.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();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ChronoParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2020-01-21 22:46:00 +00:00
|
|
|
|
assert_eq!(message.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();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ChronoParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2020-01-21 22:46:00 +00:00
|
|
|
|
assert_eq!(message.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")));
|
|
|
|
|
}
|
|
|
|
|
}
|