use crate::DateKind;
#[derive(Debug, Clone, Copy)]
pub struct FormatSpec {
pub template: &'static str,
pub kind: DateKind,
pub has_century: bool,
}
#[must_use]
pub const fn format_kind(template: &'static str) -> DateKind {
let bytes = template.as_bytes();
let mut i = 0;
let mut has_h = false;
let mut has_d = false;
while i < bytes.len() {
if bytes[i] == b'H' {
has_h = true;
}
if bytes[i] == b'D' {
has_d = true;
}
i += 1;
}
if has_h {
DateKind::DateTime
} else if has_d {
DateKind::DateOnly
} else {
DateKind::MonthOnly
}
}
const fn has_century(template: &'static str) -> bool {
let bytes = template.as_bytes();
let mut i = 0;
let mut yyy_count = 0u8;
while i < bytes.len() {
if bytes[i] == b'Y' {
yyy_count += 1;
if yyy_count >= 4 {
return true;
}
} else {
yyy_count = 0;
}
i += 1;
}
false
}
const fn spec(template: &'static str) -> FormatSpec {
FormatSpec {
template,
kind: format_kind(template),
has_century: has_century(template),
}
}
pub const POSSIBLE_FORMATS: &[FormatSpec] = &[
spec("MM_YYYY"),
spec("DD_MM_YYYY"),
spec("DD_MM_YY"),
spec("DDMMYYYY"),
spec("DDMMYY"),
spec("DD_MM_YYYY_HH_mm_ss"),
spec("DD_MM_YY_HH_mm_ss"),
spec("DD_MM_YYYY_HH_mm"),
spec("DDMMYYYYHHmm"),
spec("YYYY_MM"),
spec("YYYY_MM_DD"),
spec("YYYYMMDD"),
spec("YY_MM_DD_HH_mm_ss"),
spec("YYYY_MM_DD_HH_mm_ss"),
spec("YYYY_MM_DD_HH_mm"),
spec("YYYYMMDDHHmmss"),
spec("YYYYMMDD_HHmmss"),
];