From 620fa7dfb44d05a1683178c32a45c0c839ac8a66 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Mon, 19 Aug 2024 16:09:55 +0200 Subject: [PATCH 1/3] refactor: drop time crate in favor of std::time::SystemTime --- neqo-common/Cargo.toml | 1 - neqo-common/src/qlog.rs | 11 +++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/neqo-common/Cargo.toml b/neqo-common/Cargo.toml index 2832bd2521..9acb60e7cb 100644 --- a/neqo-common/Cargo.toml +++ b/neqo-common/Cargo.toml @@ -22,7 +22,6 @@ env_logger = { version = "0.10", default-features = false } hex = { version = "0.4", default-features = false, features = ["alloc"], optional = true } log = { workspace = true } qlog = { workspace = true } -time = { version = "0.3", default-features = false, features = ["formatting"] } [dev-dependencies] test-fixture = { path = "../test-fixture" } diff --git a/neqo-common/src/qlog.rs b/neqo-common/src/qlog.rs index 601fdfc65f..b4bc1ceb46 100644 --- a/neqo-common/src/qlog.rs +++ b/neqo-common/src/qlog.rs @@ -11,6 +11,7 @@ use std::{ io::BufWriter, path::PathBuf, rc::Rc, + time::SystemTime, }; use qlog::{ @@ -152,6 +153,7 @@ impl Drop for NeqoQlogShared { } } +#[allow(clippy::missing_panics_doc)] #[must_use] pub fn new_trace(role: Role) -> qlog::TraceSeq { TraceSeq { @@ -173,11 +175,16 @@ pub fn new_trace(role: Role) -> qlog::TraceSeq { group_id: None, protocol_type: None, reference_time: { - // It is better to allow this than deal with a conversion from i64 to f64. + // It is better to allow this than deal with a conversion from u128 to f64. // We can't do the obvious two-step conversion with f64::from(i32::try_from(...)), // because that overflows earlier than is ideal. This should be fine for a while. #[allow(clippy::cast_precision_loss)] - Some((time::OffsetDateTime::now_utc().unix_timestamp() * 1000) as f64) + Some( + SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .expect("expect UNIX_EPOCH to always be earlier than now") + .as_millis() as f64, + ) }, time_format: Some("relative".to_string()), }), From 187f635828ffea0c715d97aabdd609f6cb16b361 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Tue, 20 Aug 2024 10:12:41 +0200 Subject: [PATCH 2/3] use as_secs_f64 --- neqo-common/src/qlog.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/neqo-common/src/qlog.rs b/neqo-common/src/qlog.rs index b4bc1ceb46..99d909c92f 100644 --- a/neqo-common/src/qlog.rs +++ b/neqo-common/src/qlog.rs @@ -175,15 +175,12 @@ pub fn new_trace(role: Role) -> qlog::TraceSeq { group_id: None, protocol_type: None, reference_time: { - // It is better to allow this than deal with a conversion from u128 to f64. - // We can't do the obvious two-step conversion with f64::from(i32::try_from(...)), - // because that overflows earlier than is ideal. This should be fine for a while. - #[allow(clippy::cast_precision_loss)] Some( SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .expect("expect UNIX_EPOCH to always be earlier than now") - .as_millis() as f64, + .as_secs_f64() + * 1_000.0, ) }, time_format: Some("relative".to_string()), From cca50a19bdcd781b4501b9986a59d54ac6fec172 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Tue, 20 Aug 2024 10:17:01 +0200 Subject: [PATCH 3/3] don't panic, but return None --- neqo-common/src/qlog.rs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/neqo-common/src/qlog.rs b/neqo-common/src/qlog.rs index 99d909c92f..04651b587b 100644 --- a/neqo-common/src/qlog.rs +++ b/neqo-common/src/qlog.rs @@ -153,7 +153,6 @@ impl Drop for NeqoQlogShared { } } -#[allow(clippy::missing_panics_doc)] #[must_use] pub fn new_trace(role: Role) -> qlog::TraceSeq { TraceSeq { @@ -174,15 +173,10 @@ pub fn new_trace(role: Role) -> qlog::TraceSeq { common_fields: Some(CommonFields { group_id: None, protocol_type: None, - reference_time: { - Some( - SystemTime::now() - .duration_since(SystemTime::UNIX_EPOCH) - .expect("expect UNIX_EPOCH to always be earlier than now") - .as_secs_f64() - * 1_000.0, - ) - }, + reference_time: SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .map(|d| d.as_secs_f64() * 1_000.0) + .ok(), time_format: Some("relative".to_string()), }), }