gix_date/time/
init.rs

1use crate::{OffsetInSeconds, SecondsSinceUnixEpoch, Time};
2
3/// Instantiation
4impl Time {
5    /// Create a new instance from seconds and offset.
6    pub fn new(seconds: SecondsSinceUnixEpoch, offset: OffsetInSeconds) -> Self {
7        Time { seconds, offset }
8    }
9
10    /// Return the current time without figuring out a timezone offset
11    pub fn now_utc() -> Self {
12        let seconds = jiff::Timestamp::now().as_second();
13        Self { seconds, offset: 0 }
14    }
15
16    /// Return the current local time, or `None` if the local time wasn't available.
17    pub fn now_local() -> Option<Self> {
18        Some(Self::now_local_or_utc())
19    }
20
21    /// Return the current local time, or the one at UTC if the local time wasn't available.
22    pub fn now_local_or_utc() -> Self {
23        let zdt = jiff::Zoned::now();
24        let seconds = zdt.timestamp().as_second();
25        let offset = zdt.offset().seconds();
26        Self { seconds, offset }
27    }
28}