Skip to main content

chrono_light/
types.rs

1#[cfg(not(feature = "std"))]
2use alloc::vec::Vec;
3#[cfg(feature = "scale")]
4use codec::{Decode, Encode};
5#[cfg(feature = "scale")]
6use scale_info::TypeInfo;
7
8use super::constants::*;
9
10/// DateTime representation from year to ms. Valid values are:
11/// - year:   [1970, 4000]
12/// - month:  [1, 12]
13/// - day:    [1, 31] (depending on month, leap year)
14/// - hour:   [0: 23]
15/// - minute: [0, 59]
16/// - second: [0, 59]
17/// - ms:     [0, 999]
18/// 
19/// Note: other values will be accepted, but will be classified invalid by the calendar, and if used,
20/// appropriate values will be added on top, eg. 32/01 -> 01/02.
21#[derive(Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
22#[cfg_attr(feature = "scale", derive(Encode, Decode, TypeInfo))]
23pub struct DateTime {
24    // date
25    pub year:   u16,
26    pub month:  u8,
27    pub day:    u8,
28
29    // time
30    pub hour:   u8,
31    pub minute: u8,
32    pub second: u8,
33    pub ms:     u16,
34}
35
36impl DateTime {
37    /// Calculates ms for the day
38    pub fn to_day_unixtime(&self) -> u64 {
39        self.day.checked_sub(1).expect("failed to calc day - 1") as u64 * MS_IN_DAY
40            + self.hour as u64 * MS_IN_HOUR
41            + self.minute as u64 * MS_IN_MIN
42            + self.second as u64 * MS_IN_SEC
43            + self.ms as u64
44    }
45}
46
47/// Schedule, represented by a `start` `DateTime`, optional `end` `DateTime`, and multiple pairs of (`Frequency`, `multiplier`).
48/// Next occurrence of trigger time is calculated by taking the earliest occurrence of `Frequency` * `multiplier`, from `start`, but before `end`.
49#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
50#[cfg_attr(feature = "scale", derive(Encode, Decode, TypeInfo))]
51pub struct Schedule {
52    pub start: DateTime,
53    pub items: Vec<(Frequency, u32)>,  // frequency with multiplier
54    pub end: Option<DateTime>,
55}
56
57#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
58#[cfg_attr(feature = "scale", derive(Encode, Decode, TypeInfo))]
59#[repr(u8)]
60pub enum Frequency {
61    Year,
62    Month,
63    Week,
64    Day,
65    Hour,
66    Minute,
67    Second,
68    Ms,
69}
70
71impl Frequency {
72    #[inline]
73    pub fn to_ms(&self) -> u32 {
74        match self {
75            Frequency::Year   => 666_u32,
76            Frequency::Month  => 999_u32,
77            Frequency::Week   => 7 * MS_IN_DAY as u32,
78            Frequency::Day    => MS_IN_DAY as u32,
79            Frequency::Hour   => MS_IN_HOUR as u32,
80            Frequency::Minute => MS_IN_MIN as u32,
81            Frequency::Second => MS_IN_SEC as u32,
82            Frequency::Ms     => 1_u32,
83        }
84    }
85}
86
87#[derive(PartialEq, Eq, Debug)]
88pub enum ValidationError {
89    /// `DateTime` not covered by this library, eg. 01/01/1000 00:00:00:000, 01/01/5000 00:00:00:000
90    OutOfScope,
91    /// Invalid `DateTime`, eg. 32/13/2000 66:66:66:6666, 29/02/2021 10:10:10:000 (non leap year)
92    Invalid
93}