whatwg_datetime/components/
time.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use crate::parse_format;
use crate::tokens::Token;
use crate::utils::{collect_ascii_digits, is_valid_hour, is_valid_min_or_sec};
use chrono::NaiveTime;
use whatwg_infra::collect_codepoints;

/// Parse a specific time containing an hour, minute, and optionally a second,
/// and a fraction of a second
///
/// This follows the rules for [parsing a time string][whatwg-html-parse]
/// per [WHATWG HTML Standard § 2.3.5.4 Times][whatwg-html-time].
///
/// # Examples
/// ```
/// use chrono::NaiveTime;
/// use whatwg_datetime::parse_time;
///
/// // parse a local datetime with hours and minutes
/// assert_eq!(parse_time("14:59"), NaiveTime::from_hms_opt(14, 59, 0));
///
/// // parse a local datetime with hours, minutes, and seconds
/// assert_eq!(parse_time("14:59:39"), NaiveTime::from_hms_opt(14, 59, 39));
///
/// // parse a local datetime with hours, minutes, seconds, and milliseconds
/// assert_eq!(parse_time("14:59:39.929"), NaiveTime::from_hms_milli_opt(14, 59, 39, 929));
/// ```
///
/// [whatwg-html-time]: https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#times
/// [whatwg-html-parse]: https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#parse-a-time-string
#[inline]
pub fn parse_time(s: &str) -> Option<NaiveTime> {
	parse_format(s, parse_time_component)
}

/// Low-level function for parsing an individual time component at a given position
///
/// This follows the rules for [parsing a time component][whatwg-html-parse]
/// per [WHATWG HTML Standard § 2.3.5.4 Times][whatwg-html-time].
///
/// > **Note**:
/// > This function exposes a lower-level API than [`parse_time`]. More than likely,
/// > you will want to use [`parse_time`] instead.
///
/// # Examples
/// ```
/// use chrono::NaiveTime;
/// use whatwg_datetime::parse_time_component;
///
/// let mut position = 0usize;
/// let date = parse_time_component("14:59", &mut position);
///
/// assert_eq!(date, NaiveTime::from_hms_opt(14, 59, 0));
/// ```
///
/// [whatwg-html-time]: https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#times
/// [whatwg-html-parse]: https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#parse-a-time-component
pub fn parse_time_component(s: &str, position: &mut usize) -> Option<NaiveTime> {
	let parsed_hour = collect_ascii_digits(s, position);
	if parsed_hour.len() != 2 {
		return None;
	}

	let hour = parsed_hour.parse::<u32>().ok()?;
	if !is_valid_hour(&hour) {
		return None;
	}

	if *position > s.len() || s.chars().nth(*position) != Some(Token::COLON) {
		return None;
	} else {
		*position += 1;
	}

	let parsed_minute = collect_ascii_digits(s, position);
	if parsed_minute.len() != 2 {
		return None;
	}
	let minute = parsed_minute.parse::<u32>().ok()?;
	if !is_valid_min_or_sec(&minute) {
		return None;
	}

	let mut seconds = 0u32;
	let mut milliseconds = 0u32;
	if *position < s.len() && s.chars().nth(*position) == Some(Token::COLON) {
		*position += 1;

		if *position >= s.len() {
			return None;
		}

		let parsed_second =
			collect_codepoints(s, position, |c| c.is_ascii_digit() || c == Token::DOT);
		let parsed_second_len = parsed_second.len();
		if parsed_second_len == 3
			|| (parsed_second_len > 3
				&& parsed_second.chars().nth(2) != Some(Token::DOT))
			|| has_at_least_n_instances(s, Token::DOT, 2)
		{
			return None;
		}

		let (parsed_seconds, parsed_milliseconds) =
			parse_seconds_milliseconds(&parsed_second);
		seconds = parsed_seconds;
		milliseconds = parsed_milliseconds;
		if !is_valid_min_or_sec(&seconds) {
			return None;
		}
	}

	NaiveTime::from_hms_milli_opt(hour, minute, seconds, milliseconds)
}

fn has_at_least_n_instances(s: &str, c: char, n: usize) -> bool {
	let mut count = 0usize;
	for ch in s.chars() {
		if ch == c {
			count += 1usize;
			if count >= n {
				return true;
			}
		}
	}
	false
}

fn parse_seconds_milliseconds(s: &str) -> (u32, u32) {
	let parts: Vec<&str> = s.split(Token::DOT).collect();
	let seconds = parts.first().unwrap_or(&"0").parse().unwrap_or(0);
	let milliseconds = parts.get(1).unwrap_or(&"0").parse().unwrap_or(0);

	(seconds, milliseconds)
}

#[cfg(test)]
mod tests {
	use super::{parse_time, parse_time_component, NaiveTime};

	#[test]
	fn test_parse_time_succeeds_hm() {
		assert_eq!(
			parse_time("12:31"),
			NaiveTime::from_hms_milli_opt(12, 31, 0, 0)
		);
	}

	#[test]
	fn test_parse_time_succeeds_hms() {
		assert_eq!(
			parse_time("12:31:59"),
			NaiveTime::from_hms_milli_opt(12, 31, 59, 0)
		);
	}

	#[test]
	fn test_parse_time_succeeds_hms_fractional_seconds() {
		assert_eq!(
			parse_time("14:54:39.929"),
			NaiveTime::from_hms_milli_opt(14, 54, 39, 929)
		);
	}

	#[test]
	fn test_parse_time_fails_multiple_decimals() {
		assert_eq!(parse_time("12:31:59...29"), None);
	}

	#[test]
	fn test_parse_time_fails_hour_length() {
		assert_eq!(parse_time("123:31:59"), None);
	}

	#[test]
	fn test_parse_time_fails_hour_value_upper_bound() {
		assert_eq!(parse_time("24:31:59"), None);
	}

	#[test]
	fn test_parse_time_fails_delimiter() {
		assert_eq!(parse_time("12-31-59"), None);
	}

	#[test]
	fn test_parse_time_fails_minute_length() {
		assert_eq!(parse_time("12:311:59"), None);
	}

	#[test]
	fn test_parse_time_fails_minute_value_upper_bound() {
		assert_eq!(parse_time("12:79:59"), None);
	}

	#[test]
	fn test_parse_time_fails_seconds_length() {
		assert_eq!(parse_time("12:31:591"), None);
	}

	#[test]
	fn test_parse_time_fails_seconds_value_upper_bound() {
		assert_eq!(parse_time("12:31:79"), None);
	}

	#[test]
	fn test_parse_time_component() {
		let mut position = 0usize;
		let parsed = parse_time_component("12:31:59", &mut position);

		assert_eq!(parsed, NaiveTime::from_hms_milli_opt(12, 31, 59, 0));
	}
}