thisweek_core/
time.rs

1use chrono::{DateTime, Local};
2
3pub fn get_unix_day_from_local_datetime(datetime: DateTime<Local>) -> i32 {
4    // get the unix timestamp, add the local timezone offset, then calculate the day index
5    let utc_epoch = datetime.to_utc().timestamp(); // Seconds since Unix epoch
6    let offset_seconds = datetime.offset().local_minus_utc(); // offset seconds based on timezone
7    let epoch_with_local_offset = utc_epoch + offset_seconds as i64;
8    (epoch_with_local_offset / 3600 / 24) as i32
9}
10
11pub fn get_local_datetime_form_unix_day(day: i32) -> DateTime<Local> {
12    // the reverse operation of get_unix_day()
13    let offset_seconds = Local::now().offset().local_minus_utc();
14    let sec: i64 = day as i64 * 3600 * 24 - offset_seconds as i64;
15    let nano: u32 = 0;
16    let datetime = DateTime::from_timestamp(sec, nano).expect("this should never happen!!");
17    let datetime: DateTime<Local> = datetime.into();
18    // println!("constructed datetime: {}", datetime);
19    datetime
20}