forked from nacos-group/r-nacos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatetime_utils.rs
More file actions
35 lines (28 loc) · 888 Bytes
/
datetime_utils.rs
File metadata and controls
35 lines (28 loc) · 888 Bytes
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
use chrono::{DateTime, FixedOffset, Utc};
use std::time::SystemTime;
const DATETIME_TIMESTAMP_FMT: &str = "%Y-%m-%dT%H:%M:%S%.3f%:z";
/// Returns the duration since UNIX_EPOCH. Panics only if system clock is before epoch.
#[inline]
fn since_epoch() -> std::time::Duration {
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("system clock is before UNIX epoch")
}
pub fn now_millis() -> u64 {
since_epoch().as_millis() as u64
}
pub fn now_millis_i64() -> i64 {
since_epoch().as_millis() as i64
}
pub fn now_second_i32() -> i32 {
since_epoch().as_secs() as i32
}
pub fn now_second_u32() -> u32 {
since_epoch().as_secs() as u32
}
pub fn get_now_timestamp_str(offset: &FixedOffset) -> String {
DateTime::<Utc>::from(SystemTime::now())
.with_timezone(offset)
.format(DATETIME_TIMESTAMP_FMT)
.to_string()
}