Skip to content

Instantly share code, notes, and snippets.

@marnunez
Last active December 22, 2021 18:33
Show Gist options
  • Save marnunez/9bda69d6c5e3f1940ec1aa3be70540f2 to your computer and use it in GitHub Desktop.
Save marnunez/9bda69d6c5e3f1940ec1aa3be70540f2 to your computer and use it in GitHub Desktop.
use chrono::NaiveDateTime;
use deku::{
bitvec::{BitSlice, BitVec, BitView, Msb0},
prelude::*,
};
#[derive(Debug)]
pub struct TimeT(NaiveDateTime);
impl<'a> DekuRead<'a> for TimeT {
fn read(
input: &'a BitSlice<Msb0, u8>,
_ctx: (),
) -> Result<(&'a BitSlice<Msb0, u8>, Self), DekuError>
where
Self: Sized,
{
let (rest, time) = u32::read(input, ()).map_err(|e| DekuError::from(e))?;
let time = Local.from_utc_datetime(&NaiveDateTime::from_timestamp(time as i64, 0));
Ok((rest, TimeT(time)))
}
}
impl<'a> DekuWrite for TimeT {
fn write(&self, output: &mut BitVec<Msb0, u8>, _ctx: ()) -> Result<(), DekuError> {
Ok(BitVec::extend(
output,
(self.0.timestamp() as u32)
.to_le_bytes()
.view_bits::<Msb0>(),
))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment