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
use chrono::{FixedOffset, NaiveTime, Timelike};

use crate::{format_nanoseconds, format_timezone, Datatype, ParseXsd, XsdValue};
use core::fmt;

#[derive(Debug, thiserror::Error)]
#[error("invalid time value")]
pub struct InvalidTimeValue;

#[derive(Debug, Clone, Copy)]
pub struct Time {
	pub time: NaiveTime,
	pub offset: Option<FixedOffset>,
}

impl Time {
	pub fn new(time: NaiveTime, offset: Option<FixedOffset>) -> Self {
		Self { time, offset }
	}
}

impl XsdValue for Time {
	fn datatype(&self) -> Datatype {
		Datatype::Time
	}
}

impl ParseXsd for Time {
	type LexicalForm = crate::lexical::Time;
}

impl fmt::Display for Time {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		write!(
			f,
			"{:02}:{:02}:{:02}",
			self.time.hour(),
			self.time.minute(),
			self.time.second()
		)?;

		format_nanoseconds(self.time.nanosecond(), f)?;
		format_timezone(self.offset, f)
	}
}