diff options
Diffstat (limited to 'vendor/gix-date/src/time/init.rs')
-rw-r--r-- | vendor/gix-date/src/time/init.rs | 52 |
1 files changed, 21 insertions, 31 deletions
diff --git a/vendor/gix-date/src/time/init.rs b/vendor/gix-date/src/time/init.rs index 7e4cdefc6..7df1e7aee 100644 --- a/vendor/gix-date/src/time/init.rs +++ b/vendor/gix-date/src/time/init.rs @@ -1,28 +1,26 @@ -use std::{convert::TryInto, ops::Sub}; +use std::ops::Sub; -use crate::{time::Sign, Time}; +use crate::{time::Sign, OffsetInSeconds, SecondsSinceUnixEpoch, Time}; /// Instantiation impl Time { /// Create a new instance from seconds and offset. - pub fn new(seconds_since_unix_epoch: u32, offset_in_seconds: i32) -> Self { + pub fn new(seconds: SecondsSinceUnixEpoch, offset: OffsetInSeconds) -> Self { Time { - seconds_since_unix_epoch, - offset_in_seconds, - sign: offset_in_seconds.into(), + seconds, + offset, + sign: offset.into(), } } /// Return the current time without figuring out a timezone offset pub fn now_utc() -> Self { - let seconds_since_unix_epoch = time::OffsetDateTime::now_utc() + let seconds = time::OffsetDateTime::now_utc() .sub(std::time::SystemTime::UNIX_EPOCH) - .whole_seconds() - .try_into() - .expect("this is not year 2038"); + .whole_seconds(); Self { - seconds_since_unix_epoch, - offset_in_seconds: 0, + seconds, + offset: 0, sign: Sign::Plus, } } @@ -30,18 +28,14 @@ impl Time { /// Return the current local time, or `None` if the local time wasn't available. pub fn now_local() -> Option<Self> { let now = time::OffsetDateTime::now_utc(); - let seconds_since_unix_epoch = now - .sub(std::time::SystemTime::UNIX_EPOCH) - .whole_seconds() - .try_into() - .expect("this is not year 2038"); + let seconds = now.sub(std::time::SystemTime::UNIX_EPOCH).whole_seconds(); // TODO: make this work without cfg(unsound_local_offset), see // https://github.com/time-rs/time/issues/293#issuecomment-909158529 - let offset_in_seconds = time::UtcOffset::local_offset_at(now).ok()?.whole_seconds(); + let offset = time::UtcOffset::local_offset_at(now).ok()?.whole_seconds(); Self { - seconds_since_unix_epoch, - offset_in_seconds, - sign: offset_in_seconds.into(), + seconds, + offset, + sign: offset.into(), } .into() } @@ -49,20 +43,16 @@ impl Time { /// Return the current local time, or the one at UTC if the local time wasn't available. pub fn now_local_or_utc() -> Self { let now = time::OffsetDateTime::now_utc(); - let seconds_since_unix_epoch = now - .sub(std::time::SystemTime::UNIX_EPOCH) - .whole_seconds() - .try_into() - .expect("this is not year 2038"); + let seconds = now.sub(std::time::SystemTime::UNIX_EPOCH).whole_seconds(); // TODO: make this work without cfg(unsound_local_offset), see // https://github.com/time-rs/time/issues/293#issuecomment-909158529 - let offset_in_seconds = time::UtcOffset::local_offset_at(now) - .map(|ofs| ofs.whole_seconds()) + let offset = time::UtcOffset::local_offset_at(now) + .map(time::UtcOffset::whole_seconds) .unwrap_or(0); Self { - seconds_since_unix_epoch, - offset_in_seconds, - sign: offset_in_seconds.into(), + seconds, + offset, + sign: offset.into(), } } } |