whatwg_datetime/components/
global_datetime.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use crate::tokens::Token;
use crate::{parse_date_component, parse_time_component, parse_timezone_offset_component};
use chrono::{DateTime, Duration, NaiveDateTime, TimeZone, Utc};

/// Parse a [proleptic-Gregorian date][proleptic-greg] consisting
/// of a date, time, and an optional time-zone offset
///
/// This follows the rules for [parsing a global datetime string][whatwg-html-parse]
/// per [WHATWG HTML Standard ยง 2.3.5.7 Global dates and times][whatwg-html-global-datetime].
///
/// # Examples
/// A global date-time string with a time (hours and minutes):
/// ```
/// use chrono::{NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc};
/// use whatwg_datetime::parse_global_datetime;
///
/// assert_eq!(
///     parse_global_datetime("2011-11-18T14:54Z"),
///     Some(Utc.from_utc_datetime(
///         &NaiveDateTime::new(
///             NaiveDate::from_ymd_opt(2011, 11, 18).unwrap(),
///             NaiveTime::from_hms_opt(14, 54, 0).unwrap(),
///         )
///     ))
/// );
/// ```
///
/// [proleptic-greg]: https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#proleptic-gregorian-date
/// [whatwg-html-global-datetime]: https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#global-dates-and-times
/// [whatwg-html-parse]: https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#parse-a-global-date-and-time-string
pub fn parse_global_datetime(s: &str) -> Option<DateTime<Utc>> {
	let mut position = 0usize;
	let date = parse_date_component(s, &mut position)?;

	let last_char = s.chars().nth(position);
	if position > s.len() || !matches!(last_char, Some(Token::T) | Some(Token::SPACE)) {
		return None;
	} else {
		position += 1;
	}

	let time = parse_time_component(s, &mut position)?;
	if position > s.len() {
		return None;
	}

	let timezone_offset = parse_timezone_offset_component(s, &mut position)?;
	if position < s.len() {
		return None;
	}

	let timezone_offset_as_duration =
		Duration::minutes(timezone_offset.minute as i64 + timezone_offset.hour as i64 * 60);
	let naive_datetime = NaiveDateTime::new(
		date,
		time.overflowing_sub_signed(timezone_offset_as_duration).0,
	);

	Some(Utc.from_utc_datetime(&naive_datetime))
}

#[cfg(test)]
mod tests {
	use super::parse_global_datetime;
	use chrono::{NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc};

	#[test]
	fn test_parse_global_datetime_t_hm() {
		assert_eq!(
			parse_global_datetime("2004-12-31T12:31"),
			Some(Utc.from_utc_datetime(&NaiveDateTime::new(
				NaiveDate::from_ymd_opt(2004, 12, 31).unwrap(),
				NaiveTime::from_hms_opt(12, 31, 0).unwrap(),
			)))
		);
	}

	#[test]
	fn test_parse_global_datetime_t_hms() {
		assert_eq!(
			parse_global_datetime("2004-12-31T12:31:59"),
			Some(Utc.from_utc_datetime(&NaiveDateTime::new(
				NaiveDate::from_ymd_opt(2004, 12, 31).unwrap(),
				NaiveTime::from_hms_opt(12, 31, 59).unwrap(),
			)))
		);
	}

	#[test]
	fn test_parse_global_datetime_t_hms_milliseconds() {
		assert_eq!(
			parse_global_datetime("2027-11-29T12:31:59.123"),
			Some(Utc.from_utc_datetime(&NaiveDateTime::new(
				NaiveDate::from_ymd_opt(2027, 11, 29).unwrap(),
				NaiveTime::from_hms_milli_opt(12, 31, 59, 123).unwrap(),
			)))
		);
	}

	#[test]
	fn test_parse_global_datetime_t_hms_z() {
		assert_eq!(
			parse_global_datetime("2004-12-31T12:31:59Z"),
			Some(Utc.from_utc_datetime(&NaiveDateTime::new(
				NaiveDate::from_ymd_opt(2004, 12, 31).unwrap(),
				NaiveTime::from_hms_opt(12, 31, 59).unwrap(),
			)))
		);
	}

	#[test]
	fn test_parse_global_datetime_space_hm() {
		assert_eq!(
			parse_global_datetime("2004-12-31 12:31"),
			Some(Utc.from_utc_datetime(&NaiveDateTime::new(
				NaiveDate::from_ymd_opt(2004, 12, 31).unwrap(),
				NaiveTime::from_hms_opt(12, 31, 0).unwrap(),
			)))
		);
	}

	#[test]
	fn test_parse_global_datetime_space_hms() {
		assert_eq!(
			parse_global_datetime("2004-12-31 12:31:59"),
			Some(Utc.from_utc_datetime(&NaiveDateTime::new(
				NaiveDate::from_ymd_opt(2004, 12, 31).unwrap(),
				NaiveTime::from_hms_opt(12, 31, 59).unwrap(),
			)))
		);
	}

	#[test]
	fn test_parse_global_datetime_space_hms_milliseconds() {
		assert_eq!(
			parse_global_datetime("2004-12-31 12:31:59.123"),
			Some(Utc.from_utc_datetime(&NaiveDateTime::new(
				NaiveDate::from_ymd_opt(2004, 12, 31).unwrap(),
				NaiveTime::from_hms_milli_opt(12, 31, 59, 123).unwrap(),
			)))
		);
	}

	#[test]
	fn test_parse_global_datetime_fails_invalid_date() {
		assert_eq!(parse_global_datetime("2004/13/31T12:31"), None);
	}

	#[test]
	fn test_parse_global_datetime_fails_invalid_delimiter() {
		assert_eq!(parse_global_datetime("1986-08-14/12-31"), None);
	}

	#[test]
	fn test_parse_global_datetime_fails_invalid_time() {
		assert_eq!(parse_global_datetime("2006-06-05T24:31"), None);
	}

	#[test]
	fn test_parse_global_datetime_fails_invalid_time_long_pos() {
		assert_eq!(parse_global_datetime("2006-06-05T24:31:5999"), None);
	}

	#[test]
	fn test_parse_global_datetime_fails_invalid_timezone_offset_1() {
		assert_eq!(parse_global_datetime("2019-12-31T11:17+24:00"), None);
	}

	#[test]
	fn test_parse_global_datetime_fails_invalid_timezone_offset_2() {
		assert_eq!(parse_global_datetime("1456-02-24T11:17C"), None);
	}
}