diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 18:31:44 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 18:31:44 +0000 |
commit | c23a457e72abe608715ac76f076f47dc42af07a5 (patch) | |
tree | 2772049aaf84b5c9d0ed12ec8d86812f7a7904b6 /vendor/gix-date/src/time | |
parent | Releasing progress-linux version 1.73.0+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-c23a457e72abe608715ac76f076f47dc42af07a5.tar.xz rustc-c23a457e72abe608715ac76f076f47dc42af07a5.zip |
Merging upstream version 1.74.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/gix-date/src/time')
-rw-r--r-- | vendor/gix-date/src/time/format.rs | 12 | ||||
-rw-r--r-- | vendor/gix-date/src/time/init.rs | 52 | ||||
-rw-r--r-- | vendor/gix-date/src/time/mod.rs | 9 | ||||
-rw-r--r-- | vendor/gix-date/src/time/write.rs | 89 |
4 files changed, 103 insertions, 59 deletions
diff --git a/vendor/gix-date/src/time/format.rs b/vendor/gix-date/src/time/format.rs index 37cc12bd2..639ed58fa 100644 --- a/vendor/gix-date/src/time/format.rs +++ b/vendor/gix-date/src/time/format.rs @@ -68,12 +68,16 @@ impl Time { /// Use the [`format_description`](https://time-rs.github.io/book/api/format-description.html) macro to create and /// validate formats at compile time, courtesy of the [`time`] crate. pub fn format<'a>(&self, format: impl Into<Format<'a>>) -> String { - match format.into() { + self.format_inner(format.into()) + } + + fn format_inner(&self, format: Format<'_>) -> String { + match format { Format::Custom(format) => self .to_time() .format(&format) .expect("well-known format into memory never fails"), - Format::Unix => self.seconds_since_unix_epoch.to_string(), + Format::Unix => self.seconds.to_string(), Format::Raw => self.to_bstring().to_string(), } } @@ -81,8 +85,8 @@ impl Time { impl Time { fn to_time(self) -> time::OffsetDateTime { - time::OffsetDateTime::from_unix_timestamp(self.seconds_since_unix_epoch as i64) + time::OffsetDateTime::from_unix_timestamp(self.seconds) .expect("always valid unix time") - .to_offset(time::UtcOffset::from_whole_seconds(self.offset_in_seconds).expect("valid offset")) + .to_offset(time::UtcOffset::from_whole_seconds(self.offset).expect("valid offset")) } } 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(), } } } diff --git a/vendor/gix-date/src/time/mod.rs b/vendor/gix-date/src/time/mod.rs index 612a3cdef..22c4e42f0 100644 --- a/vendor/gix-date/src/time/mod.rs +++ b/vendor/gix-date/src/time/mod.rs @@ -6,11 +6,6 @@ impl Time { pub fn is_set(&self) -> bool { *self != Self::default() } - - /// Return the passed seconds since epoch since this signature was made. - pub fn seconds(&self) -> u32 { - self.seconds_since_unix_epoch - } } /// Indicates if a number is positive or negative for use in [`Time`]. @@ -58,8 +53,8 @@ mod impls { impl Default for Time { fn default() -> Self { Time { - seconds_since_unix_epoch: 0, - offset_in_seconds: 0, + seconds: 0, + offset: 0, sign: Sign::Plus, } } diff --git a/vendor/gix-date/src/time/write.rs b/vendor/gix-date/src/time/write.rs index d4d017bd3..cef8e4037 100644 --- a/vendor/gix-date/src/time/write.rs +++ b/vendor/gix-date/src/time/write.rs @@ -12,9 +12,9 @@ impl Time { } /// Serialize this instance to `out` in a format suitable for use in header fields of serialized git commits or tags. - pub fn write_to(&self, mut out: impl std::io::Write) -> std::io::Result<()> { + pub fn write_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()> { let mut itoa = itoa::Buffer::new(); - out.write_all(itoa.format(self.seconds_since_unix_epoch).as_bytes())?; + out.write_all(itoa.format(self.seconds).as_bytes())?; out.write_all(b" ")?; out.write_all(match self.sign { Sign::Plus => b"+", @@ -24,7 +24,7 @@ impl Time { const ZERO: &[u8; 1] = b"0"; const SECONDS_PER_HOUR: i32 = 60 * 60; - let offset = self.offset_in_seconds.abs(); + let offset = self.offset.abs(); let hours = offset / SECONDS_PER_HOUR; assert!(hours < 25, "offset is more than a day: {hours}"); let minutes = (offset - (hours * SECONDS_PER_HOUR)) / 60; @@ -40,30 +40,85 @@ impl Time { out.write_all(itoa.format(minutes).as_bytes()).map(|_| ()) } - /// Computes the number of bytes necessary to render this time. + /// Computes the number of bytes necessary to write it using [`Time::write_to()`]. pub fn size(&self) -> usize { - // TODO: this is not year 2038 safeā¦but we also can't parse larger numbers (or represent them) anyway. It's a trap nonetheless - // that can be fixed by increasing the size to usize. - (if self.seconds_since_unix_epoch >= 1_000_000_000 { + (if self.seconds >= 1_000_000_000_000_000_000 { + 19 + } else if self.seconds >= 100_000_000_000_000_000 { + 18 + } else if self.seconds >= 10_000_000_000_000_000 { + 17 + } else if self.seconds >= 1_000_000_000_000_000 { + 16 + } else if self.seconds >= 100_000_000_000_000 { + 15 + } else if self.seconds >= 10_000_000_000_000 { + 14 + } else if self.seconds >= 1_000_000_000_000 { + 13 + } else if self.seconds >= 100_000_000_000 { + 12 + } else if self.seconds >= 10_000_000_000 { + 11 + } else if self.seconds >= 1_000_000_000 { 10 - } else if self.seconds_since_unix_epoch >= 100_000_000 { + } else if self.seconds >= 100_000_000 { 9 - } else if self.seconds_since_unix_epoch >= 10_000_000 { + } else if self.seconds >= 10_000_000 { 8 - } else if self.seconds_since_unix_epoch >= 1_000_000 { + } else if self.seconds >= 1_000_000 { 7 - } else if self.seconds_since_unix_epoch >= 100_000 { + } else if self.seconds >= 100_000 { 6 - } else if self.seconds_since_unix_epoch >= 10_000 { + } else if self.seconds >= 10_000 { 5 - } else if self.seconds_since_unix_epoch >= 1_000 { + } else if self.seconds >= 1_000 { 4 - } else if self.seconds_since_unix_epoch >= 100 { + } else if self.seconds >= 100 { 3 - } else if self.seconds_since_unix_epoch >= 10 { + } else if self.seconds >= 10 { 2 - } else { + } else if self.seconds >= 0 { 1 - }) + 2 /*space + sign*/ + 2 /*hours*/ + 2 /*minutes*/ + // from here, it's sign + num-digits characters + } else if self.seconds >= -10 { + 2 + } else if self.seconds >= -100 { + 3 + } else if self.seconds >= -1_000 { + 4 + } else if self.seconds >= -10_000 { + 5 + } else if self.seconds >= -100_000 { + 6 + } else if self.seconds >= -1_000_000 { + 7 + } else if self.seconds >= -10_000_000 { + 8 + } else if self.seconds >= -100_000_000 { + 9 + } else if self.seconds >= -1_000_000_000 { + 10 + } else if self.seconds >= -10_000_000_000 { + 11 + } else if self.seconds >= -100_000_000_000 { + 12 + } else if self.seconds >= -1_000_000_000_000 { + 13 + } else if self.seconds >= -10_000_000_000_000 { + 14 + } else if self.seconds >= -100_000_000_000_000 { + 15 + } else if self.seconds >= -1_000_000_000_000_000 { + 16 + } else if self.seconds >= -10_000_000_000_000_000 { + 17 + } else if self.seconds >= -100_000_000_000_000_000 { + 18 + } else if self.seconds >= -1_000_000_000_000_000_000 { + 19 + } else { + 20 + }) + 2 /*space + offset sign*/ + 2 /*offset hours*/ + 2 /*offset minutes*/ } } |