aboutsummaryrefslogtreecommitdiff
path: root/.config/awesome/quarrel/native/src/time.rs
blob: 9850822159659849be5dba81fd84b16e5684622a (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use chrono::prelude::*;
use mlua::{
    prelude::*,
    LuaSerdeExt,
};

// Taken from https://github.com/chronotope/chrono/issues/69#issuecomment-1510506428
trait NaiveDateExt {
    fn days_in_month(&self) -> u32;
    fn days_in_year(&self) -> u32;
    fn is_leap_year(&self) -> bool;
}

impl NaiveDateExt for NaiveDate {
    fn days_in_month(&self) -> u32 {
        let month = self.month();
        match month {
            1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
            4 | 6 | 9 | 11 => 30,
            2 => {
                if self.is_leap_year() {
                    29
                } else {
                    28
                }
            }
            _ => panic!("Invalid month: {}", month),
        }
    }

    fn days_in_year(&self) -> u32 {
        if self.is_leap_year() {
            366
        } else {
            365
        }
    }

    fn is_leap_year(&self) -> bool {
        let year = self.year();
        return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
    }
}

struct Day {
    day: u32,
    ce: bool,
    weekday: Weekday,
}

pub fn get_calendar_table(lua: &Lua, (year, month, day): (i32, u32, u32)) -> LuaResult<LuaTable> {
    let date =
        NaiveDate::from_ymd_opt(year, month, day).ok_or(LuaError::external("invalid date"))?;
    let days: Vec<Day> = (1..=date.days_in_month())
        .map(|day| NaiveDate::from_ymd_opt(date.year(), date.month(), day).unwrap())
        .map(|date| {
            let (ce, year) = date.year_ce();
            Day {
                day: date.day(),
                ce,
                weekday: date.weekday(),
            }
        })
        .collect();

    let calendar: Vec<Vec<Day>> = vec![vec![], vec![], vec![], vec![], vec![], vec![], vec![]];

    if days[1].weekday != Weekday::Mon {
        get_calendar_table(lua)
    }

    if days.last().unwrap().weekday != Weekday::Sun {}

    Ok(lua.create_table()?)
}