embedded_date_time/
date_time.rs1use core::fmt;
2
3use crate::Date;
4use crate::Time;
5use crate::Weekday;
6
7#[expect(
9 missing_copy_implementations,
10 reason = "This type isn't `Copy` on purpose as embedded systems might not handle 64-bit operations efficiently."
11)]
12#[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Clone)]
13pub struct DateTime {
14 pub date: Date,
16
17 pub time: Time,
19}
20
21impl DateTime {
22 #[must_use]
24 pub fn new(date: Date, time: Time) -> Self {
25 Self { date, time }
26 }
27
28 #[must_use]
30 pub fn year(&self) -> u16 {
31 self.date.year
32 }
33
34 #[must_use]
36 pub fn month(&self) -> u8 {
37 self.date.month
38 }
39
40 #[must_use]
42 pub fn day(&self) -> u8 {
43 self.date.day
44 }
45
46 #[must_use]
48 pub fn hour(&self) -> u8 {
49 self.time.hour
50 }
51
52 #[must_use]
54 pub fn minute(&self) -> u8 {
55 self.time.minute
56 }
57
58 #[must_use]
62 pub fn second(&self) -> u8 {
63 self.time.second
64 }
65
66 #[must_use]
72 pub fn weekday(&self) -> Weekday {
73 self.date.weekday()
74 }
75}
76
77impl fmt::Debug for DateTime {
78 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79 fmt::Display::fmt(&self, f)
80 }
81}
82
83impl fmt::Display for DateTime {
84 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85 write!(f, "{} {}", self.date, self.time)
86 }
87}
88
89#[cfg(feature = "defmt")]
90impl defmt::Format for DateTime {
91 fn format(&self, fmt: defmt::Formatter<'_>) {
92 defmt::write!(fmt, "{} {}", self.date, self.time);
93 }
94}
95
96#[cfg(feature = "ufmt")]
97impl ufmt::uDebug for DateTime {
98 fn fmt<W>(&self, fmt: &mut ufmt::Formatter<'_, W>) -> Result<(), W::Error>
99 where
100 W: ufmt::uWrite + ?Sized,
101 {
102 ufmt::uDisplay::fmt(&self, fmt)
103 }
104}
105
106#[cfg(feature = "ufmt")]
107impl ufmt::uDisplay for DateTime {
108 fn fmt<W>(&self, fmt: &mut ufmt::Formatter<'_, W>) -> Result<(), W::Error>
109 where
110 W: ufmt::uWrite + ?Sized,
111 {
112 ufmt::uwrite!(fmt, "{} {}", self.date, self.time)
113 }
114}