use chrono for day of week labels
move `window::get_sunday` to `time::get_calender_first`
This commit is contained in:
parent
ee73884b07
commit
e5f135a76e
3 changed files with 17 additions and 10 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
mod localize;
|
mod localize;
|
||||||
|
mod time;
|
||||||
mod window;
|
mod window;
|
||||||
|
|
||||||
use window::Window;
|
use window::Window;
|
||||||
|
|
|
||||||
8
cosmic-applet-time/src/time.rs
Normal file
8
cosmic-applet-time/src/time.rs
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|
@ -15,10 +15,11 @@ use cosmic::{
|
||||||
Element, Theme,
|
Element, Theme,
|
||||||
};
|
};
|
||||||
|
|
||||||
use chrono::{DateTime, Datelike, Days, Local, NaiveDate, Timelike};
|
use chrono::{DateTime, Datelike, Local, Timelike, Weekday};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use crate::fl;
|
use crate::fl;
|
||||||
|
use crate::time::get_calender_first;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
|
@ -240,17 +241,20 @@ impl cosmic::Application for Window {
|
||||||
|
|
||||||
// Calender
|
// Calender
|
||||||
let mut calender: Grid<'_, Message> = grid().width(Length::Fill);
|
let mut calender: Grid<'_, Message> = grid().width(Length::Fill);
|
||||||
for day_of_week in ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] {
|
let mut first_day_of_week = Weekday::Sun; // TODO: Configurable
|
||||||
|
for _ in 0..7 {
|
||||||
calender = calender.push(
|
calender = calender.push(
|
||||||
text(day_of_week)
|
text(first_day_of_week)
|
||||||
.size(12)
|
.size(12)
|
||||||
.width(Length::Fixed(36.0))
|
.width(Length::Fixed(36.0))
|
||||||
.horizontal_alignment(Horizontal::Center),
|
.horizontal_alignment(Horizontal::Center),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
first_day_of_week = first_day_of_week.succ();
|
||||||
}
|
}
|
||||||
calender = calender.insert_row();
|
calender = calender.insert_row();
|
||||||
|
|
||||||
let monday = get_sunday(self.now.year(), self.now.month());
|
let monday = get_calender_first(self.now.year(), self.now.month(), first_day_of_week);
|
||||||
let mut day_iter = monday.iter_days();
|
let mut day_iter = monday.iter_days();
|
||||||
for i in 0..35 {
|
for i in 0..35 {
|
||||||
if i > 0 && i % 7 == 0 {
|
if i > 0 && i % 7 == 0 {
|
||||||
|
|
@ -322,9 +326,3 @@ fn date_button(
|
||||||
button
|
button
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_sunday(year: i32, month: u32) -> NaiveDate {
|
|
||||||
let date = NaiveDate::from_ymd_opt(year, month, 1).unwrap();
|
|
||||||
let num_days = date.weekday().num_days_from_sunday();
|
|
||||||
date.checked_sub_days(Days::new(num_days as u64)).unwrap()
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue