1use std::fmt;
10use std::time::Duration;
11
12use waft_foundation::{App, DateTime};
13use waft_services::TextInputFormatterRef;
14
15use crate::material_localizations::MaterialLocalizations;
16
17pub trait CalendarDelegate: fmt::Debug {
24 fn now(&self, app: &App) -> DateTime;
26
27 fn date_only(&self, app: &App, date: &DateTime) -> DateTime;
29
30 fn dates_only(&self, app: &App, range: &DateTimeRange) -> DateTimeRange {
32 DateTimeRange::new(
33 self.date_only(app, &range.start),
34 self.date_only(app, &range.end),
35 )
36 }
37
38 fn is_same_day(&self, app: &App, date_a: Option<&DateTime>, date_b: Option<&DateTime>) -> bool {
41 date_a.map(|date| (date.year(app), date.month(app), date.day(app)))
42 == date_b.map(|date| (date.year(app), date.month(app), date.day(app)))
43 }
44
45 fn is_same_month(
48 &self,
49 app: &App,
50 date_a: Option<&DateTime>,
51 date_b: Option<&DateTime>,
52 ) -> bool {
53 date_a.map(|date| (date.year(app), date.month(app)))
54 == date_b.map(|date| (date.year(app), date.month(app)))
55 }
56
57 fn month_delta(&self, app: &App, start_date: &DateTime, end_date: &DateTime) -> i32;
59
60 fn add_months_to_month_date(
63 &self,
64 app: &App,
65 month_date: &DateTime,
66 months_to_add: i32,
67 ) -> DateTime;
68
69 fn add_days_to_date(&self, app: &App, date: &DateTime, days: i32) -> DateTime;
71
72 fn first_day_offset(
75 &self,
76 app: &App,
77 year: i32,
78 month: i32,
79 localizations: &dyn MaterialLocalizations,
80 ) -> i32;
81
82 fn get_days_in_month(&self, year: i32, month: i32) -> i32;
84
85 fn get_month(&self, app: &App, year: i32, month: i32) -> DateTime;
87
88 fn get_day(&self, app: &App, year: i32, month: i32, day: i32) -> DateTime;
90
91 fn format_month_year(
93 &self,
94 app: &App,
95 date: &DateTime,
96 localizations: &dyn MaterialLocalizations,
97 ) -> String;
98
99 fn format_year(
101 &self,
102 app: &App,
103 year: i32,
104 localizations: &dyn MaterialLocalizations,
105 ) -> String {
106 localizations.format_year(app, &DateTime::new(app, year, 1, 1))
107 }
108
109 fn format_medium_date(
111 &self,
112 app: &App,
113 date: &DateTime,
114 localizations: &dyn MaterialLocalizations,
115 ) -> String;
116
117 fn format_short_month_day(
119 &self,
120 app: &App,
121 date: &DateTime,
122 localizations: &dyn MaterialLocalizations,
123 ) -> String;
124
125 fn format_short_date(
127 &self,
128 app: &App,
129 date: &DateTime,
130 localizations: &dyn MaterialLocalizations,
131 ) -> String;
132
133 fn format_full_date(
135 &self,
136 app: &App,
137 date: &DateTime,
138 localizations: &dyn MaterialLocalizations,
139 ) -> String;
140
141 fn format_compact_date(
143 &self,
144 app: &App,
145 date: &DateTime,
146 localizations: &dyn MaterialLocalizations,
147 ) -> String;
148
149 fn parse_compact_date(
151 &self,
152 app: &App,
153 input_string: Option<&str>,
154 localizations: &dyn MaterialLocalizations,
155 ) -> Option<DateTime>;
156
157 fn date_help_text(&self, localizations: &dyn MaterialLocalizations) -> String;
159
160 fn keyboard_input_formatters(
162 &self,
163 _localizations: &dyn MaterialLocalizations,
164 ) -> Option<Vec<TextInputFormatterRef>> {
165 None
166 }
167}
168
169#[derive(Clone, Copy, Debug, Default)]
171pub struct GregorianCalendarDelegate;
172
173impl CalendarDelegate for GregorianCalendarDelegate {
174 fn now(&self, _app: &App) -> DateTime {
175 DateTime::now()
176 }
177
178 fn date_only(&self, app: &App, date: &DateTime) -> DateTime {
179 DateUtils::date_only(app, date)
180 }
181
182 fn month_delta(&self, app: &App, start_date: &DateTime, end_date: &DateTime) -> i32 {
183 DateUtils::month_delta(app, start_date, end_date)
184 }
185
186 fn add_months_to_month_date(
187 &self,
188 app: &App,
189 month_date: &DateTime,
190 months_to_add: i32,
191 ) -> DateTime {
192 DateUtils::add_months_to_month_date(app, month_date, months_to_add)
193 }
194
195 fn add_days_to_date(&self, app: &App, date: &DateTime, days: i32) -> DateTime {
196 DateUtils::add_days_to_date(app, date, days)
197 }
198
199 fn first_day_offset(
200 &self,
201 app: &App,
202 year: i32,
203 month: i32,
204 localizations: &dyn MaterialLocalizations,
205 ) -> i32 {
206 DateUtils::first_day_offset(app, year, month, localizations)
207 }
208
209 fn get_days_in_month(&self, year: i32, month: i32) -> i32 {
210 DateUtils::get_days_in_month(year, month)
211 }
212
213 fn get_month(&self, app: &App, year: i32, month: i32) -> DateTime {
214 DateTime::new(app, year, month, 1)
215 }
216
217 fn get_day(&self, app: &App, year: i32, month: i32, day: i32) -> DateTime {
218 DateTime::new(app, year, month, day)
219 }
220
221 fn format_month_year(
222 &self,
223 app: &App,
224 date: &DateTime,
225 localizations: &dyn MaterialLocalizations,
226 ) -> String {
227 localizations.format_month_year(app, date)
228 }
229
230 fn format_medium_date(
231 &self,
232 app: &App,
233 date: &DateTime,
234 localizations: &dyn MaterialLocalizations,
235 ) -> String {
236 localizations.format_medium_date(app, date)
237 }
238
239 fn format_short_month_day(
240 &self,
241 app: &App,
242 date: &DateTime,
243 localizations: &dyn MaterialLocalizations,
244 ) -> String {
245 localizations.format_short_month_day(app, date)
246 }
247
248 fn format_short_date(
249 &self,
250 app: &App,
251 date: &DateTime,
252 localizations: &dyn MaterialLocalizations,
253 ) -> String {
254 localizations.format_short_date(app, date)
255 }
256
257 fn format_full_date(
258 &self,
259 app: &App,
260 date: &DateTime,
261 localizations: &dyn MaterialLocalizations,
262 ) -> String {
263 localizations.format_full_date(app, date)
264 }
265
266 fn format_compact_date(
267 &self,
268 app: &App,
269 date: &DateTime,
270 localizations: &dyn MaterialLocalizations,
271 ) -> String {
272 localizations.format_compact_date(app, date)
273 }
274
275 fn parse_compact_date(
276 &self,
277 app: &App,
278 input_string: Option<&str>,
279 localizations: &dyn MaterialLocalizations,
280 ) -> Option<DateTime> {
281 localizations.parse_compact_date(app, input_string)
282 }
283
284 fn date_help_text(&self, localizations: &dyn MaterialLocalizations) -> String {
285 localizations.date_help_text()
286 }
287}
288
289pub struct DateUtils;
291
292impl DateUtils {
293 pub fn date_only(app: &App, date: &DateTime) -> DateTime {
295 DateTime::new(app, date.year(app), date.month(app), date.day(app))
296 }
297
298 pub fn dates_only(app: &App, range: &DateTimeRange) -> DateTimeRange {
303 DateTimeRange::new(
304 Self::date_only(app, &range.start),
305 Self::date_only(app, &range.end),
306 )
307 }
308
309 pub fn is_same_day(app: &App, date_a: Option<&DateTime>, date_b: Option<&DateTime>) -> bool {
312 GregorianCalendarDelegate.is_same_day(app, date_a, date_b)
313 }
314
315 pub fn is_same_month(app: &App, date_a: Option<&DateTime>, date_b: Option<&DateTime>) -> bool {
318 GregorianCalendarDelegate.is_same_month(app, date_a, date_b)
319 }
320
321 pub fn month_delta(app: &App, start_date: &DateTime, end_date: &DateTime) -> i32 {
339 (end_date.year(app) - start_date.year(app)) * 12 + end_date.month(app)
340 - start_date.month(app)
341 }
342
343 pub fn add_months_to_month_date(
360 app: &App,
361 month_date: &DateTime,
362 months_to_add: i32,
363 ) -> DateTime {
364 DateTime::new(
365 app,
366 month_date.year(app),
367 month_date.month(app) + months_to_add,
368 1,
369 )
370 }
371
372 pub fn add_days_to_date(app: &App, date: &DateTime, days: i32) -> DateTime {
374 DateTime::new(app, date.year(app), date.month(app), date.day(app) + days)
375 }
376
377 pub fn first_day_offset(
410 app: &App,
411 year: i32,
412 month: i32,
413 localizations: &dyn MaterialLocalizations,
414 ) -> i32 {
415 let weekday_from_monday = DateTime::new(app, year, month, 1).weekday(app) - 1;
417
418 let first_day_of_week_index = localizations.first_day_of_week_index();
420
421 let first_day_of_week_index = (first_day_of_week_index - 1).rem_euclid(7);
424
425 (weekday_from_monday - first_day_of_week_index).rem_euclid(7)
428 }
429
430 pub fn get_days_in_month(year: i32, month: i32) -> i32 {
435 if month == DateTime::FEBRUARY {
436 let is_leap_year = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
437 return if is_leap_year { 29 } else { 28 };
438 }
439 const DAYS_IN_MONTH: [i32; 12] = [31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
440 DAYS_IN_MONTH[(month - 1) as usize]
441 }
442}
443
444pub(crate) fn is_day_before(app: &App, a: &DateTime, b: &DateTime) -> bool {
448 (a.year(app), a.month(app), a.day(app)) < (b.year(app), b.month(app), b.day(app))
449}
450
451#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
456pub enum DatePickerEntryMode {
457 Calendar,
460
461 Input,
464
465 CalendarOnly,
468
469 InputOnly,
472}
473
474#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
478pub enum DatePickerMode {
479 Day,
481
482 Year,
484}
485
486#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
495pub struct DateTimeRange {
496 pub start: DateTime,
498
499 pub end: DateTime,
501}
502
503impl DateTimeRange {
504 pub fn new(start: DateTime, end: DateTime) -> DateTimeRange {
508 debug_assert!(!start.is_after(&end), "start must not be after end");
509 DateTimeRange { start, end }
510 }
511
512 pub fn duration(&self) -> Duration {
517 Duration::from_micros(
518 (self.end.microseconds_since_epoch() - self.start.microseconds_since_epoch()) as u64,
519 )
520 }
521}
522
523impl DateTimeRange {
524 pub fn to_string(&self, app: &App) -> String {
526 format!(
527 "{} - {}",
528 self.start.to_string(app),
529 self.end.to_string(app)
530 )
531 }
532}
533
534#[cfg(test)]
535mod tests {
536 use waft_foundation::AppCell;
537
538 use super::*;
539 use crate::material_localizations::DefaultMaterialLocalizations;
540
541 #[test]
544 fn date_utils_count_local_calendar_days_across_a_change_of_offset() {
545 use std::rc::Rc;
546 use waft_embedder::{PlatformRef, SignedDuration};
547 use waft_test::{TestPlatform, TestTimeZone};
548 const SPRING_FORWARD: i64 = 1_774_746_000;
550 let central_europe = TestTimeZone::new(|seconds| {
551 if seconds >= SPRING_FORWARD {
552 (SignedDuration::from_hours(2), "CEST".to_string())
553 } else {
554 (SignedDuration::from_hours(1), "CET".to_string())
555 }
556 });
557 let platform: PlatformRef = Rc::new(TestPlatform::new().with_time_zone(central_europe));
558 let cell = AppCell::with_platform(platform);
559 let app = cell.borrow();
560
561 let late_evening = DateTime::new_with_time(&app, 2026, 3, 28, 23, 30, 0, 0, 0);
562 let day = DateUtils::date_only(&app, &late_evening);
563 assert_eq!((day.day(&app), day.hour(&app)), (28, 0));
564 let next = DateUtils::add_days_to_date(&app, &day, 1);
565 let after = DateUtils::add_days_to_date(&app, &day, 2);
566 assert_eq!((next.day(&app), next.hour(&app)), (29, 0));
567 assert_eq!((after.day(&app), after.hour(&app)), (30, 0));
568 assert_eq!(
569 after.microseconds_since_epoch() - next.microseconds_since_epoch(),
570 23 * 60 * 60 * 1_000_000,
571 "the day the clocks go forward has 23 hours"
572 );
573 assert!(DateUtils::is_same_day(
574 &app,
575 Some(&late_evening),
576 Some(&day)
577 ));
578 }
579
580 #[test]
581 fn date_utils_date_only_drops_the_time() {
582 let cell = AppCell::new();
583 let app = cell.borrow();
584 let date = DateTime::new_with_time(&app, 2019, 6, 15, 13, 45, 30, 0, 0);
585 assert_eq!(
586 DateUtils::date_only(&app, &date),
587 DateTime::new(&app, 2019, 6, 15)
588 );
589 }
590
591 #[test]
592 fn date_utils_dates_only_drops_the_times_of_a_range() {
593 let cell = AppCell::new();
594 let app = cell.borrow();
595 let range = DateTimeRange::new(
596 DateTime::new_with_time(&app, 2019, 6, 15, 13, 0, 0, 0, 0),
597 DateTime::new_with_time(&app, 2019, 6, 20, 1, 0, 0, 0, 0),
598 );
599 let dates = DateUtils::dates_only(&app, &range);
600 assert_eq!(dates.start, DateTime::new(&app, 2019, 6, 15));
601 assert_eq!(dates.end, DateTime::new(&app, 2019, 6, 20));
602 }
603
604 #[test]
605 fn date_utils_is_same_day_and_month_compare_the_calendar_fields() {
606 let cell = AppCell::new();
607 let app = cell.borrow();
608 let morning = DateTime::new_with_time(&app, 2019, 6, 15, 1, 0, 0, 0, 0);
609 let evening = DateTime::new_with_time(&app, 2019, 6, 15, 23, 0, 0, 0, 0);
610 let next_day = DateTime::new(&app, 2019, 6, 16);
611 let next_year = DateTime::new(&app, 2020, 6, 15);
612 assert!(DateUtils::is_same_day(&app, Some(&morning), Some(&evening)));
613 assert!(!DateUtils::is_same_day(
614 &app,
615 Some(&morning),
616 Some(&next_day)
617 ));
618 assert!(!DateUtils::is_same_day(
619 &app,
620 Some(&morning),
621 Some(&next_year)
622 ));
623 assert!(DateUtils::is_same_day(&app, None, None));
624 assert!(!DateUtils::is_same_day(&app, Some(&morning), None));
625 assert!(DateUtils::is_same_month(
626 &app,
627 Some(&morning),
628 Some(&next_day)
629 ));
630 assert!(!DateUtils::is_same_month(
631 &app,
632 Some(&morning),
633 Some(&next_year)
634 ));
635 assert!(DateUtils::is_same_month(&app, None, None));
636 }
637
638 #[test]
639 fn date_utils_month_delta_counts_whole_months() {
640 let cell = AppCell::new();
641 let app = cell.borrow();
642 let date1 = DateTime::new(&app, 2019, 6, 15);
643 let date2 = DateTime::new(&app, 2020, 1, 15);
644 assert_eq!(DateUtils::month_delta(&app, &date1, &date2), 7);
645 assert_eq!(DateUtils::month_delta(&app, &date2, &date1), -7);
646 }
647
648 #[test]
649 fn date_utils_adds_months_and_days_across_year_ends() {
650 let cell = AppCell::new();
651 let app = cell.borrow();
652 let december = DateTime::new(&app, 2019, 12, 1);
653 assert_eq!(
654 DateUtils::add_months_to_month_date(&app, &december, 1),
655 DateTime::new(&app, 2020, 1, 1)
656 );
657 assert_eq!(
658 DateUtils::add_months_to_month_date(&app, &DateTime::new(&app, 2020, 1, 1), -1),
659 december
660 );
661 assert_eq!(
662 DateUtils::add_days_to_date(&app, &DateTime::new(&app, 2019, 12, 31), 1),
663 DateTime::new(&app, 2020, 1, 1)
664 );
665 assert_eq!(
666 DateUtils::add_days_to_date(&app, &DateTime::new(&app, 2020, 3, 1), -1),
667 DateTime::new(&app, 2020, 2, 29)
668 );
669 }
670
671 #[test]
672 fn date_utils_get_days_in_month_follows_the_gregorian_leap_rule() {
673 assert_eq!(DateUtils::get_days_in_month(2019, 2), 28);
674 assert_eq!(DateUtils::get_days_in_month(2020, 2), 29);
675 assert_eq!(DateUtils::get_days_in_month(1900, 2), 28);
676 assert_eq!(DateUtils::get_days_in_month(2000, 2), 29);
677 assert_eq!(DateUtils::get_days_in_month(2019, 1), 31);
678 assert_eq!(DateUtils::get_days_in_month(2019, 4), 30);
679 assert_eq!(DateUtils::get_days_in_month(2019, 12), 31);
680 }
681
682 #[test]
683 fn date_utils_first_day_offset_counts_the_blanks_before_the_first() {
684 let cell = AppCell::new();
686 let app = cell.borrow();
687 let english = DefaultMaterialLocalizations;
688 assert_eq!(DateUtils::first_day_offset(&app, 2017, 9, &english), 5);
689 }
690
691 #[test]
692 fn date_time_range_has_the_duration_between_its_dates() {
693 let cell = AppCell::new();
694 let app = cell.borrow();
695 let range = DateTimeRange::new(
696 DateTime::new(&app, 2019, 6, 15),
697 DateTime::new(&app, 2019, 6, 20),
698 );
699 assert_eq!(range.duration(), Duration::from_secs(5 * 24 * 60 * 60));
700 assert_eq!(
701 range.to_string(&app),
702 "2019-06-15 00:00:00.000 - 2019-06-20 00:00:00.000"
703 );
704 }
705
706 #[test]
707 #[should_panic]
708 fn date_time_range_asserts_that_start_is_not_after_end() {
709 let cell = AppCell::new();
710 let app = cell.borrow();
711 DateTimeRange::new(
712 DateTime::new(&app, 2019, 6, 20),
713 DateTime::new(&app, 2019, 6, 15),
714 );
715 }
716
717 #[test]
718 fn the_gregorian_delegate_builds_months_and_days() {
719 let cell = AppCell::new();
720 let app = cell.borrow();
721 let delegate = GregorianCalendarDelegate;
722 assert_eq!(
723 delegate.get_month(&app, 2019, 3),
724 DateTime::new(&app, 2019, 3, 1)
725 );
726 assert_eq!(
727 delegate.get_day(&app, 2019, 3, 4),
728 DateTime::new(&app, 2019, 3, 4)
729 );
730 assert_eq!(delegate.get_days_in_month(2020, 2), 29);
731 }
732}