use chrono::format::{Fixed, Item, Numeric, StrftimeItems};
use chrono::{DateTime, Datelike, NaiveDate, NaiveDateTime, SecondsFormat};
use crate::row_batches::models::{Cell, Parse};
const SHOWN_CHARS: usize = 60;
const YEARS: std::ops::RangeInclusive<i32> = 1..=9999;
pub(crate) fn parsed(cell: &Cell, parse: &Parse) -> Result<Cell, String> {
let micros_per_unit: i64 = match parse {
Parse::EpochS => 1_000_000,
Parse::EpochMs => 1_000,
Parse::EpochUs => 1,
Parse::Timestamp { format } => return timestamp(cell, format),
Parse::Date { format } => return date(cell, format),
};
let expected = || {
let unit = match parse {
Parse::EpochS => "seconds",
Parse::EpochMs => "milliseconds",
_ => "microseconds",
};
format!("a number of {unit} since 1970-01-01 UTC, in the years 1 to 9999")
};
let instant = epoch_micros(cell, micros_per_unit)
.and_then(DateTime::from_timestamp_micros)
.filter(|instant| YEARS.contains(&instant.year()))
.ok_or_else(expected)?;
Ok(Cell::Text(
instant.to_rfc3339_opts(SecondsFormat::AutoSi, true),
))
}
fn epoch_micros(cell: &Cell, micros_per_unit: i64) -> Option<i64> {
let number = match cell {
Cell::Int(value) => return value.checked_mul(micros_per_unit),
Cell::Float(value) => *value,
Cell::Text(text) | Cell::Number(text) => match text.trim().parse::<i64>() {
Ok(whole) => return whole.checked_mul(micros_per_unit),
Err(_) => match text.trim().parse::<f64>() {
Ok(number) => number,
Err(_) => return None,
},
},
_ => return None,
};
let micros = (number * micros_per_unit as f64).round();
(micros.is_finite() && micros.abs() < i64::MAX as f64).then_some(micros as i64)
}
pub(crate) fn format_problem(parse: &Parse) -> Option<String> {
let (Parse::Timestamp { format } | Parse::Date { format }) = parse else {
return None;
};
if StrftimeItems::new(format).any(|item| item == Item::Error) {
return Some(format!(
"parse format {format:?} has a directive that is not a strftime one such as %Y, %m, %d, %H, %M, %S or %z"
));
}
let nanoseconds = |item: Item<'_>| matches!(item, Item::Numeric(Numeric::Nanosecond, _));
StrftimeItems::new(format).any(nanoseconds).then(|| {
format!(
"parse format {format:?} has %f, which reads nanoseconds here; write %.f for a fraction after a dot, as in \"%H:%M:%S%.f\" for 08:05:00.123456"
)
})
}
pub(crate) fn shown(text: &str) -> String {
match text.chars().count() > SHOWN_CHARS {
true => format!("{}...", text.chars().take(SHOWN_CHARS).collect::<String>()),
false => text.to_owned(),
}
}
fn timestamp(cell: &Cell, format: &str) -> Result<Cell, String> {
let expected = || format!("a timestamp in the format {format:?}");
let Cell::Text(text) = cell else {
return Err(expected());
};
match has_offset(format) {
true => match DateTime::parse_from_str(text, format) {
Ok(instant) if YEARS.contains(&instant.to_utc().year()) => Ok(Cell::Text(
instant
.to_utc()
.to_rfc3339_opts(SecondsFormat::AutoSi, true),
)),
Ok(_) | Err(_) => Err(expected()),
},
false => match NaiveDateTime::parse_from_str(text, format) {
Ok(moment) if YEARS.contains(&moment.year()) => {
Ok(Cell::TimestampNtz(moment.and_utc().timestamp_micros()))
}
Ok(_) | Err(_) => Err(expected()),
},
}
}
fn date(cell: &Cell, format: &str) -> Result<Cell, String> {
let expected = || format!("a date in the format {format:?}");
let Cell::Text(text) = cell else {
return Err(expected());
};
let day = match NaiveDate::parse_from_str(text, format) {
Ok(day) if YEARS.contains(&day.year()) => day,
Ok(_) | Err(_) => return Err(expected()),
};
let days = day
.signed_duration_since(DateTime::UNIX_EPOCH.date_naive())
.num_days();
i32::try_from(days).map(Cell::Date).map_err(|_| expected())
}
fn has_offset(format: &str) -> bool {
format.contains("%#z")
|| StrftimeItems::new(format).any(|item| {
matches!(
item,
Item::Fixed(
Fixed::TimezoneOffset
| Fixed::TimezoneOffsetColon
| Fixed::TimezoneOffsetDoubleColon
| Fixed::TimezoneOffsetTripleColon
| Fixed::TimezoneOffsetColonZ
| Fixed::TimezoneOffsetZ
| Fixed::RFC3339
)
)
})
}