summaryrefslogtreecommitdiff
path: root/src/datetime.rs
blob: 2e06d249b6c0adfefee38c227e9148cbd4f16cce (plain)
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
use chrono::prelude::*;
use chrono_tz::Tz;


pub type DTime = DateTime<Tz>;

pub fn from_ordinal(ordinal: isize) -> DateTime<Utc> {
    let timestamp = ordinal * 24 * 60 * 60;
    let naive = NaiveDateTime::from_timestamp(timestamp as i64, 0);
    DateTime::from_utc(naive, Utc)
}

pub fn to_ordinal(date: &DateTime<Utc>) -> isize {
    (date.timestamp() / 60 / 60 / 24) as isize
}

#[derive(Debug)]
pub struct Time {
    pub hour: usize,
    pub minute: usize,
    pub second: usize,
    pub millisecond: usize,
}

impl Time {
    pub fn new(hour: usize, minute: usize, second: usize, millisecond: usize) -> Self {
        Self {
            hour,
            minute,
            second,
            millisecond,
        }
    }

    pub fn time(&self) -> usize {
        (self.hour * 60 * 60 + self.minute * 60 + self.second) * 1000 + self.millisecond
    }
}