Skip to content

Commit

Permalink
feat: Support git default date format
Browse files Browse the repository at this point in the history
This is the format output by default by `git log` or when using
`--pretty=%ad`.

The new git_date::time::format::GIT_DEFAULT format description may be used
to output date strings in this format. It is also now used by
git_date::parse() to accept date strings that may be in this format.

Refs: #679

Signed-off-by: Peter Grayson <pete@jpgrayson.net>
  • Loading branch information
jpgrayson committed Dec 30, 2022
1 parent 8094351 commit 4066ac7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
4 changes: 3 additions & 1 deletion git-date/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(crate) mod function {
use crate::{
parse::{relative, Error},
time::{
format::{DEFAULT, ISO8601, ISO8601_STRICT, SHORT},
format::{DEFAULT, GIT_DEFAULT, ISO8601, ISO8601_STRICT, SHORT},
Sign,
},
Time,
Expand All @@ -43,6 +43,8 @@ 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) = 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
10 changes: 10 additions & 0 deletions git-date/src/time/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,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
16 changes: 16 additions & 0 deletions git-date/tests/time/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,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 Down
13 changes: 13 additions & 0 deletions git-date/tests/time/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ fn raw() {
);
}

#[test]
fn git_default() {
assert_eq!(
git_date::parse("Thu Aug 8 12:45:06 2022 +0800", None).expect("parsed git default string"),
Time {
seconds_since_unix_epoch: 1659933906,
offset_in_seconds: 28800,
sign: Sign::Plus,
},
"could not parse with git default format"
);
}

#[test]
fn invalid_dates_can_be_produced_without_current_time() {
assert!(matches!(
Expand Down

0 comments on commit 4066ac7

Please sign in to comment.