dev_tool/
date_util.rs

1// 日期工具
2use chrono::{DateTime, Local, Duration, Utc};
3use std::time::{SystemTime, UNIX_EPOCH};
4
5pub struct DateUtil;
6
7impl DateUtil {
8
9    pub fn current_time_string() -> String {
10        let now = Local::now();
11        now.format("%Y-%m-%d %H:%M:%S").to_string()
12    }
13
14    pub fn current_timestamp() -> u64 {
15        let time = Local::now();
16        DateUtil::timestamp(time)
17    }
18
19    pub fn timestamp(time: DateTime<Local>) -> u64 {
20        let system_time: SystemTime = time.into();
21        let duration = system_time.duration_since(UNIX_EPOCH).unwrap();
22        let timestamp = duration.as_secs();
23        timestamp
24    }
25
26    pub fn timestamp_add(time: DateTime<Local>, seconds: i64) -> u64 {
27        let diff = Duration::seconds(seconds);
28        let time = time + diff;
29        DateUtil::timestamp(time)
30    }
31
32}
33
34#[cfg(test)]
35mod tests {
36
37    #[test]
38    fn it_works() {
39        // TODO 测试使用
40    }
41
42}