cosmic-applets/cosmic-applet-time/src/time.rs
2024-05-06 21:56:50 +02:00

11 lines
525 B
Rust

// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only
use chrono::{Datelike, Days, NaiveDate, Weekday};
/// Gets the first date that will be visible on the calender
pub fn get_calender_first(year: i32, month: u32, from_weekday: Weekday) -> NaiveDate {
let date = NaiveDate::from_ymd_opt(year, month, 1).unwrap();
let num_days = (date.weekday() as u32 + 7 - from_weekday as u32) % 7; // chrono::Weekday.num_days_from
date.checked_sub_days(Days::new(num_days as u64)).unwrap()
}