Skip to main content

kine_core/
calendar.rs

1use crate::{Time, TimeResult};
2
3/// A calendar system, including timezone if need be
4pub trait Calendar {
5    /// The data needed to represent a time in this calendar
6    type Time: CalendarTime;
7
8    /// Find the possible ways of writing time `t` in this calendar system
9    fn write(&self, t: &Time) -> crate::Result<Self::Time>;
10
11    /// Retrieve the current time in this calendar
12    fn try_now(&self) -> crate::Result<Self::Time> {
13        self.write(&Time::try_now()?.any_approximate())
14    }
15
16    /// Retrieve the current time in this calendar
17    ///
18    /// This function is allowed to panic if the current time is not representable
19    /// in this calendar. If this is a problem for you, please use `write`.
20    fn now(&self) -> Self::Time {
21        self.write(&Time::now())
22            .expect("Trying to write out-of-range time")
23    }
24}
25
26/// Time as represented by a calendar
27pub trait CalendarTime {
28    /// Find the possible times this written time could be about
29    fn read(&self) -> crate::Result<TimeResult>;
30}