Skip to content

Commit

Permalink
Merge branch 'issue-679'
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Dec 31, 2022
2 parents 3569b9f + 26597b9 commit a910d9e
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 12 deletions.
2 changes: 1 addition & 1 deletion etc/check-package-size.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ echo "in root: gitoxide CLI"
(enter git-protocol && indent cargo diet -n --package-size-limit 80KB)
(enter git-packetline && indent cargo diet -n --package-size-limit 35KB)
(enter git-repository && indent cargo diet -n --package-size-limit 250KB)
(enter git-transport && indent cargo diet -n --package-size-limit 80KB)
(enter git-transport && indent cargo diet -n --package-size-limit 85KB)
(enter gitoxide-core && indent cargo diet -n --package-size-limit 100KB)
8 changes: 5 additions & 3 deletions git-date/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ pub enum Error {
pub(crate) mod function {
use std::{convert::TryInto, str::FromStr, time::SystemTime};

use time::{Date, OffsetDateTime};
use time::{format_description::well_known, Date, OffsetDateTime};

use crate::{
parse::{relative, Error},
time::{
format::{DEFAULT, ISO8601, ISO8601_STRICT, RFC2822, SHORT},
format::{DEFAULT, GIT_DEFAULT, ISO8601, ISO8601_STRICT, SHORT},
Sign,
},
Time,
Expand All @@ -35,14 +35,16 @@ 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())
} else if let Ok(val) = OffsetDateTime::parse(input, RFC2822) {
} else if let Ok(val) = OffsetDateTime::parse(input, &well_known::Rfc2822) {
Time::new(val.unix_timestamp().try_into()?, val.offset().whole_seconds())
} else if let Ok(val) = OffsetDateTime::parse(input, ISO8601) {
Time::new(val.unix_timestamp().try_into()?, 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())
} 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) = OffsetDateTime::parse(input, GIT_DEFAULT) {
Time::new(val.unix_timestamp().try_into()?, val.offset().whole_seconds())
} else if let Ok(val) = u32::from_str(input) {
// Format::Unix
Time::new(val, 0)
Expand Down
20 changes: 20 additions & 0 deletions git-date/src/time/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ pub const RFC2822: &[FormatItem<'_>] = format_description!(
"[weekday repr:short], [day] [month repr:short] [year] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]"
);

/// E.g. `Thu, 8 Aug 2022 12:45:06 +0800`. This is output by `git log --pretty=%aD`.
pub const GIT_RFC2822: &[FormatItem<'_>] = format_description!(
"[weekday repr:short], \
[day padding:none] \
[month repr:short] \
[year] \
[hour]:[minute]:[second] \
[offset_hour sign:mandatory][offset_minute]"
);

/// E.g. `2022-08-17 22:04:58 +0200`
pub const ISO8601: &[FormatItem<'_>] =
format_description!("[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]");
Expand All @@ -29,6 +39,16 @@ pub const DEFAULT: &[FormatItem<'_>] = format_description!(
"[weekday repr:short] [month repr:short] [day] [year] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]"
);

/// E.g. `Thu Sep 4 10:45:06 2022 -0400`. This is output by `git log --pretty=%ad`.
pub const GIT_DEFAULT: &[FormatItem<'_>] = format_description!(
"[weekday repr:short] \
[month repr:short] \
[day padding:none] \
[hour]:[minute]:[second] \
[year] \
[offset_hour sign:mandatory][offset_minute]"
);

mod format_impls {
use time::format_description::FormatItem;

Expand Down
34 changes: 34 additions & 0 deletions git-date/tests/time/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ fn iso8601_strict() {
#[test]
fn rfc2822() {
assert_eq!(time().format(format::RFC2822), "Fri, 30 Nov 1973 00:03:09 +0230");
assert_eq!(time_dec1().format(format::RFC2822), "Sat, 01 Dec 1973 00:03:09 +0230");
}

#[test]
fn git_rfc2822() {
assert_eq!(time().format(format::GIT_RFC2822), "Fri, 30 Nov 1973 00:03:09 +0230");
assert_eq!(
time_dec1().format(format::GIT_RFC2822),
"Sat, 1 Dec 1973 00:03:09 +0230"
);
}

#[test]
Expand All @@ -44,6 +54,22 @@ fn default() {
time().format(git_date::time::format::DEFAULT),
"Fri Nov 30 1973 00:03:09 +0230"
);
assert_eq!(
time_dec1().format(git_date::time::format::DEFAULT),
"Sat Dec 01 1973 00:03:09 +0230"
)
}

#[test]
fn git_default() {
assert_eq!(
time().format(git_date::time::format::GIT_DEFAULT),
"Fri Nov 30 00:03:09 1973 +0230"
);
assert_eq!(
time_dec1().format(git_date::time::format::GIT_DEFAULT),
"Sat Dec 1 00:03:09 1973 +0230"
)
}

#[test]
Expand All @@ -61,3 +87,11 @@ fn time() -> Time {
sign: Sign::Plus,
}
}

fn time_dec1() -> Time {
Time {
seconds_since_unix_epoch: 123543189,
offset_in_seconds: 9000,
sign: Sign::Plus,
}
}
43 changes: 35 additions & 8 deletions git-date/tests/time/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn special_time_is_ok_for_now() {
#[test]
fn short() {
assert_eq!(
git_date::parse("1979-02-26", Some(SystemTime::now())).expect("parsed date"),
git_date::parse("1979-02-26", Some(SystemTime::now())).unwrap(),
Time {
seconds_since_unix_epoch: 288835200,
offset_in_seconds: 0,
Expand All @@ -30,20 +30,36 @@ fn short() {
#[test]
fn rfc2822() {
assert_eq!(
git_date::parse("Thu, 18 Aug 2022 12:45:06 +0800", None).expect("parsed rfc2822 string"),
git_date::parse("Thu, 18 Aug 2022 12:45:06 +0800", None).unwrap(),
Time {
seconds_since_unix_epoch: 1660797906,
offset_in_seconds: 28800,
sign: Sign::Plus,
},
"could not parse with RFC2822 format"
);
}

#[test]
fn git_rfc2822() {
let expected = Time {
seconds_since_unix_epoch: 1659329106,
offset_in_seconds: 28800,
sign: Sign::Plus,
};
assert_eq!(
git_date::parse("Thu, 1 Aug 2022 12:45:06 +0800", None).unwrap(),
expected,
);
assert_eq!(
git_date::parse("Thu, 1 Aug 2022 12:45:06 +0800", None).unwrap(),
expected,
);
}

#[test]
fn raw() {
assert_eq!(
git_date::parse("1660874655 +0800", None).expect("parsed raw string"),
git_date::parse("1660874655 +0800", None).unwrap(),
Time {
seconds_since_unix_epoch: 1660874655,
offset_in_seconds: 28800,
Expand All @@ -53,13 +69,24 @@ fn raw() {
);

assert_eq!(
git_date::parse("1660874655 -0800", None).expect("parsed raw string"),
git_date::parse("1660874655 -0800", None).unwrap(),
Time {
seconds_since_unix_epoch: 1660874655,
offset_in_seconds: -28800,
sign: Sign::Minus,
},
"could not parse with raw format"
);
}

#[test]
fn git_default() {
assert_eq!(
git_date::parse("Thu Aug 8 12:45:06 2022 +0800", None).unwrap(),
Time {
seconds_since_unix_epoch: 1659933906,
offset_in_seconds: 28800,
sign: Sign::Plus,
},
);
}

Expand Down Expand Up @@ -97,14 +124,14 @@ mod relative {
#[test]
fn various() {
let now = Some(SystemTime::now());
let two_weeks_ago = git_date::parse("2 weeks ago", now).expect("valid time");
let two_weeks_ago = git_date::parse("2 weeks ago", now).unwrap();
assert_eq!(Sign::Plus, two_weeks_ago.sign);
assert_eq!(0, two_weeks_ago.offset_in_seconds);
let expected = OffsetDateTime::from(now.unwrap()).saturating_sub(Duration::weeks(2));
// account for the loss of precision when creating `Time` with seconds
let expected = expected.replace_nanosecond(0).unwrap();
assert_eq!(
OffsetDateTime::from_unix_timestamp(two_weeks_ago.seconds_since_unix_epoch as i64).expect("valid datetime"),
OffsetDateTime::from_unix_timestamp(two_weeks_ago.seconds_since_unix_epoch as i64).unwrap(),
expected,
"relative times differ"
);
Expand Down

0 comments on commit a910d9e

Please sign in to comment.