Skip to main content

embedded_date_time/
date_time.rs

1use core::fmt;
2
3use crate::Date;
4use crate::Time;
5use crate::Weekday;
6
7/// Combines a simple date and time.
8#[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    /// The date part of this date and time.
15    pub date: Date,
16
17    /// The time part of this date and time.
18    pub time: Time,
19}
20
21impl DateTime {
22    /// Create a new `DateTime` from a Date and Time.
23    #[must_use]
24    pub fn new(date: Date, time: Time) -> Self {
25        Self { date, time }
26    }
27
28    /// Return the year of the date.
29    #[must_use]
30    pub fn year(&self) -> u16 {
31        self.date.year
32    }
33
34    /// Return the month of the date [1-12].
35    #[must_use]
36    pub fn month(&self) -> u8 {
37        self.date.month
38    }
39
40    /// Return the day of the month for this date [1-31].
41    #[must_use]
42    pub fn day(&self) -> u8 {
43        self.date.day
44    }
45
46    /// Read the hour of the time [0-23].
47    #[must_use]
48    pub fn hour(&self) -> u8 {
49        self.time.hour
50    }
51
52    /// Read the minute of the time [0-59].
53    #[must_use]
54    pub fn minute(&self) -> u8 {
55        self.time.minute
56    }
57
58    /// Read the second of the time [0-60].
59    ///
60    /// A value of 60 might occur on a leap second.
61    #[must_use]
62    pub fn second(&self) -> u8 {
63        self.time.second
64    }
65
66    /// Compute the weekday of the date.
67    ///
68    /// This is guaranteed to work for all valid dates within the years 2000 to 2171.
69    ///
70    /// Invalid dates will return arbitrary results or may panic.
71    #[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}