Skip to content

Commit

Permalink
use jiff for date formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
scottlamb committed Aug 20, 2024
1 parent ed81cc1 commit 4c5bd05
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
matrix:
rust:
- stable
- 1.67
- 1.70
include:
- rust: stable
extra_components: rustfmt
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## unreleased

* update minimum Rust to 1.70.
* use [`jiff`](https://crates.io/crates/jiff) for date formatting.

## `v0.4.9` (2024-08-19)

* added helpers for building `.mp4` `VisualSampleEntry` and `AudioSampleEntry`
Expand Down
27 changes: 26 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ categories = ["network-programming", "multimedia"]
description = "high-level RTSP multimedia streaming library"
repository = "https://github.com/scottlamb/retina"
include = ["src/**/*", "benches", "Cargo.toml"]
rust-version = "1.67"
rust-version = "1.70"

[package.metadata.docs.rs]
# https://docs.rs/about/metadata
Expand All @@ -29,6 +29,7 @@ futures = "0.3.14"
h264-reader = "0.7.0"
hex = "0.4.3"
http-auth = "0.1.2"
jiff = "0.1.8"
log = "0.4.8"
once_cell = "1.7.2"
pin-project = "1.0.7"
Expand All @@ -38,7 +39,6 @@ rtsp-types = "0.1.0"
sdp-types = "0.1.4"
smallvec = { version = "1.6.1", features = ["union"] }
thiserror = "1.0.25"
time = "0.1.43"
tokio = { version = "1.11.0", features = ["macros", "net", "rt", "time"] }
tokio-util = { version = "0.7.3", features = ["codec"] }
url = "2.2.1"
Expand Down
31 changes: 8 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,21 +204,11 @@ 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 ns = i32::try_from(((since_epoch & 0xFFFF_FFFF) * 1_000_000_000) >> 32)
.expect("should be < 1_000_000_000");
let tm = jiff::Timestamp::new(i64::from(sec_since_epoch), ns)
.expect("u32 sec should be valid Timestamp");
std::fmt::Display::fmt(&tm, f)
}
}

Expand All @@ -233,22 +223,17 @@ impl std::fmt::Debug for NtpTimestamp {
///
/// Currently this just allows formatting via `Debug` and `Display`.
#[derive(Copy, Clone, Debug)]
pub struct WallTime(time::Timespec);
pub struct WallTime(jiff::Timestamp);

impl WallTime {
fn now() -> Self {
Self(time::get_time())
Self(jiff::Timestamp::now())
}
}

impl Display for WallTime {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(
&time::at(self.0)
.strftime("%FT%T")
.map_err(|_| std::fmt::Error)?,
f,
)
std::fmt::Display::fmt(&self.0, f)
}
}

Expand Down

0 comments on commit 4c5bd05

Please sign in to comment.