Skip to main content

satellite_datetime/earth/
unix.rs

1//! POSIX Unix time (leap seconds not included in the integer) vs elapsed SI.
2
3use crate::constants::{NS_PER_DAY, NS_PER_SEC, TAI_EPOCH_UNIX_DAYS};
4use crate::earth::CivilUtc;
5use crate::error::{Error, Result};
6use crate::instant::Instant;
7use crate::julian::unix_days_from_civil;
8
9/// POSIX seconds since 1970-01-01 00:00:00 UTC, **not** counting leap seconds.
10pub fn to_posix_seconds(instant: Instant) -> Result<i64> {
11    let c = instant.to_utc()?;
12    let days = unix_days_from_civil(c.year, c.month, c.day);
13    let mut sod = c.hour as i64 * 3600 + c.minute as i64 * 60 + c.second.min(59) as i64;
14    if c.second == 60 {
15        // POSIX typically repeats 23:59:59 or jumps; we map the leap second to the
16        // next day's 00:00:00 Unix value minus 0 (same as 2017-01-01 00:00:00).
17        sod = 86400;
18    }
19    days.checked_mul(86_400)
20        .and_then(|d| d.checked_add(sod))
21        .ok_or(Error::Overflow)
22}
23
24/// Interpret POSIX seconds as UTC civil time (no leap in the day count) and convert to TAI.
25pub fn from_posix_seconds(secs: i64) -> Result<Instant> {
26    from_posix_nanos(secs as i128 * NS_PER_SEC)
27}
28
29/// POSIX nanoseconds (Java `Instant`, JS `Date` × 1e6).
30pub fn from_posix_nanos(ns: i128) -> Result<Instant> {
31    let mut days = ns / NS_PER_DAY;
32    let mut sod = ns % NS_PER_DAY;
33    if sod < 0 {
34        days -= 1;
35        sod += NS_PER_DAY;
36    }
37    let unix_days = i64::try_from(days).map_err(|_| Error::Overflow)?;
38    let (y, m, d) = crate::julian::civil_from_unix_days(unix_days);
39    let hour = (sod / (3600 * NS_PER_SEC)) as u8;
40    let rem = sod % (3600 * NS_PER_SEC);
41    let minute = (rem / (60 * NS_PER_SEC)) as u8;
42    let rem = rem % (60 * NS_PER_SEC);
43    let second = (rem / NS_PER_SEC) as u8;
44    let nanosecond = (rem % NS_PER_SEC) as u32;
45    CivilUtc::new(y, m, d, hour, minute, second, nanosecond)?.to_instant()
46}
47
48/// True SI nanoseconds from the Unix epoch instant (1970-01-01 00:00:00 UTC) to `instant`.
49///
50/// This *does* count leap seconds. It disagrees with POSIX by the number of leaps
51/// between the epoch and the instant.
52pub fn si_nanos_since_unix_epoch(instant: Instant) -> Result<i128> {
53    let epoch = CivilUtc::new(1970, 1, 1, 0, 0, 0, 0)?.to_instant()?;
54    Ok(instant.duration_since(epoch)?.as_nanos())
55}
56
57/// TAI nanoseconds from 1958-01-01 to Unix epoch (computed via the leap table).
58#[allow(dead_code)]
59pub fn unix_epoch_tai_ns() -> Result<i128> {
60    Ok(CivilUtc::new(1970, 1, 1, 0, 0, 0, 0)?
61        .to_instant()?
62        .as_tai_nanos())
63}
64
65const _: i64 = TAI_EPOCH_UNIX_DAYS;
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70    use crate::earth::CivilUtc;
71
72    #[test]
73    fn posix_ignores_leaps() {
74        let a = CivilUtc::new(2016, 12, 31, 23, 59, 59, 0)
75            .unwrap()
76            .to_instant()
77            .unwrap();
78        let b = CivilUtc::new(2017, 1, 1, 0, 0, 0, 0)
79            .unwrap()
80            .to_instant()
81            .unwrap();
82        assert_eq!(
83            to_posix_seconds(b).unwrap() - to_posix_seconds(a).unwrap(),
84            1
85        );
86        assert_eq!(b.duration_since(a).unwrap().as_seconds(), 2);
87        let si = si_nanos_since_unix_epoch(b).unwrap() - si_nanos_since_unix_epoch(a).unwrap();
88        assert_eq!(si, 2_000_000_000);
89    }
90
91    #[test]
92    fn posix_2017() {
93        let inst = from_posix_seconds(1_483_228_800).unwrap();
94        let c = inst.to_utc().unwrap();
95        assert_eq!(c.year, 2017);
96        assert_eq!(c.month, 1);
97        assert_eq!(c.day, 1);
98    }
99}