xsd_types/value/
time.rs

1use chrono::{FixedOffset, NaiveTime, Timelike};
2
3use crate::{format_nanoseconds, format_timezone, Datatype, ParseXsd, XsdValue};
4use core::fmt;
5
6#[derive(Debug, thiserror::Error)]
7#[error("invalid time value")]
8pub struct InvalidTimeValue;
9
10#[derive(Debug, Clone, Copy)]
11pub struct Time {
12	pub time: NaiveTime,
13	pub offset: Option<FixedOffset>,
14}
15
16impl Time {
17	pub fn new(time: NaiveTime, offset: Option<FixedOffset>) -> Self {
18		Self { time, offset }
19	}
20}
21
22impl XsdValue for Time {
23	fn datatype(&self) -> Datatype {
24		Datatype::Time
25	}
26}
27
28impl ParseXsd for Time {
29	type LexicalForm = crate::lexical::Time;
30}
31
32impl fmt::Display for Time {
33	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34		write!(
35			f,
36			"{:02}:{:02}:{:02}",
37			self.time.hour(),
38			self.time.minute(),
39			self.time.second()
40		)?;
41
42		format_nanoseconds(self.time.nanosecond(), f)?;
43		format_timezone(self.offset, f)
44	}
45}