junobuild_shared/
date.rs

1use crate::types::utils::CalendarDate;
2use time::{Duration, OffsetDateTime};
3
4/// Converts a Unix timestamp (in nanoseconds) to an `OffsetDateTime`.
5///
6/// # Arguments
7/// - `timestamp`: A reference to a `u64` Unix timestamp in nanoseconds.
8///
9/// # Returns
10/// An `OffsetDateTime` representing the given timestamp.
11///
12/// # Panics
13/// Panics if the conversion to a timestamp fails, which can happen if the value
14/// represents a time outside the valid range that `OffsetDateTime` can represent.
15fn to_date(timestamp: &u64) -> OffsetDateTime {
16    let nanoseconds = *timestamp as i64;
17    let seconds = nanoseconds / 1_000_000_000;
18    let nanos_remainder = nanoseconds % 1_000_000_000;
19
20    OffsetDateTime::from_unix_timestamp(seconds).unwrap() + Duration::nanoseconds(nanos_remainder)
21}
22
23/// Converts a Unix timestamp (in nanoseconds) to a `CalendarDate`.
24///
25/// # Arguments
26/// - `timestamp`: A reference to a `u64` Unix timestamp in nanoseconds.
27///
28/// # Returns
29/// A `CalendarDate` representing the date of the given timestamp.
30pub fn calendar_date(timestamp: &u64) -> CalendarDate {
31    CalendarDate::from(&to_date(timestamp).to_calendar_date())
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37    use time::format_description::well_known::Rfc3339;
38    use time::OffsetDateTime;
39
40    fn ts_ns(rfc3339: &str) -> u64 {
41        OffsetDateTime::parse(rfc3339, &Rfc3339)
42            .unwrap()
43            .unix_timestamp_nanos()
44            .try_into()
45            .unwrap()
46    }
47
48    #[test]
49    fn calendar_date_is_same_within_day() {
50        // Same calendar day, different times → same CalendarDate
51        let morning = ts_ns("2025-10-21T00:00:00Z");
52        let night = ts_ns("2025-10-21T23:59:59Z");
53
54        let a = calendar_date(&morning);
55        let b = calendar_date(&night);
56
57        assert_eq!(a, b);
58    }
59}