hel_time/
iso8601.rs

1use crate::write_hundreds;
2use crate::DateTime;
3
4#[derive(Debug, Clone, Copy)]
5pub struct ISO8601UTC(DateTime);
6impl ISO8601UTC {
7	#[inline]
8	pub fn now() -> Self {
9		Self(DateTime::utc())
10	}
11
12	/// Will format self into `yyyy-MM-ddTHH:mm:ss.fffZ` string
13	/// Is not accepting any format options
14	#[inline]
15	pub fn fmt(&self, f: &mut impl std::fmt::Write) -> std::fmt::Result {
16		use crate::utils::Helper;
17
18		write_hundreds!(f, self.0.year / 100);
19		write_hundreds!(f, self.0.year % 100);
20		f.write_u8(b'-')?;
21
22		write_hundreds!(f, self.0.month);
23		f.write_u8(b'-')?;
24
25		write_hundreds!(f, self.0.day);
26		f.write_u8(b'T')?;
27
28		write_hundreds!(f, self.0.hour);
29		f.write_u8(b':')?;
30
31		write_hundreds!(f, self.0.minute);
32		f.write_u8(b':')?;
33
34		write_hundreds!(f, self.0.second);
35		f.write_u8(b'.')?;
36
37		f.write_u8(b'0' + (self.0.ms / 100) as u8)?;
38		write_hundreds!(f, self.0.ms % 100);
39		f.write_u8(b'Z')
40	}
41
42	#[inline]
43	#[allow(clippy::inherent_to_string_shadow_display)]
44	pub fn to_string(&self) -> String {
45		let mut res = String::with_capacity(25);
46		unsafe { self.fmt(&mut res).unwrap_unchecked() };
47		res
48	}
49}
50
51impl std::fmt::Display for ISO8601UTC {
52	#[inline(always)]
53	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54		ISO8601UTC::fmt(self, f)
55	}
56}