use crate::date::{DateTime, day_of_week, day_of_year, days_in_month};
pub(crate) fn the_date_of_the_v1(text: &str) -> Option<DateTime> {
let text = text.trim();
if text.is_empty() || !text.chars().all(|letter| letter.is_ascii_digit()) {
return None;
}
let digits = text.as_bytes();
if digits.len() < 8 {
return None;
}
let year = the_number_of_the_digits(digits, 0, 4);
let month = the_number_of_the_digits(digits, 4, 2);
let day = the_number_of_the_digits(digits, 6, 2);
let hour = the_number_of_the_digits(digits, 8, digits.len().saturating_sub(8).min(2));
let minute = the_number_of_the_digits(digits, 10, digits.len().saturating_sub(10).min(2));
let second = the_number_of_the_digits(digits, 12, digits.len().saturating_sub(12).min(2));
if !(1601..=9999).contains(&year) || !(1..=12).contains(&month) {
return None;
}
if day < 1 || day > days_in_month(year, month) {
return None;
}
if hour > 23 || minute > 59 || second > 59 {
return None;
}
Some(DateTime {
year,
month,
day,
hour,
minute,
second,
millisecond: 0,
weekday: day_of_week(year, month, day),
yday: day_of_year(year, month, day),
})
}
fn the_number_of_the_digits(digits: &[u8], at: usize, count: usize) -> i64 {
let mut number = 0;
for digit in digits.iter().skip(at).take(count) {
number = number * 10 + (digit - b'0') as i64;
}
number
}
pub(crate) fn the_text_is_a_number_of_days(text: &str) -> bool {
let text = text.trim();
!text.is_empty() && text.chars().all(|letter| letter.is_ascii_digit())
}