-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtime.rs
More file actions
53 lines (44 loc) · 1.58 KB
/
time.rs
File metadata and controls
53 lines (44 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#[cfg(feature = "local-time")]
mod localtime {
use std::time::SystemTime;
use jiff::Zoned;
/// Return a string representing the current date and time as localtime.
///
/// Available with the `localtime` feature toggle.
pub fn format_now_datetime_seconds() -> String {
Zoned::now().strftime("%F %T %Z").to_string()
}
/// Return a string representing the current time as localtime.
///
/// Available with the `localtime` feature toggle.
pub fn format_time_for_messages(time: SystemTime) -> String {
Zoned::try_from(time)
.expect("system time is always in range -9999-01-01..=9999-12-31")
.strftime("%T")
.to_string()
}
}
/// An `hours:minute:seconds` format.
pub const DATE_TIME_HMS: usize = "00:51:45".len();
#[cfg(not(feature = "local-time"))]
mod utc {
use std::time::SystemTime;
use super::DATE_TIME_HMS;
/// Return a string representing the current date and time as UTC.
///
/// Available without the `localtime` feature toggle.
pub fn format_time_for_messages(time: SystemTime) -> String {
let time = jiff::Timestamp::try_from(time).expect("reasonable system time");
time.strftime("%T").to_string()
}
/// Return a string representing the current time as UTC.
///
/// Available without the `localtime` feature toggle.
pub fn format_now_datetime_seconds() -> String {
jiff::Timestamp::now().strftime("%FT%T").to_string()
}
}
#[cfg(feature = "local-time")]
pub use localtime::*;
#[cfg(not(feature = "local-time"))]
pub use utc::*;