| 1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
| 2 | // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 |
| 3 | |
| 4 | use crate::SharedString; |
| 5 | #[cfg (feature = "std" )] |
| 6 | use chrono::Local; |
| 7 | use chrono::{Datelike, NaiveDate}; |
| 8 | |
| 9 | pub fn use_24_hour_format() -> bool { |
| 10 | true |
| 11 | } |
| 12 | |
| 13 | /// Returns the number of days in a given month |
| 14 | pub fn month_day_count(month: u32, year: i32) -> Option<i32> { |
| 15 | Some( |
| 16 | NaiveDateTimeDelta::from_ymd_opt( |
| 17 | year:match month { |
| 18 | 12 => year + 1, |
| 19 | _ => year, |
| 20 | }, |
| 21 | month:match month { |
| 22 | 12 => 1, |
| 23 | _ => month + 1, |
| 24 | }, |
| 25 | day:1, |
| 26 | )? |
| 27 | .signed_duration_since(NaiveDate::from_ymd_opt(year, month, day:1)?) |
| 28 | .num_days() as i32, |
| 29 | ) |
| 30 | } |
| 31 | |
| 32 | pub fn month_offset(month: u32, year: i32) -> i32 { |
| 33 | if let Some(date: NaiveDate) = NaiveDate::from_ymd_opt(year, month, day:1) { |
| 34 | let offset: i32 = date.weekday().number_from_monday() as i32; |
| 35 | |
| 36 | // sunday |
| 37 | if offset >= 7 { |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | return offset; |
| 42 | } |
| 43 | |
| 44 | // The result is only None if month == 0, it should not happen because the function is only |
| 45 | // used internal and not directly by the user. So it is ok to return 0 on a None result |
| 46 | 0 |
| 47 | } |
| 48 | |
| 49 | pub fn format_date(format: &str, day: u32, month: u32, year: i32) -> SharedString { |
| 50 | if let Some(date: NaiveDate) = NaiveDate::from_ymd_opt(year, month, day) { |
| 51 | return crate::format!(" {}" , date.format(format)); |
| 52 | } |
| 53 | |
| 54 | // Don't panic, this function is used only internal |
| 55 | SharedString::default() |
| 56 | } |
| 57 | |
| 58 | pub fn parse_date(date: &str, format: &str) -> Option<[i32; 3]> { |
| 59 | NaiveDateOption::parse_from_str(s:date, fmt:format) |
| 60 | .ok() |
| 61 | .map(|date: NaiveDate| [date.day() as i32, date.month() as i32, date.year()]) |
| 62 | } |
| 63 | |
| 64 | #[cfg (feature = "std" )] |
| 65 | pub fn date_now() -> [i32; 3] { |
| 66 | let now: NaiveDate = Local::now().date_naive(); |
| 67 | [now.day() as i32, now.month() as i32, now.year()] |
| 68 | } |
| 69 | |
| 70 | // display the today date is currently not implemented for no_std |
| 71 | #[cfg (not(feature = "std" ))] |
| 72 | pub fn date_now() -> [i32; 3] { |
| 73 | [-1, -1, -1] |
| 74 | } |
| 75 | |
| 76 | #[cfg (feature = "ffi" )] |
| 77 | mod ffi { |
| 78 | #![allow (unsafe_code)] |
| 79 | |
| 80 | use super::*; |
| 81 | |
| 82 | #[no_mangle ] |
| 83 | pub extern "C" fn slint_date_time_use_24_hour_format() -> bool { |
| 84 | use_24_hour_format() |
| 85 | } |
| 86 | |
| 87 | #[no_mangle ] |
| 88 | pub extern "C" fn slint_date_time_month_day_count(month: u32, year: i32) -> i32 { |
| 89 | month_day_count(month, year).unwrap_or(0) |
| 90 | } |
| 91 | |
| 92 | #[no_mangle ] |
| 93 | pub extern "C" fn slint_date_time_month_offset(month: u32, year: i32) -> i32 { |
| 94 | month_offset(month, year) |
| 95 | } |
| 96 | |
| 97 | #[no_mangle ] |
| 98 | pub extern "C" fn slint_date_time_format_date( |
| 99 | format: &SharedString, |
| 100 | day: u32, |
| 101 | month: u32, |
| 102 | year: i32, |
| 103 | out: &mut SharedString, |
| 104 | ) { |
| 105 | *out = format_date(format, day, month, year) |
| 106 | } |
| 107 | |
| 108 | #[no_mangle ] |
| 109 | pub extern "C" fn slint_date_time_date_now(d: &mut i32, m: &mut i32, y: &mut i32) { |
| 110 | [*d, *m, *y] = date_now(); |
| 111 | } |
| 112 | |
| 113 | #[no_mangle ] |
| 114 | pub extern "C" fn slint_date_time_parse_date( |
| 115 | date: &SharedString, |
| 116 | format: &SharedString, |
| 117 | d: &mut i32, |
| 118 | m: &mut i32, |
| 119 | y: &mut i32, |
| 120 | ) -> bool { |
| 121 | if let Some(x) = parse_date(date, format) { |
| 122 | [*d, *m, *y] = x; |
| 123 | true |
| 124 | } else { |
| 125 | false |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |