1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 04:40:03 -04:00

Chrono update (#2397)

* limit day to last day of month if the month or year is changed

* update chrono to 0.4.23, switch to NaiveDate and remove use of deprecated functions.
This commit is contained in:
René Rössler
2022-12-06 11:42:53 +01:00
committed by GitHub
parent 9e3da91a59
commit c3932f7f7f
5 changed files with 21 additions and 22 deletions

View File

@@ -1,4 +1,4 @@
use chrono::{Date, Datelike, NaiveDate, Utc, Weekday};
use chrono::{Datelike, NaiveDate, Weekday};
use egui::{Align, Button, Color32, ComboBox, Direction, Id, Layout, RichText, Ui, Vec2};
@@ -16,7 +16,8 @@ struct DatePickerPopupState {
impl DatePickerPopupState {
fn last_day_of_month(&self) -> u32 {
let date: Date<Utc> = Date::from_utc(NaiveDate::from_ymd(self.year, self.month, 1), Utc);
let date: NaiveDate =
NaiveDate::from_ymd_opt(self.year, self.month, 1).expect("Could not create NaiveDate");
date.with_day(31)
.map(|_| 31)
.or_else(|| date.with_day(30).map(|_| 30))
@@ -26,7 +27,7 @@ impl DatePickerPopupState {
}
pub(crate) struct DatePickerPopup<'a> {
pub selection: &'a mut Date<Utc>,
pub selection: &'a mut NaiveDate,
pub button_id: Id,
pub combo_boxes: bool,
pub arrows: bool,
@@ -38,7 +39,7 @@ impl<'a> DatePickerPopup<'a> {
/// Returns `true` if user pressed `Save` button.
pub fn draw(&mut self, ui: &mut Ui) -> bool {
let id = ui.make_persistent_id("date_picker");
let today = chrono::offset::Utc::now().date();
let today = chrono::offset::Utc::now().date_naive();
let mut popup_state = ui
.memory()
.data
@@ -369,14 +370,12 @@ impl<'a> DatePickerPopup<'a> {
strip.cell(|ui| {
ui.with_layout(Layout::top_down_justified(Align::Center), |ui| {
if ui.button("Save").clicked() {
*self.selection = Date::from_utc(
NaiveDate::from_ymd(
popup_state.year,
popup_state.month,
popup_state.day,
),
Utc,
);
*self.selection = NaiveDate::from_ymd_opt(
popup_state.year,
popup_state.month,
popup_state.day,
)
.expect("Could not create NaiveDate");
saved = true;
close = true;
}