dots/.config/awesome/quarrel/native/src/time.rs
delta f42a3a2cc9 major update of awesome config
add new icons, switch over to using stylesheets instead of gears.color.recolor_image, add a music widget to the panel, optimize services in common.lua, fix the application lense filtering and increase the update rate of services in common.lua

Signed-off-by: delta <darkussdelta@gmail.com>
2023-05-21 10:12:46 +02:00

76 lines
1.9 KiB
Rust

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()?)
}