29
29
//! - Outage analysis
30
30
//! - Store metadata (hashes, versions)
31
31
32
+ use chrono:: { DateTime , Local } ;
32
33
use deepsize:: DeepSizeOf ;
33
34
34
- use crate :: common:: fmt_timestamp;
35
35
use crate :: errors:: AnalysisError ;
36
36
use crate :: records:: { Check , CheckType , IpType } ;
37
37
use crate :: store:: Store ;
@@ -40,6 +40,16 @@ use std::fmt::{Display, Write};
40
40
use std:: hash:: Hash ;
41
41
use std:: os:: unix:: fs:: MetadataExt ;
42
42
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
+
43
53
/// Represents a period of consecutive failed checks.
44
54
///
45
55
/// An outage is defined by:
@@ -150,6 +160,28 @@ pub fn analyze(store: &Store) -> Result<String, AnalysisError> {
150
160
Ok ( f)
151
161
}
152
162
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
+
153
185
/// Adds a section divider to the report with a title.
154
186
///
155
187
/// Creates a divider line of '=' characters with the title centered.
0 commit comments