From 37367d708e0aea72bf3cef00808ab0069be0a606 Mon Sep 17 00:00:00 2001 From: Pierre Chevalier Date: Sun, 6 Apr 2025 13:08:31 +0100 Subject: [PATCH 01/14] feat: Expose parsing for Time `gix_date::Time` can be parsed in two ways: * From raw bytes (in the data format Git adheres too) * From the date format used in gix config Add utility constructors for these two cases. When parsing from raw bytes, also support a format like `` since this is a situation that happens in the wild and that Git can handle. --- gix-date/src/lib.rs | 17 +++++++++++++++++ gix-date/src/parse.rs | 11 ++++++++--- gix-date/tests/time/parse.rs | 19 +++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/gix-date/src/lib.rs b/gix-date/src/lib.rs index 20893a7cab8..9c5f8faee3b 100644 --- a/gix-date/src/lib.rs +++ b/gix-date/src/lib.rs @@ -15,7 +15,11 @@ pub mod time; /// pub mod parse; +use bstr::{BStr, ByteSlice}; pub use parse::function::parse; +use parse::function::parse_raw; +use parse::Error; +use std::time::SystemTime; /// A timestamp with timezone. #[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)] @@ -29,6 +33,19 @@ pub struct Time { pub sign: time::Sign, } +impl Time { + /// Parse date in config format into a Time + pub fn from_config(i: &BStr) -> Result { + let s = i.as_bstr().to_str().expect("Input must be ascii"); + parse(s, Some(SystemTime::now())) + } + /// Parse raw bytes into a Time + pub fn from_bytes(i: &BStr) -> Result { + let s = i.as_bstr().to_str().expect("Input must be ascii"); + parse_raw(s).ok_or(Error::InvalidDateString { input: s.into() }) + } +} + /// The amount of seconds since unix epoch. /// /// Note that negative dates represent times before the unix epoch. diff --git a/gix-date/src/parse.rs b/gix-date/src/parse.rs index afd3470ac60..622804ac0d1 100644 --- a/gix-date/src/parse.rs +++ b/gix-date/src/parse.rs @@ -60,11 +60,11 @@ pub(crate) mod function { }) } - fn parse_raw(input: &str) -> Option