Skip to content

Commit

Permalink
feat: Format git-style RFC 2822 date strings
Browse files Browse the repository at this point in the history
Git outputs the day-of-month field as a non-padded number whereas strict
RFC 2822 date strings are supposed to use a zero-padded two-digit number.

The new git_date::time::format::GIT_RFC2822 format description allows Time
to be formatted in git's RFC 2822 style. (Whereas the existing RFC2822
format description produces a strict RFC 2822 date string).

Refs: #679

Signed-off-by: Peter Grayson <pete@jpgrayson.net>
  • Loading branch information
jpgrayson committed Dec 30, 2022
1 parent dff0aa0 commit 8094351
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
10 changes: 10 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 Down
9 changes: 9 additions & 0 deletions git-date/tests/time/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ fn rfc2822() {
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]
fn default() {
assert_eq!(
Expand Down

0 comments on commit 8094351

Please sign in to comment.