Skip to content

Commit 4165c6a

Browse files
committed
refactor: move time formatting utils to analyze
1 parent 43b70ff commit 4165c6a

File tree

3 files changed

+34
-34
lines changed

3 files changed

+34
-34
lines changed

src/analyze.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
//! - Outage analysis
3030
//! - Store metadata (hashes, versions)
3131
32+
use chrono::{DateTime, Local};
3233
use deepsize::DeepSizeOf;
3334

34-
use crate::common::fmt_timestamp;
3535
use crate::errors::AnalysisError;
3636
use crate::records::{Check, CheckType, IpType};
3737
use crate::store::Store;
@@ -40,6 +40,16 @@ use std::fmt::{Display, Write};
4040
use std::hash::Hash;
4141
use std::os::unix::fs::MetadataExt;
4242

43+
/// Formatting rules for timestamps that are easily readable by humans.
44+
///
45+
/// ```rust
46+
/// use chrono::{DateTime, Local};
47+
/// # use netpulse::analyze::TIME_FORMAT_HUMANS;
48+
/// let datetime: DateTime<Local> = Local::now();
49+
/// println!("it is now: {}", datetime.format(TIME_FORMAT_HUMANS));
50+
/// ```
51+
pub const TIME_FORMAT_HUMANS: &str = "%Y-%m-%d %H:%M:%S %Z";
52+
4353
/// Represents a period of consecutive failed checks.
4454
///
4555
/// An outage is defined by:
@@ -150,6 +160,28 @@ pub fn analyze(store: &Store) -> Result<String, AnalysisError> {
150160
Ok(f)
151161
}
152162

163+
/// Formats a [SystemTime](std::time::SystemTime) as an easily readable timestamp for humans.
164+
///
165+
/// Works with [`std::time::SystemTime`] and [`chrono::DateTime<Local>`].
166+
///
167+
/// # Examples
168+
///
169+
/// ```rust
170+
/// # use netpulse::analyze::fmt_timestamp;
171+
/// use std::time::SystemTime;
172+
/// use chrono;
173+
/// let datetime: SystemTime = SystemTime::now();
174+
/// println!("it is now: {}", fmt_timestamp(datetime));
175+
/// let datetime: chrono::DateTime<chrono::Local> = chrono::Local::now();
176+
/// println!("it is now: {}", fmt_timestamp(datetime));
177+
/// let datetime: chrono::DateTime<chrono::Utc> = chrono::Utc::now();
178+
/// println!("it is now: {}", fmt_timestamp(datetime));
179+
/// ```
180+
pub fn fmt_timestamp(timestamp: impl Into<DateTime<Local>>) -> String {
181+
let a: chrono::DateTime<chrono::Local> = timestamp.into();
182+
format!("{}", a.format(TIME_FORMAT_HUMANS))
183+
}
184+
153185
/// Adds a section divider to the report with a title.
154186
///
155187
/// Creates a divider line of '=' characters with the title centered.

src/common.rs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,12 @@ use std::str::FromStr;
3939

4040
use crate::DAEMON_PID_FILE;
4141

42-
use chrono::{DateTime, Local};
4342
use getopts::Options;
4443
use tracing::{error, info, trace};
4544
use tracing_subscriber::FmtSubscriber;
4645

4746
/// Environment variable name for configuring log level
4847
pub const ENV_LOG_LEVEL: &str = "NETPULSE_LOG_LEVEL";
49-
/// Formatting rules for timestamps that are easily readable by humans.
50-
///
51-
/// ```rust
52-
/// use chrono::{DateTime, Local};
53-
/// # use netpulse::common::TIME_FORMAT_HUMANS;
54-
/// let datetime: DateTime<Local> = Local::now();
55-
/// println!("it is now: {}", datetime.format(TIME_FORMAT_HUMANS));
56-
/// ```
57-
pub const TIME_FORMAT_HUMANS: &str = "%Y-%m-%d %H:%M:%S %Z";
5848

5949
/// Ensures the program is running with root privileges.
6050
///
@@ -272,28 +262,6 @@ pub fn getpid() -> Option<i32> {
272262
}
273263
}
274264

275-
/// Formats a [SystemTime](std::time::SystemTime) as an easily readable timestamp for humans.
276-
///
277-
/// Works with [`std::time::SystemTime`] and [`chrono::DateTime<Local>`].
278-
///
279-
/// # Examples
280-
///
281-
/// ```rust
282-
/// # use netpulse::common::fmt_timestamp;
283-
/// use std::time::SystemTime;
284-
/// use chrono;
285-
/// let datetime: SystemTime = SystemTime::now();
286-
/// println!("it is now: {}", fmt_timestamp(datetime));
287-
/// let datetime: chrono::DateTime<chrono::Local> = chrono::Local::now();
288-
/// println!("it is now: {}", fmt_timestamp(datetime));
289-
/// let datetime: chrono::DateTime<chrono::Utc> = chrono::Utc::now();
290-
/// println!("it is now: {}", fmt_timestamp(datetime));
291-
/// ```
292-
pub fn fmt_timestamp(timestamp: impl Into<DateTime<Local>>) -> String {
293-
let a: chrono::DateTime<chrono::Local> = timestamp.into();
294-
format!("{}", a.format(TIME_FORMAT_HUMANS))
295-
}
296-
297265
/// Sets up a custom panic handler for user-friendly error reporting.
298266
///
299267
/// Should be called early in the program startup, ideally before any other operations.

src/records.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use flagset::{flags, FlagSet};
4848
use serde::{Deserialize, Serialize};
4949
use tracing::error;
5050

51-
use crate::common::fmt_timestamp;
51+
use crate::analyze::fmt_timestamp;
5252
use crate::errors::StoreError;
5353
use crate::store::Version;
5454

0 commit comments

Comments
 (0)