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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
use static_regular_grammar::RegularGrammar;

use crate::{utils::byte_index_of, InvalidDateTimeValue};

use super::{Lexical, LexicalFormOf};

/// Date and time.
///
/// ```abnf
/// date-time = year "-" month "-" day %s"T" time [timezone]
///
/// time = hour ":" minute ":" second ["." fraction]
///      / "24:00:00" ["." 1*"0"]
///
/// year = [ "-" ] year-number
///
/// year-number = *3DIGIT NZDIGIT
///             / *2DIGIT NZDIGIT DIGIT
///             / *1DIGIT NZDIGIT 2DIGIT
///             / NZDIGIT 3*DIGIT
///
/// month = "0" NZDIGIT
///       / "1" ( "0" / "1" / "2" )
///
/// day = "0" NZDIGIT
///     / ("1" / "2") DIGIT
///     / "3" ("0" / "1")
///
/// hour = ("0" / "1") DIGIT
///      / "2" ("0" / "1" / "2" / "3")
///
/// minute = ("0" / "1" / "2" / "3" / "4" / "5") DIGIT
///
/// second = ("0" / "1" / "2" / "3" / "4" / "5") DIGIT
///
/// fraction = 1*DIGIT
///
/// timezone = ("+" / "-") ((("0" DIGIT / "1" ("0" / "1" / "2" / "3")) ":" minute) / "14:00")
///          / %s"Z"
///
/// NZDIGIT = "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9"
/// ```
///
#[derive(RegularGrammar, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[grammar(sized(DateTimeBuf, derive(PartialEq, Eq, PartialOrd, Ord, Hash)))]
pub struct DateTime(str);

impl DateTime {
	pub fn parts(&self) -> Parts {
		let year_end = byte_index_of(self.0.as_bytes(), 4, b'-').unwrap();
		let month_end = year_end + 3;
		let day_end = month_end + 3;
		let hour_end = day_end + 3;
		let minute_end = hour_end + 3;
		let second_end = byte_index_of(self.0.as_bytes(), minute_end + 3, [b'+', b'-', b'Z'])
			.unwrap_or(self.0.len());

		Parts {
			year: &self.0[..year_end],
			month: &self.0[(year_end + 1)..month_end],
			day: &self.0[(month_end + 1)..day_end],
			hours: &self.0[(day_end + 1)..hour_end],
			minutes: &self.0[(hour_end + 1)..minute_end],
			seconds: &self.0[(minute_end + 1)..second_end],
			timezone: if second_end == self.0.len() {
				None
			} else {
				Some(&self.0[second_end..])
			},
		}
	}
}

impl Lexical for DateTime {
	type Error = InvalidDateTime<String>;

	fn parse(value: &str) -> Result<&Self, Self::Error> {
		Self::new(value).map_err(|_| InvalidDateTime(value.to_owned()))
	}
}

impl LexicalFormOf<crate::DateTime> for DateTime {
	type ValueError = InvalidDateTimeValue;

	fn try_as_value(&self) -> Result<crate::DateTime, Self::ValueError> {
		self.parts().to_datetime()
	}
}

#[derive(Debug, PartialEq, Eq)]
pub struct Parts<'a> {
	pub year: &'a str,
	pub month: &'a str,
	pub day: &'a str,
	pub hours: &'a str,
	pub minutes: &'a str,
	pub seconds: &'a str,
	pub timezone: Option<&'a str>,
}

impl<'a> Parts<'a> {
	pub fn new(
		year: &'a str,
		month: &'a str,
		day: &'a str,
		hours: &'a str,
		minutes: &'a str,
		seconds: &'a str,
		timezone: Option<&'a str>,
	) -> Self {
		Self {
			year,
			month,
			day,
			hours,
			minutes,
			seconds,
			timezone,
		}
	}

	fn to_datetime(&self) -> Result<crate::DateTime, crate::InvalidDateTimeValue> {
		let date = chrono::NaiveDate::from_ymd_opt(
			self.year.parse().unwrap(),
			self.month.parse().unwrap(),
			self.day.parse().unwrap(),
		)
		.ok_or(crate::InvalidDateTimeValue)?;

		let (seconds, nanoseconds) = parse_seconds_decimal(self.seconds);

		let time = chrono::NaiveTime::from_hms_nano_opt(
			self.hours.parse().unwrap(),
			self.minutes.parse().unwrap(),
			seconds,
			nanoseconds,
		)
		.ok_or(crate::InvalidDateTimeValue)?;

		let datetime = chrono::NaiveDateTime::new(date, time);

		Ok(crate::DateTime::new(
			datetime,
			self.timezone.map(parse_timezone),
		))
	}
}

/// Parses a decimal number representing seconds and returns the represented
/// number of seconds and nanoseconds.
pub(crate) fn parse_seconds_decimal(decimal: &str) -> (u32, u32) {
	match decimal.split_once('.') {
		Some((integer, fract)) => {
			let seconds = integer.parse().unwrap();
			let fract = if fract.len() > 9 { &fract[..9] } else { fract };
			let nano_seconds = fract.parse::<u32>().unwrap() * 10u32.pow(9 - fract.len() as u32);

			(seconds, nano_seconds)
		}
		None => (decimal.parse().unwrap(), 0),
	}
}

pub(crate) fn parse_timezone(tz: &str) -> chrono::FixedOffset {
	const HOUR: i32 = 3600;
	const MINUTE: i32 = 60;

	match tz {
		"Z" => chrono::FixedOffset::east_opt(0).unwrap(),
		"14:00" => chrono::FixedOffset::east_opt(14 * HOUR).unwrap(),
		n => {
			let (h, m) = n.split_once(':').unwrap();
			chrono::FixedOffset::east_opt(
				h.parse::<i32>().unwrap() * HOUR + m.parse::<i32>().unwrap() * MINUTE,
			)
			.unwrap()
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn parsing() {
		let vectors = [
			(
				"2002-05-31T13:07:12+01:00",
				Parts::new("2002", "05", "31", "13", "07", "12", Some("+01:00")),
			),
			(
				"2002-05-31T13:07:12",
				Parts::new("2002", "05", "31", "13", "07", "12", None),
			),
			(
				"-2002-05-31T13:07:12+01:00",
				Parts::new("-2002", "05", "31", "13", "07", "12", Some("+01:00")),
			),
			(
				"2002-10-10T12:00:00-05:00",
				Parts::new("2002", "10", "10", "12", "00", "00", Some("-05:00")),
			),
			(
				"202002-10-10T12:00:00.00001-05:00",
				Parts::new("202002", "10", "10", "12", "00", "00.00001", Some("-05:00")),
			),
			(
				"-202002-10-10T12:00:00.00001-05:00",
				Parts::new(
					"-202002",
					"10",
					"10",
					"12",
					"00",
					"00.00001",
					Some("-05:00"),
				),
			),
		];

		for (input, parts) in vectors {
			let lexical_repr = DateTime::new(input).unwrap();
			assert_eq!(lexical_repr.parts(), parts);

			let value = lexical_repr.try_as_value().unwrap();
			assert_eq!(value.to_string().as_str(), input)
		}
	}
}