debot_utils/
datetime_utils.rs

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
54
55
56
57
use chrono::{DateTime, Datelike, FixedOffset, Utc};
use std::{env, time::SystemTime};

pub struct DateTimeUtils {}

pub trait ToDateTimeString {
    fn to_datetime_string(self) -> String;
}

impl ToDateTimeString for i64 {
    fn to_datetime_string(self) -> String {
        let datetime = DateTime::<Utc>::from_timestamp(self, 0).expect("Invalid timestamp");
        datetime.format("%Y-%m-%d %H:%M:%S").to_string()
    }
}

impl ToDateTimeString for SystemTime {
    fn to_datetime_string(self) -> String {
        let datetime: DateTime<Utc> = self.into();
        datetime.format("%Y-%m-%d %H:%M:%S").to_string()
    }
}

impl DateTimeUtils {
    pub fn get_current_datetime_string() -> String {
        let current_time = Utc::now().timestamp();
        let datetime = DateTime::<Utc>::from_timestamp(current_time, 0).expect("Invalid timestamp");
        datetime.format("%Y-%m-%d %H:%M:%S").to_string()
    }

    pub fn get_current_date_string() -> String {
        let current_time = Utc::now().timestamp();
        let datetime = DateTime::<Utc>::from_timestamp(current_time, 0).expect("Invalid timestamp");
        datetime.format("%Y-%m-%d").to_string()
    }
}

pub fn get_local_time() -> (i64, String) {
    let offset_seconds = env::var("TIMEZONE_OFFSET")
        .unwrap_or_else(|_| "3600".to_string())
        .parse::<i32>()
        .expect("Invalid TIMEZONE_OFFSET");

    let offset = FixedOffset::east_opt(offset_seconds).expect("Invalid offset");

    let utc_now: DateTime<Utc> = Utc::now();
    let local_now = utc_now.with_timezone(&offset);
    (
        local_now.timestamp(),
        local_now.format("%Y-%m-%dT%H:%M:%S%z").to_string(),
    )
}

pub fn is_sunday() -> bool {
    let current_date = Utc::now();
    current_date.weekday().num_days_from_sunday() == 0
}