1// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7use std::borrow::Cow;
8use std::str::FromStr;
9
10use xso::{error::Error, AsXmlText, FromXmlText};
11
12use chrono::{DateTime as ChronoDateTime, FixedOffset};
13use minidom::{IntoAttributeValue, Node};
14
15/// Implements the DateTime profile of XEP-0082, which represents a
16/// non-recurring moment in time, with an accuracy of seconds or fraction of
17/// seconds, and includes a timezone.
18#[derive(Debug, Clone, PartialEq)]
19pub struct DateTime(pub ChronoDateTime<FixedOffset>);
20
21impl DateTime {
22 /// Retrieves the associated timezone.
23 pub fn timezone(&self) -> FixedOffset {
24 self.0.timezone()
25 }
26
27 /// Returns a new `DateTime` with a different timezone.
28 pub fn with_timezone(&self, tz: FixedOffset) -> DateTime {
29 DateTime(self.0.with_timezone(&tz))
30 }
31
32 /// Formats this `DateTime` with the specified format string.
33 pub fn format(&self, fmt: &str) -> String {
34 format!("{}", self.0.format(fmt))
35 }
36}
37
38impl FromStr for DateTime {
39 type Err = chrono::ParseError;
40
41 fn from_str(s: &str) -> Result<DateTime, Self::Err> {
42 Ok(DateTime(ChronoDateTime::parse_from_rfc3339(s)?))
43 }
44}
45
46impl FromXmlText for DateTime {
47 fn from_xml_text(s: String) -> Result<Self, Error> {
48 s.parse().map_err(Error::text_parse_error)
49 }
50}
51
52impl AsXmlText for DateTime {
53 fn as_xml_text(&self) -> Result<Cow<'_, str>, Error> {
54 Ok(Cow::Owned(self.0.to_rfc3339()))
55 }
56}
57
58impl IntoAttributeValue for DateTime {
59 fn into_attribute_value(self) -> Option<String> {
60 Some(self.0.to_rfc3339())
61 }
62}
63
64impl From<DateTime> for Node {
65 fn from(date: DateTime) -> Node {
66 Node::Text(date.0.to_rfc3339())
67 }
68}
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73 use chrono::{Datelike, Timelike};
74
75 // DateTime’s size doesn’t depend on the architecture.
76 #[test]
77 fn test_size() {
78 assert_size!(DateTime, 16);
79 }
80
81 #[test]
82 fn test_simple() {
83 let date: DateTime = "2002-09-10T23:08:25Z".parse().unwrap();
84 assert_eq!(date.0.year(), 2002);
85 assert_eq!(date.0.month(), 9);
86 assert_eq!(date.0.day(), 10);
87 assert_eq!(date.0.hour(), 23);
88 assert_eq!(date.0.minute(), 08);
89 assert_eq!(date.0.second(), 25);
90 assert_eq!(date.0.nanosecond(), 0);
91 assert_eq!(date.0.timezone(), FixedOffset::east_opt(0).unwrap());
92 }
93
94 #[test]
95 fn test_invalid_date() {
96 // There is no thirteenth month.
97 let error = DateTime::from_str("2017-13-01T12:23:34Z").unwrap_err();
98 assert_eq!(error.to_string(), "input is out of range");
99
100 // Timezone ≥24:00 aren’t allowed.
101 let error = DateTime::from_str("2017-05-27T12:11:02+25:00").unwrap_err();
102 assert_eq!(error.to_string(), "input is out of range");
103
104 // Timezone without the : separator aren’t allowed.
105 let error = DateTime::from_str("2017-05-27T12:11:02+0100").unwrap_err();
106 assert_eq!(error.to_string(), "input contains invalid characters");
107
108 // No seconds, error message could be improved.
109 let error = DateTime::from_str("2017-05-27T12:11+01:00").unwrap_err();
110 assert_eq!(error.to_string(), "input contains invalid characters");
111
112 // TODO: maybe we’ll want to support this one, as per XEP-0082 §4.
113 let error = DateTime::from_str("20170527T12:11:02+01:00").unwrap_err();
114 assert_eq!(error.to_string(), "input contains invalid characters");
115
116 // No timezone.
117 let error = DateTime::from_str("2017-05-27T12:11:02").unwrap_err();
118 assert_eq!(error.to_string(), "premature end of input");
119 }
120
121 #[test]
122 fn test_serialise() {
123 let date =
124 DateTime(ChronoDateTime::parse_from_rfc3339("2017-05-21T20:19:55+01:00").unwrap());
125 let attr = date.into_attribute_value();
126 assert_eq!(attr, Some(String::from("2017-05-21T20:19:55+01:00")));
127 }
128}