tetratto_shared/
time.rs

1use chrono::{TimeZone, Utc};
2use std::time::{SystemTime, UNIX_EPOCH};
3
4/// Get a [`usize`] timestamp
5pub fn unix_epoch_timestamp() -> usize {
6    let right_now = SystemTime::now();
7    let time_since = right_now
8        .duration_since(UNIX_EPOCH)
9        .expect("Time travel is not allowed");
10
11    time_since.as_millis() as usize
12}
13
14/// Get a [`i64`] timestamp from the given `year` epoch
15pub fn epoch_timestamp(year: u32) -> i64 {
16    let now = Utc::now().timestamp_millis();
17    let then = Utc
18        .with_ymd_and_hms(year as i32, 1, 1, 0, 0, 0)
19        .unwrap()
20        .timestamp_millis();
21
22    now - then
23}