Skip to content

Commit f3b83ad

Browse files
authored
Allow unpadded days in patch headers (#14)
A closer look at the Git source shows that the rfc2822 and default formats do not zero-pad days, so we need to accept single-digit days when parsing. The single-digit pattern will also accept the padded format, so there's no loss of functionality. Change the parsing test to use single-digit values to emphasize when padding is required and when it is not.
1 parent 4fa9801 commit f3b83ad

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

gitdiff/patch_header.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,10 @@ func ParsePatchDate(s string) PatchDate {
126126
const (
127127
isoFormat = "2006-01-02 15:04:05 -0700"
128128
isoStrictFormat = "2006-01-02T15:04:05-07:00"
129-
rfc2822Format = "Mon, 02 Jan 2006 15:04:05 -0700"
129+
rfc2822Format = "Mon, 2 Jan 2006 15:04:05 -0700"
130130
shortFormat = "2006-01-02"
131-
defaultFormat = "Mon Jan 02 15:04:05 2006 -0700"
132-
defaultLocalFormat = "Mon Jan 02 15:04:05 2006"
131+
defaultFormat = "Mon Jan 2 15:04:05 2006 -0700"
132+
defaultLocalFormat = "Mon Jan 2 15:04:05 2006"
133133
)
134134

135135
d := PatchDate{Raw: s}

gitdiff/patch_header_test.go

+21-21
Original file line numberDiff line numberDiff line change
@@ -65,72 +65,72 @@ func TestParsePatchIdentity(t *testing.T) {
6565
}
6666

6767
func TestParsePatchDate(t *testing.T) {
68-
expected := time.Date(2020, 04, 11, 22, 21, 23, 0, time.UTC)
68+
expected := time.Date(2020, 4, 9, 8, 7, 6, 0, time.UTC)
6969

7070
tests := map[string]struct {
7171
Input string
7272
Output PatchDate
7373
}{
7474
"default": {
75-
Input: "Sat Apr 11 15:21:23 2020 -0700",
75+
Input: "Thu Apr 9 01:07:06 2020 -0700",
7676
Output: PatchDate{
7777
Parsed: expected,
78-
Raw: "Sat Apr 11 15:21:23 2020 -0700",
78+
Raw: "Thu Apr 9 01:07:06 2020 -0700",
7979
},
8080
},
8181
"defaultLocal": {
82-
Input: "Sat Apr 11 15:21:23 2020",
82+
Input: "Thu Apr 9 01:07:06 2020",
8383
Output: PatchDate{
84-
Parsed: time.Date(2020, 04, 11, 15, 21, 23, 0, time.Local),
85-
Raw: "Sat Apr 11 15:21:23 2020",
84+
Parsed: time.Date(2020, 4, 9, 1, 7, 6, 0, time.Local),
85+
Raw: "Thu Apr 9 01:07:06 2020",
8686
},
8787
},
8888
"iso": {
89-
Input: "2020-04-11 15:21:23 -0700",
89+
Input: "2020-04-09 01:07:06 -0700",
9090
Output: PatchDate{
9191
Parsed: expected,
92-
Raw: "2020-04-11 15:21:23 -0700",
92+
Raw: "2020-04-09 01:07:06 -0700",
9393
},
9494
},
9595
"isoStrict": {
96-
Input: "2020-04-11T15:21:23-07:00",
96+
Input: "2020-04-09T01:07:06-07:00",
9797
Output: PatchDate{
9898
Parsed: expected,
99-
Raw: "2020-04-11T15:21:23-07:00",
99+
Raw: "2020-04-09T01:07:06-07:00",
100100
},
101101
},
102102
"rfc": {
103-
Input: "Sat, 11 Apr 2020 15:21:23 -0700",
103+
Input: "Thu, 9 Apr 2020 01:07:06 -0700",
104104
Output: PatchDate{
105105
Parsed: expected,
106-
Raw: "Sat, 11 Apr 2020 15:21:23 -0700",
106+
Raw: "Thu, 9 Apr 2020 01:07:06 -0700",
107107
},
108108
},
109109
"short": {
110-
Input: "2020-04-11",
110+
Input: "2020-04-09",
111111
Output: PatchDate{
112-
Parsed: time.Date(2020, 04, 11, 0, 0, 0, 0, time.Local),
113-
Raw: "2020-04-11",
112+
Parsed: time.Date(2020, 4, 9, 0, 0, 0, 0, time.Local),
113+
Raw: "2020-04-09",
114114
},
115115
},
116116
"raw": {
117-
Input: "1586643683 -0700",
117+
Input: "1586419626 -0700",
118118
Output: PatchDate{
119119
Parsed: expected,
120-
Raw: "1586643683 -0700",
120+
Raw: "1586419626 -0700",
121121
},
122122
},
123123
"unix": {
124-
Input: "1586643683",
124+
Input: "1586419626",
125125
Output: PatchDate{
126126
Parsed: expected,
127-
Raw: "1586643683",
127+
Raw: "1586419626",
128128
},
129129
},
130130
"unknownFormat": {
131-
Input: "4/11/2020 15:21:23 PDT",
131+
Input: "4/9/2020 01:07:06 PDT",
132132
Output: PatchDate{
133-
Raw: "4/11/2020 15:21:23 PDT",
133+
Raw: "4/9/2020 01:07:06 PDT",
134134
},
135135
},
136136
"empty": {

0 commit comments

Comments
 (0)