From 4bc0ae1eb313ad83c793f397af9ca791b9b171e5 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sat, 10 Jun 2023 16:42:49 +0200 Subject: [PATCH 01/14] feat!: Represent time as 64 bit integer. That way, dates beyond 2038 are supported. Further, we rename `Time::seconds_since_unix_epoch` to `seconds`, and remove `seconds()` as there now is a new type, `SecondsSinceUnixEpoch`. In the same vein, we rename `Time::offset_in_seconds` to `offset` as it now has at type `OffsetInSeconds`. --- gix-date/src/lib.rs | 9 +++- gix-date/src/parse.rs | 14 +++--- gix-date/src/time/format.rs | 6 +-- gix-date/src/time/init.rs | 42 ++++++++-------- gix-date/src/time/mod.rs | 9 +--- gix-date/src/time/write.rs | 48 +++++++++++++------ .../fixtures/generate_git_date_baseline.sh | 4 +- .../generate_git_date_baseline.tar.xz | 4 +- gix-date/tests/time/baseline.rs | 11 +++-- gix-date/tests/time/format.rs | 8 ++-- gix-date/tests/time/mod.rs | 32 +++++++++---- gix-date/tests/time/parse.rs | 32 ++++++------- 12 files changed, 126 insertions(+), 93 deletions(-) diff --git a/gix-date/src/lib.rs b/gix-date/src/lib.rs index 736f5e59816..997f9167713 100644 --- a/gix-date/src/lib.rs +++ b/gix-date/src/lib.rs @@ -22,9 +22,14 @@ 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. +pub type SecondsSinceUnixEpoch = u64; +/// time offset in seconds. +pub type OffsetInSeconds = i32; diff --git a/gix-date/src/parse.rs b/gix-date/src/parse.rs index 7038a80fbd1..fc257834e10 100644 --- a/gix-date/src/parse.rs +++ b/gix-date/src/parse.rs @@ -7,7 +7,7 @@ pub enum Error { 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, @@ -24,7 +24,7 @@ pub(crate) mod function { format::{DEFAULT, GITOXIDE, ISO8601, ISO8601_STRICT, SHORT}, Sign, }, - Time, + SecondsSinceUnixEpoch, Time, }; #[allow(missing_docs)] @@ -47,7 +47,7 @@ pub(crate) mod function { Time::new(val.unix_timestamp().try_into()?, 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) { + } else if let Ok(val) = SecondsSinceUnixEpoch::from_str(input) { // Format::Unix Time::new(val, 0) } else if let Some(val) = parse_raw(input) { @@ -60,7 +60,7 @@ pub(crate) mod function { }) } - fn timestamp(date: OffsetDateTime) -> Result { + fn timestamp(date: OffsetDateTime) -> Result { let timestamp = date.unix_timestamp(); if timestamp < 0 { Err(Error::TooEarly { timestamp }) @@ -71,7 +71,7 @@ pub(crate) mod function { fn parse_raw(input: &str) -> Option