hyperlane_time/time/
time.rs

1use super::r#type::from_env_var;
2use std::fmt::Write;
3use std::time::{Duration, SystemTime, UNIX_EPOCH};
4
5/// Leap Year
6pub static LEAP_YEAR: [u64; 12] = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
7/// Common Year
8pub static COMMON_YEAR: [u64; 12] = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
9/// Days
10pub static DAYS: [&str; 7] = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
11/// Months
12pub static MONTHS: [&str; 12] = [
13    "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
14];
15
16/// Determines if a year is a leap year.
17///
18/// # Parameters
19/// `u64`: The year
20///
21/// # Returns
22/// `bool`: Whether it is a leap year
23fn is_leap_year(year: u64) -> bool {
24    (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
25}
26
27/// Calculates the current year, month, day, and the number of seconds remaining in the day.
28///
29/// # Returns
30/// A tuple containing:
31/// - `year`: The current year
32/// - `month`: The current month
33/// - `day`: The current day
34/// - `remaining_seconds`: The number of seconds passed today
35fn calculate_current_date() -> (u64, u64, u64, u64) {
36    // Get the current time
37    let start: SystemTime = SystemTime::now();
38    let duration: Duration = start.duration_since(UNIX_EPOCH).unwrap();
39    let total_seconds: u64 = duration.as_secs();
40    let mut total_days: u64 = total_seconds / 86400;
41    let mut year: u64 = 1970;
42    while total_days >= if is_leap_year(year) { 366 } else { 365 } {
43        total_days -= if is_leap_year(year) { 366 } else { 365 };
44        year += 1;
45    }
46    let mut month: u64 = 1;
47    let month_days: [u64; 12] = if is_leap_year(year) {
48        LEAP_YEAR
49    } else {
50        COMMON_YEAR
51    };
52    while total_days >= month_days[month as usize - 1] {
53        total_days -= month_days[month as usize - 1];
54        month += 1;
55    }
56    let day: u64 = total_days + 1;
57    let remaining_seconds: u64 = total_seconds % 86400;
58    (year, month, day, remaining_seconds)
59}
60
61/// Gets the current time, including the date and time.
62///
63/// # Returns
64/// `String`: The formatted time as "YYYY-MM-DD HH:MM:SS"
65pub fn current_time() -> String {
66    let (year, month, day, remaining_seconds) = calculate_current_date();
67    let timezone_offset: u64 = from_env_var().value(); // Assuming from_env_var() is defined elsewhere
68    let hours: u64 = ((remaining_seconds + timezone_offset) / 3600) % 24;
69    let minutes: u64 = (remaining_seconds % 3600) / 60;
70    let seconds: u64 = remaining_seconds % 60;
71    let mut date_time: String = String::new();
72    write!(
73        &mut date_time,
74        "{:04}-{:02}-{:02} {:02}:{:02}:{:02}",
75        year, month, day, hours, minutes, seconds
76    )
77    .unwrap_or_default();
78    date_time
79}
80
81/// Gets the current day, without the time.
82///
83/// # Returns
84/// `String`: The formatted date as "YYYY-MM-DD"
85pub fn current_date() -> String {
86    let (year, month, day, _) = calculate_current_date();
87    let mut date_time: String = String::new();
88    write!(&mut date_time, "{:04}-{:02}-{:02}", year, month, day).unwrap_or_default();
89    date_time
90}
91
92/// Computes the year, month, and day from days since Unix epoch (1970-01-01).
93///
94/// - `days_since_epoch`: Number of days since `1970-01-01`.
95/// - Returns: `(year, month, day)`
96fn compute_date(mut days_since_epoch: u64) -> (u64, u64, u64) {
97    let mut year: u64 = 1970;
98    loop {
99        let days_in_year = if is_leap_year(year) { 366 } else { 365 };
100        if days_since_epoch < days_in_year {
101            break;
102        }
103        days_since_epoch -= days_in_year as u64;
104        year += 1;
105    }
106    let mut month: u64 = 0;
107    for (i, &days) in COMMON_YEAR.iter().enumerate() {
108        let days_in_month = if i == 1 && is_leap_year(year) {
109            days + 1
110        } else {
111            days
112        };
113        if days_since_epoch < days_in_month as u64 {
114            month = i as u64 + 1;
115            return (year, month, (days_since_epoch + 1) as u64);
116        }
117        days_since_epoch -= days_in_month as u64;
118    }
119
120    (year, month, 1)
121}
122
123pub fn current_date_gmt() -> String {
124    let now: SystemTime = SystemTime::now();
125    let duration_since_epoch: Duration = now.duration_since(UNIX_EPOCH).unwrap();
126    let timestamp: u64 = duration_since_epoch.as_secs();
127    let seconds_in_day: u64 = 86_400;
128    let days_since_epoch: u64 = timestamp / seconds_in_day;
129    let seconds_of_day: u64 = timestamp % seconds_in_day;
130    let hours: u64 = (seconds_of_day / 3600) as u64;
131    let minutes: u64 = ((seconds_of_day % 3600) / 60) as u64;
132    let seconds: u64 = (seconds_of_day % 60) as u64;
133    let (year, month, day) = compute_date(days_since_epoch);
134    let weekday: usize = ((days_since_epoch + 4) % 7) as usize;
135    format!(
136        "{}, {:02} {} {} {:02}:{:02}:{:02} GMT",
137        DAYS[weekday],
138        day,
139        MONTHS[month as usize - 1],
140        year,
141        hours,
142        minutes,
143        seconds
144    )
145}