Skip to content

Commit

Permalink
Chrono update (#2397)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
elwerene committed Dec 6, 2022
1 parent 9e3da91 commit c3932f7
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/egui_demo_lib/src/demo/widget_gallery.rs
Expand Up @@ -20,7 +20,7 @@ pub struct WidgetGallery {

#[cfg(feature = "chrono")]
#[cfg_attr(feature = "serde", serde(skip))]
date: Option<chrono::Date<chrono::Utc>>,
date: Option<chrono::NaiveDate>,

#[cfg_attr(feature = "serde", serde(skip))]
texture: Option<egui::TextureHandle>,
Expand Down Expand Up @@ -218,7 +218,7 @@ impl WidgetGallery {

#[cfg(feature = "chrono")]
{
let date = date.get_or_insert_with(|| chrono::offset::Utc::now().date());
let date = date.get_or_insert_with(|| chrono::offset::Utc::now().date_naive());
ui.add(doc_link_label("DatePickerButton", "DatePickerButton"));
ui.add(egui_extras::DatePickerButton::new(date));
ui.end_row();
Expand Down
6 changes: 3 additions & 3 deletions crates/egui_extras/src/datepicker/button.rs
@@ -1,5 +1,5 @@
use super::popup::DatePickerPopup;
use chrono::{Date, Utc};
use chrono::NaiveDate;
use egui::{Area, Button, Frame, InnerResponse, Key, Order, RichText, Ui, Widget};

#[derive(Default, Clone, serde::Deserialize, serde::Serialize)]
Expand All @@ -8,7 +8,7 @@ pub(crate) struct DatePickerButtonState {
}

pub struct DatePickerButton<'a> {
selection: &'a mut Date<Utc>,
selection: &'a mut NaiveDate,
id_source: Option<&'a str>,
combo_boxes: bool,
arrows: bool,
Expand All @@ -17,7 +17,7 @@ pub struct DatePickerButton<'a> {
}

impl<'a> DatePickerButton<'a> {
pub fn new(selection: &'a mut Date<Utc>) -> Self {
pub fn new(selection: &'a mut NaiveDate) -> Self {
Self {
selection,
id_source: None,
Expand Down
6 changes: 3 additions & 3 deletions crates/egui_extras/src/datepicker/mod.rs
Expand Up @@ -2,16 +2,16 @@ mod button;
mod popup;

pub use button::DatePickerButton;
use chrono::{Date, Datelike, Duration, NaiveDate, Utc, Weekday};
use chrono::{Datelike, Duration, NaiveDate, Weekday};

#[derive(Debug)]
struct Week {
number: u8,
days: Vec<Date<Utc>>,
days: Vec<NaiveDate>,
}

fn month_data(year: i32, month: u32) -> Vec<Week> {
let first = Date::from_utc(NaiveDate::from_ymd(year, month, 1), Utc);
let first = NaiveDate::from_ymd_opt(year, month, 1).expect("Could not create NaiveDate");
let mut start = first;
while start.weekday() != Weekday::Mon {
start = start.checked_sub_signed(Duration::days(1)).unwrap();
Expand Down
23 changes: 11 additions & 12 deletions crates/egui_extras/src/datepicker/popup.rs
@@ -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};

Expand All @@ -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))
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit c3932f7

Please sign in to comment.