#[cfg(test)]
use std::time::Duration;
use std::time::SystemTime;
use time::{OffsetDateTime, Time, UtcOffset};

// #[cfg(test)]
// pub(crate) fn current_time_millis() -> u64 {
//     return SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or(Duration::ZERO).as_millis() as u64;
// }

#[cfg(test)]
pub(crate) fn start_of_day_millis() -> u64 {
    time_to_millis(start_of_day_time())
}

pub(crate) fn start_of_day_time() -> SystemTime {
    let offset = UtcOffset::current_local_offset().unwrap_or(UtcOffset::UTC);
    let local_now = OffsetDateTime::now_utc().to_offset(offset);
    let local_midnight = local_now.date().with_time(Time::MIDNIGHT).assume_offset(offset);
    SystemTime::from(local_midnight)
}

pub(crate) fn time_to_millis(time: SystemTime) -> u64 {
    return time.duration_since(SystemTime::UNIX_EPOCH).map_or(0, |dur| dur.as_millis() as u64);
}

#[cfg(test)]
pub fn millis_to_time(millis: u64) -> SystemTime {
    return SystemTime::UNIX_EPOCH + Duration::from_millis(millis);
}

#[cfg(test)]
pub(crate) fn millis_to_time_opt(millis: u64) -> Option<SystemTime> {
    Some(SystemTime::UNIX_EPOCH + Duration::from_millis(millis))
}
