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 | |
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')
-rw-r--r-- | vendor/gix-date/src/lib.rs | 16 | ||||
-rw-r--r-- | vendor/gix-date/src/parse.rs | 39 | ||||
-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 |
6 files changed, 131 insertions, 86 deletions
diff --git a/vendor/gix-date/src/lib.rs b/vendor/gix-date/src/lib.rs index 736f5e598..d71283f45 100644 --- a/vendor/gix-date/src/lib.rs +++ b/vendor/gix-date/src/lib.rs @@ -22,9 +22,21 @@ pub use parse::function::parse; #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Time { /// time in seconds since epoch. - pub seconds_since_unix_epoch: u32, + pub seconds: SecondsSinceUnixEpoch, /// time offset in seconds, may be negative to match the `sign` field. - pub offset_in_seconds: i32, + pub offset: OffsetInSeconds, /// the sign of `offset`, used to encode `-0000` which would otherwise loose sign information. pub sign: time::Sign, } + +/// The amount of seconds since unix epoch. +/// +/// Note that negative dates represent times before the unix epoch. +/// +/// ### Deviation +/// +/// `git` only supports dates *from* the UNIX epoch, whereas we chose to be more flexible at the expense of stopping time +/// a few million years before the heat-death of the universe. +pub type SecondsSinceUnixEpoch = i64; +/// time offset in seconds. +pub type OffsetInSeconds = i32; diff --git a/vendor/gix-date/src/parse.rs b/vendor/gix-date/src/parse.rs index 7038a80fb..323292cba 100644 --- a/vendor/gix-date/src/parse.rs +++ b/vendor/gix-date/src/parse.rs @@ -1,20 +1,18 @@ #[derive(thiserror::Error, Debug, Clone)] #[allow(missing_docs)] pub enum Error { - #[error("Cannot represent times before UNIX epoch at timestamp {timestamp}")] - TooEarly { timestamp: i64 }, #[error("Could not convert a duration into a date")] RelativeTimeConversion, #[error("Date string can not be parsed")] InvalidDateString { input: String }, - #[error("Dates past 2038 can not be represented.")] + #[error("The heat-death of the universe happens before this date")] InvalidDate(#[from] std::num::TryFromIntError), #[error("Current time is missing but required to handle relative dates.")] MissingCurrentTime, } pub(crate) mod function { - use std::{convert::TryInto, str::FromStr, time::SystemTime}; + use std::{str::FromStr, time::SystemTime}; use time::{format_description::well_known, Date, OffsetDateTime}; @@ -24,7 +22,7 @@ pub(crate) mod function { format::{DEFAULT, GITOXIDE, ISO8601, ISO8601_STRICT, SHORT}, Sign, }, - Time, + SecondsSinceUnixEpoch, Time, }; #[allow(missing_docs)] @@ -36,42 +34,33 @@ pub(crate) mod function { Ok(if let Ok(val) = Date::parse(input, SHORT) { let val = val.with_hms(0, 0, 0).expect("date is in range").assume_utc(); - Time::new(val.unix_timestamp().try_into()?, val.offset().whole_seconds()) + Time::new(val.unix_timestamp(), val.offset().whole_seconds()) } else if let Ok(val) = OffsetDateTime::parse(input, &well_known::Rfc2822) { - Time::new(val.unix_timestamp().try_into()?, val.offset().whole_seconds()) + Time::new(val.unix_timestamp(), val.offset().whole_seconds()) } else if let Ok(val) = OffsetDateTime::parse(input, ISO8601) { - Time::new(val.unix_timestamp().try_into()?, val.offset().whole_seconds()) + Time::new(val.unix_timestamp(), val.offset().whole_seconds()) } else if let Ok(val) = OffsetDateTime::parse(input, ISO8601_STRICT) { - Time::new(val.unix_timestamp().try_into()?, val.offset().whole_seconds()) + Time::new(val.unix_timestamp(), val.offset().whole_seconds()) } else if let Ok(val) = OffsetDateTime::parse(input, GITOXIDE) { - Time::new(val.unix_timestamp().try_into()?, val.offset().whole_seconds()) + Time::new(val.unix_timestamp(), val.offset().whole_seconds()) } else if let Ok(val) = OffsetDateTime::parse(input, DEFAULT) { - Time::new(val.unix_timestamp().try_into()?, val.offset().whole_seconds()) - } else if let Ok(val) = u32::from_str(input) { + Time::new(val.unix_timestamp(), val.offset().whole_seconds()) + } else if let Ok(val) = SecondsSinceUnixEpoch::from_str(input) { // Format::Unix Time::new(val, 0) } else if let Some(val) = parse_raw(input) { // Format::Raw val } else if let Some(time) = relative::parse(input, now).transpose()? { - Time::new(timestamp(time)?, time.offset().whole_seconds()) + Time::new(time.unix_timestamp(), time.offset().whole_seconds()) } else { return Err(Error::InvalidDateString { input: input.into() }); }) } - fn timestamp(date: OffsetDateTime) -> Result<u32, Error> { - let timestamp = date.unix_timestamp(); - if timestamp < 0 { - Err(Error::TooEarly { timestamp }) - } else { - Ok(timestamp.try_into()?) - } - } - fn parse_raw(input: &str) -> Option<Time> { let mut split = input.split_whitespace(); - let seconds_since_unix_epoch: u32 = split.next()?.parse().ok()?; + let seconds: SecondsSinceUnixEpoch = split.next()?.parse().ok()?; let offset = split.next()?; if offset.len() != 5 || split.next().is_some() { return None; @@ -88,8 +77,8 @@ pub(crate) mod function { offset_in_seconds *= -1; }; let time = Time { - seconds_since_unix_epoch, - offset_in_seconds, + seconds, + offset: offset_in_seconds, sign, }; Some(time) 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*/ } } |