diff --git a/git-date/src/parse.rs b/git-date/src/parse.rs index 1bb402e0b93..edb728f905f 100644 --- a/git-date/src/parse.rs +++ b/git-date/src/parse.rs @@ -4,6 +4,7 @@ use crate::Time; use std::convert::TryInto; use std::num::TryFromIntError; use std::str::FromStr; +use std::time::SystemTime; use time::{Date, OffsetDateTime}; #[derive(thiserror::Error, Debug)] @@ -15,10 +16,12 @@ pub enum Error { InvalidPeriod, #[error("Dates past 2038 can not be represented.")] InvalidDate(#[from] TryFromIntError), + #[error("Current time is missing.")] + MissingCurrentTime, } #[allow(missing_docs)] -pub fn parse(input: &str) -> Result { +pub fn parse(input: &str, now: Option) -> Result { // TODO: actual implementation, this is just to not constantly fail if input == "1979-02-26 18:30:00" { Ok(Time::new(42, 1800)) @@ -55,8 +58,11 @@ pub fn parse(input: &str) -> Result { } else if let Some(val) = parse_raw(input) { // Format::Raw Ok(val) - } else if let Some(val) = relative::parse(input) { - Ok(Time::new(val.unix_timestamp() as u32, val.offset().whole_seconds())) + } else if let Some(val) = relative::parse(input, now.ok_or(Error::MissingCurrentTime)?) { + Ok(Time::new( + val.unix_timestamp().try_into()?, + val.offset().whole_seconds(), + )) } else { Err(Error::InvalidDateString) }; @@ -85,26 +91,27 @@ fn parse_raw(input: &str) -> Option