use chrono::{DateTime, SecondsFormat};
use chrono_tz::Tz;
use crate::column_parse::constants::SHOWN_CHARS;
use crate::column_parse::main::parsed_value::parsed_value;
use crate::column_parse::models::{ColumnParse, ParsedTime, RawTime};
use crate::row_batches::models::Cell;
pub(crate) fn parsed(cell: &Cell, parse: &ColumnParse, zone: Tz) -> Result<Cell, String> {
let raw = match cell {
Cell::Text(text) | Cell::Number(text) => RawTime::Text(text),
Cell::Int(value) => RawTime::Int(*value),
Cell::Float(value) => RawTime::Float(*value),
_ => RawTime::Text(""),
};
match parsed_value(raw, parse, zone)? {
ParsedTime::Instant(micros) => DateTime::from_timestamp_micros(micros)
.map(|instant| Cell::Text(instant.to_rfc3339_opts(SecondsFormat::AutoSi, true)))
.ok_or_else(|| "a time out of range".to_owned()),
ParsedTime::Day(days) => Ok(Cell::Date(days)),
}
}
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(),
}
}