use ironcalc_base::{cell::CellValue, Model};
use crate::util::get_metadata_sheet_index;
const UNIX_EPOCH_SERIAL: f64 = 25569.0;
const MILLISECONDS_PER_DAY: f64 = 86_400_000.0;
pub fn get_metadata_timestamp(model: &Model) -> Option<i64> {
let sheet_index = get_metadata_sheet_index(model)?;
for row in 1..=32 {
if let Ok(CellValue::String(label)) = model.get_cell_value_by_index(sheet_index, row, 1) {
if label == "NOW" {
if let Ok(CellValue::Number(serial)) =
model.get_cell_value_by_index(sheet_index, row, 2)
{
let milliseconds = (serial - UNIX_EPOCH_SERIAL) * MILLISECONDS_PER_DAY;
return Some(milliseconds.round() as i64);
}
return None;
}
}
}
None
}
pub fn set_mock_time_from_metadata(model: &Model) {
#[allow(clippy::unwrap_used)]
let milliseconds = get_metadata_timestamp(model).unwrap_or_else(|| {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_millis() as i64
});
ironcalc_base::mock_time::set_mock_time(milliseconds);
}