1use crate::api::clock;
2use crate::sys;
3
4use time::{Duration, OffsetDateTime, UtcOffset};
5
6pub fn now() -> OffsetDateTime {
7 now_utc().to_offset(offset())
8}
9
10pub fn now_utc() -> OffsetDateTime {
11 let s = clock::epoch_time(); let ns = Duration::nanoseconds(
13 libm::floor(1e9 * (s - libm::floor(s))) as i64
14 );
15 OffsetDateTime::from_unix_timestamp(s as i64) + ns
16}
17
18pub fn from_timestamp(ts: i64) -> OffsetDateTime {
19 from_timestamp_utc(ts).to_offset(offset())
20}
21
22pub fn from_timestamp_utc(ts: i64) -> OffsetDateTime {
23 OffsetDateTime::from_unix_timestamp(ts)
24}
25
26fn offset() -> UtcOffset {
27 if let Some(tz) = sys::process::env("TZ") {
28 if let Ok(offset) = tz.parse::<i32>() {
29 return UtcOffset::seconds(offset);
30 }
31 }
32 UtcOffset::UTC
33}