Skip to content

Commit

Permalink
frame-source: convert to chrono
Browse files Browse the repository at this point in the history
  • Loading branch information
astraw committed Mar 16, 2024
1 parent c16fcad commit 2d59214
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 31 deletions.
1 change: 0 additions & 1 deletion media-utils/frame-source/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ mp4 = "0.14.0"
h264-reader = "0.7.0"
openh264 = "0.4.0"
pretty-hex = "0.3.0"
time = "0.1.43"

basic-frame = { path = "../../basic-frame" }
ci2-remote-control = { path = "../../ci2-remote-control" }
Expand Down
50 changes: 20 additions & 30 deletions media-utils/frame-source/src/ntp_timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,8 @@ pub(crate) struct NtpTimestamp(pub u64);

impl std::fmt::Display for NtpTimestamp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let since_epoch = self.0.wrapping_sub(UNIX_EPOCH.0);
let sec_since_epoch = (since_epoch >> 32) as u32;
let tm = time::at(time::Timespec {
sec: i64::from(sec_since_epoch),
nsec: 0,
});
let ms = ((since_epoch & 0xFFFF_FFFF) * 1_000) >> 32;
let zone_minutes = tm.tm_utcoff.abs() / 60;
write!(
f,
"{}.{:03}{}{:02}:{:02}",
tm.strftime("%FT%T").map_err(|_| std::fmt::Error)?,
ms,
if tm.tm_utcoff > 0 { '+' } else { '-' },
zone_minutes / 60,
zone_minutes % 60
)
let date_time: chrono::DateTime<chrono::Local> = (*self).into();
write!(f, "{}", date_time.format("%FT%T%.3f%:z"),)
}
}

Expand All @@ -60,7 +45,7 @@ where
let elapsed: chrono::TimeDelta = orig.to_utc() - epoch;
let sec_since_epoch: u32 = elapsed.num_seconds().try_into()?;
let nanos = elapsed.subsec_nanos();
let frac = nanos as f64 / 1e9;
let frac = f64::from(nanos) / 1e9;
let frac_int = (frac * f64::from(u32::MAX)).round() as u32;
let val = (u64::from(sec_since_epoch) << 32) + u64::from(frac_int);
Ok(NtpTimestamp(val))
Expand All @@ -76,24 +61,21 @@ where
}
}

// impl From<NtpTimestamp> for chrono::DateTime<chrono::Utc> {
// fn from(orig: NtpTimestamp) -> Self {
// // TODO: don't convert to string.
// let display = format!("{orig}");
// display.parse().unwrap()
// }
// }

impl<TZ> From<NtpTimestamp> for chrono::DateTime<TZ>
where
TZ: chrono::TimeZone,
chrono::DateTime<TZ>: From<chrono::DateTime<chrono::Utc>>,
{
fn from(orig: NtpTimestamp) -> Self {
// TODO: don't convert to string.
let display = format!("{orig}");
let dt_utc: chrono::DateTime<chrono::Utc> = display.parse().unwrap();
chrono::DateTime::from(dt_utc)
let since_epoch = orig.0.wrapping_sub(UNIX_EPOCH.0);
let sec_since_epoch = (since_epoch >> 32) as u32;
let frac_int = (since_epoch & 0xFFFF_FFFF) as u32;
let frac = frac_int as f64 / f64::from(u32::MAX);
let nanos = (frac * 1e9) as u32;
let timedelta: chrono::TimeDelta =
chrono::TimeDelta::new(i64::from(sec_since_epoch), nanos).unwrap();
let date_time = chrono::DateTime::UNIX_EPOCH + timedelta;
date_time.into()
}
}

Expand All @@ -111,6 +93,14 @@ mod tests {
assert_eq!(orig, parsed);
}

#[test]
fn test_ntp_roundtrip_raw() {
let orig: chrono::DateTime<chrono::Utc> = ORIG_STR.parse().unwrap();
let ntp_timestamp = chrono_to_ntp(orig).unwrap();
let parsed: chrono::DateTime<chrono::Utc> = ntp_timestamp.into();
assert_eq!(orig, parsed);
}

#[test]
fn test_ntp_decode() {
let orig: chrono::DateTime<chrono::Utc> = ORIG_STR.parse().unwrap();
Expand Down

0 comments on commit 2d59214

Please sign in to comment.