use std::ops::Sub;
use crate::{time::Sign, OffsetInSeconds, SecondsSinceUnixEpoch, Time};
impl Time {
pub fn new(seconds: SecondsSinceUnixEpoch, offset: OffsetInSeconds) -> Self {
Time {
seconds,
offset,
sign: offset.into(),
}
}
pub fn now_utc() -> Self {
let seconds = time::OffsetDateTime::now_utc()
.sub(std::time::SystemTime::UNIX_EPOCH)
.whole_seconds();
Self {
seconds,
offset: 0,
sign: Sign::Plus,
}
}
pub fn now_local() -> Option<Self> {
let now = time::OffsetDateTime::now_utc();
let seconds = now.sub(std::time::SystemTime::UNIX_EPOCH).whole_seconds();
let offset = time::UtcOffset::local_offset_at(now).ok()?.whole_seconds();
Self {
seconds,
offset,
sign: offset.into(),
}
.into()
}
pub fn now_local_or_utc() -> Self {
let now = time::OffsetDateTime::now_utc();
let seconds = now.sub(std::time::SystemTime::UNIX_EPOCH).whole_seconds();
let offset = time::UtcOffset::local_offset_at(now)
.map(time::UtcOffset::whole_seconds)
.unwrap_or(0);
Self {
seconds,
offset,
sign: offset.into(),
}
}
}