1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use core::fmt::Display;
/// A minimum set of functionality a typical calendar should provide.
pub trait CalendarDate: Sized + Display {
/// Earliest representable date.
const MIN: Self;
/// Latest representable date.
const MAX: Self;
#[must_use]
/// Convert the date into the corresponding Julian Day Number.
fn as_julian(&self) -> i32;
#[must_use]
/// Get the date from the corresponding Julian
fn from_julian(value: i32) -> Option<Self>;
#[must_use]
/// Gets the next date.
fn tomorrow(self) -> Option<Self> {
Self::from_julian(self.as_julian() + 1)
}
#[must_use]
/// Gets the previous date.
fn yesterday(self) -> Option<Self> {
Self::from_julian(self.as_julian() - 1)
}
/// Converts the calendar date to a different calendar system.
///
/// # Examples
///
/// ```
/// use sac13::prelude::*;
/// use sac13::day_counts::*;
///
/// let date_sac13 : Date = JulianDay::new(2460000).unwrap().convert();
/// let date_greg : GregorianDate = date_sac13.convert();
/// ```
///
/// It's basically like the [`From`] trait, but because of the orphan rule I failed
/// to implement it generically for all types that implement [`CalendarDate`].
#[must_use]
fn convert<T: CalendarDate>(self) -> T {
T::from_julian(self.as_julian()).expect("SAC13 range calendars to be convertible.")
}
}