MCPcopy Index your code
hub / github.com/antonmedv/gitmal / ParsePatchDate

Function ParsePatchDate

pkg/gitdiff/patch_header.go:75–117  ·  view source on GitHub ↗

ParsePatchDate parses a patch date string. It returns the parsed time or an error if s has an unknown format. ParsePatchDate supports the iso, rfc, short, raw, unix, and default formats (with local variants) used by the --date flag in Git.

(s string)

Source from the content-addressed store, hash-verified

73// short, raw, unix, and default formats (with local variants) used by the
74// --date flag in Git.
75func ParsePatchDate(s string) (time.Time, error) {
76 const (
77 isoFormat = "2006-01-02 15:04:05 -0700"
78 isoStrictFormat = "2006-01-02T15:04:05-07:00"
79 rfc2822Format = "Mon, 2 Jan 2006 15:04:05 -0700"
80 shortFormat = "2006-01-02"
81 defaultFormat = "Mon Jan 2 15:04:05 2006 -0700"
82 defaultLocalFormat = "Mon Jan 2 15:04:05 2006"
83 )
84
85 if s == "" {
86 return time.Time{}, nil
87 }
88
89 for _, fmt := range []string{
90 isoFormat,
91 isoStrictFormat,
92 rfc2822Format,
93 shortFormat,
94 defaultFormat,
95 defaultLocalFormat,
96 } {
97 if t, err := time.ParseInLocation(fmt, s, time.Local); err == nil {
98 return t, nil
99 }
100 }
101
102 // unix format
103 if unix, err := strconv.ParseInt(s, 10, 64); err == nil {
104 return time.Unix(unix, 0), nil
105 }
106
107 // raw format
108 if space := strings.IndexByte(s, ' '); space > 0 {
109 unix, uerr := strconv.ParseInt(s[:space], 10, 64)
110 zone, zerr := time.Parse("-0700", s[space+1:])
111 if uerr == nil && zerr == nil {
112 return time.Unix(unix, 0).In(zone.Location()), nil
113 }
114 }
115
116 return time.Time{}, fmt.Errorf("unknown date format: %s", s)
117}
118
119// A PatchHeaderOption modifies the behavior of ParsePatchHeader.
120type PatchHeaderOption func(*patchHeaderOptions)

Callers 3

TestParsePatchDateFunction · 0.85
parseHeaderPrettyFunction · 0.85
parseHeaderMailFunction · 0.85

Calls 1

ErrorfMethod · 0.80

Tested by 1

TestParsePatchDateFunction · 0.68