1pub fn format_timestamp(seconds: i64) -> String {
3 let days_since_epoch = seconds / 86400;
4 let time_of_day = seconds % 86400;
5 let hours = time_of_day / 3600;
6 let minutes = (time_of_day % 3600) / 60;
7 let secs = time_of_day % 60;
8
9 let (year, month, day) = days_to_date(days_since_epoch);
10
11 format!(
12 "{:04}-{:02}-{:02} {:02}:{:02}:{:02} UTC",
13 year, month, day, hours, minutes, secs
14 )
15}
16
17pub fn days_to_date(days: i64) -> (i64, u32, u32) {
21 let z = days + 719468;
22 let era = (if z >= 0 { z } else { z - 146096 }) / 146097;
23 let doe = (z - era * 146097) as u32;
24 let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
25 let y = yoe as i64 + era * 400;
26 let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
27 let mp = (5 * doy + 2) / 153;
28 let d = doy - (153 * mp + 2) / 5 + 1;
29 let m = if mp < 10 { mp + 3 } else { mp - 9 };
30 let y = if m <= 2 { y + 1 } else { y };
31 (y, m, d)
32}
33
34#[cfg(test)]
35mod tests {
36 use super::*;
37
38 #[test]
39 fn format_timestamp_epoch() {
40 assert_eq!(format_timestamp(0), "1970-01-01 00:00:00 UTC");
41 }
42
43 #[test]
44 fn format_timestamp_known_date() {
45 assert_eq!(format_timestamp(1700000000), "2023-11-14 22:13:20 UTC");
47 }
48
49 #[test]
50 fn format_timestamp_with_time_components() {
51 assert_eq!(format_timestamp(3661), "1970-01-01 01:01:01 UTC");
53 }
54
55 #[test]
56 fn format_timestamp_end_of_day() {
57 assert_eq!(format_timestamp(86399), "1970-01-01 23:59:59 UTC");
59 }
60
61 #[test]
62 fn days_to_date_epoch() {
63 assert_eq!(days_to_date(0), (1970, 1, 1));
64 }
65
66 #[test]
67 fn days_to_date_leap_year() {
68 assert_eq!(days_to_date(11016), (2000, 2, 29));
70 }
71
72 #[test]
73 fn days_to_date_end_of_month() {
74 assert_eq!(days_to_date(30), (1970, 1, 31));
76 }
77
78 #[test]
79 fn days_to_date_pre_epoch() {
80 assert_eq!(days_to_date(-1), (1969, 12, 31));
82 }
83
84 #[test]
85 fn days_to_date_year_2024() {
86 assert_eq!(days_to_date(19723), (2024, 1, 1));
88 }
89}