gix_date/time/
init.rs

1use crate::{time::Sign, 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 {
8            seconds,
9            offset,
10            sign: offset.into(),
11        }
12    }
13
14    /// Return the current time without figuring out a timezone offset
15    pub fn now_utc() -> Self {
16        let seconds = jiff::Timestamp::now().as_second();
17        Self {
18            seconds,
19            offset: 0,
20            sign: Sign::Plus,
21        }
22    }
23
24    /// Return the current local time, or `None` if the local time wasn't available.
25    pub fn now_local() -> Option<Self> {
26        Some(Self::now_local_or_utc())
27    }
28
29    /// Return the current local time, or the one at UTC if the local time wasn't available.
30    pub fn now_local_or_utc() -> Self {
31        let zdt = jiff::Zoned::now();
32        let seconds = zdt.timestamp().as_second();
33        let offset = zdt.offset().seconds();
34        Self {
35            seconds,
36            offset,
37            sign: offset.into(),
38        }
39    }
40}