Skip to content

Commit 4ba4951

Browse files
committed
replace chrono dependency with jiff
1 parent 1402225 commit 4ba4951

3 files changed

Lines changed: 115 additions & 42 deletions

File tree

Cargo.lock

Lines changed: 85 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ codegen-units = 1
3939

4040
[dependencies]
4141
async-channel = "2.5.0"
42-
chrono = "0.4.45"
4342
clap = { version = "4.6.2", features = ["derive"] }
4443
confy = "2.0.0"
4544
ctrlc = { version = "3.5.2", features = ["termination"] }
4645
dns-lookup = "3.0.1"
4746
etherparse = "0.20.3"
4847
iced = { version = "0.14.0", features = ["tokio", "svg", "advanced", "lazy", "image"] }
4948
ipnet = "2.12.0"
49+
jiff = "0.2.35"
5050
listeners = "0.6.0"
5151
maxminddb = "0.30.0"
5252
pcap = "2.4.0"

src/utils/formatted_strings.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::cmp::min;
22
use std::net::IpAddr;
33

44
use crate::utils::types::timestamp::Timestamp;
5-
use chrono::{Local, TimeZone};
5+
use jiff::tz::TimeZone;
66

77
/// Application version number (to be displayed in gui footer)
88
pub const APP_VERSION: &str = env!("CARGO_PKG_VERSION");
@@ -105,9 +105,10 @@ pub fn get_formatted_num_seconds(num_seconds: u128) -> String {
105105
pub fn get_formatted_timestamp(t: Timestamp) -> String {
106106
let date_opt = t
107107
.to_usecs()
108-
.and_then(|usecs| Local.timestamp_micros(usecs).latest());
108+
.and_then(|usecs| jiff::Timestamp::from_microsecond(usecs).ok())
109+
.map(|ts| TimeZone::system().to_datetime(ts));
109110
if let Some(date) = date_opt {
110-
date.format("%Y/%m/%d %H:%M:%S").to_string()
111+
date.strftime("%Y/%m/%d %H:%M:%S").to_string()
111112
} else {
112113
"?".to_string()
113114
}
@@ -191,6 +192,31 @@ mod tests {
191192
);
192193
}
193194

195+
#[test]
196+
fn test_get_formatted_timestamp() {
197+
// 2023/11/14 22:13:20 UTC: the rendered day is 14 or 15 depending on the system time zone
198+
let formatted = get_formatted_timestamp(Timestamp::new(1_700_000_000, 123_456));
199+
assert!(formatted.starts_with("2023/11/1"), "{formatted}");
200+
assert!(
201+
formatted.chars().enumerate().all(|(i, c)| match i {
202+
4 | 7 => c == '/',
203+
10 => c == ' ',
204+
13 | 16 => c == ':',
205+
_ => c.is_ascii_digit(),
206+
}) && formatted.len() == 19,
207+
"{formatted}"
208+
);
209+
210+
// microseconds overflow
211+
assert_eq!(get_formatted_timestamp(Timestamp::new(i64::MAX, 0)), "?");
212+
assert_eq!(get_formatted_timestamp(Timestamp::new(i64::MIN, 0)), "?");
213+
// out of the range of dates that can be represented
214+
assert_eq!(
215+
get_formatted_timestamp(Timestamp::new(1_800_000_000_000, 0)),
216+
"?"
217+
);
218+
}
219+
194220
#[cfg(windows)]
195221
#[test]
196222
fn test_logs_file_path() {

0 commit comments

Comments
 (0)