use chrono::{
offset::{LocalResult, TimeZone, Utc},
DateTime, Timelike,
};
use ownable::{IntoOwned, ToOwned};
use std::fmt;
use winnow::{
binary::{le_u16, le_u64},
seq, PResult, Parser, Partial,
};
#[derive(Clone, Copy, Eq, PartialEq, IntoOwned, ToOwned)]
pub struct MsdosTimestamp {
pub time: u16,
pub date: u16,
}
impl fmt::Debug for MsdosTimestamp {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.to_datetime() {
Some(dt) => write!(f, "MsdosTimestamp({})", dt),
None => write!(f, "MsdosTimestamp(?)"),
}
}
}
impl MsdosTimestamp {
pub fn parser(i: &mut Partial<&'_ [u8]>) -> PResult<Self> {
seq! {Self {
time: le_u16,
date: le_u16,
}}
.parse_next(i)
}
pub fn to_datetime(&self) -> Option<DateTime<Utc>> {
let res = {
let d = (self.date & 0b1_1111) as u32;
let m = ((self.date >> 5) & 0b1111) as u32;
let y = ((self.date >> 9) + 1980) as i32;
Utc.with_ymd_and_hms(y, m, d, 0, 0, 0)
};
let date = match res {
LocalResult::Single(date) => date,
_ => return None,
};
let s = (self.time & 0b1_1111) as u32 * 2;
let m = (self.time >> 5 & 0b11_1111) as u32;
let h = (self.time >> 11) as u32;
date.with_hour(h)?.with_minute(m)?.with_second(s)
}
}
#[derive(Clone, Copy, Eq, PartialEq)]
pub struct NtfsTimestamp {
pub timestamp: u64,
}
impl fmt::Debug for NtfsTimestamp {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.to_datetime() {
Some(dt) => write!(f, "NtfsTimestamp({})", dt),
None => write!(f, "NtfsTimestamp(?)"),
}
}
}
impl NtfsTimestamp {
pub fn parser(i: &mut Partial<&'_ [u8]>) -> PResult<Self> {
le_u64.map(|timestamp| Self { timestamp }).parse_next(i)
}
pub fn to_datetime(&self) -> Option<DateTime<Utc>> {
let ticks_per_second = 10_000_000;
let secs = (self.timestamp / ticks_per_second) as i64;
let nsecs = ((self.timestamp % ticks_per_second) * 100) as u32;
let epoch = Utc.with_ymd_and_hms(1601, 1, 1, 0, 0, 0).single()?;
match Utc.timestamp_opt(epoch.timestamp() + secs, nsecs) {
LocalResult::Single(date) => Some(date),
_ => None,
}
}
}
pub(crate) fn zero_datetime() -> chrono::DateTime<chrono::offset::Utc> {
chrono::DateTime::<Utc>::from_timestamp_millis(0).unwrap()
}