Skip to content

Commit

Permalink
Allow case insensitive layout names
Browse files Browse the repository at this point in the history
  • Loading branch information
maraino committed Jul 3, 2024
1 parent a87b577 commit 329b3f1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
28 changes: 14 additions & 14 deletions internal/templates/funcmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ func mustParseTime(v ...string) (time.Time, error) {
}

func toTimeLayout(fmt string) string {
switch strings.TrimPrefix(fmt, "time.") {
case "Layout":
switch strings.ToUpper(strings.TrimPrefix(fmt, "time.")) {
case "LAYOUT":
return time.Layout
case "ANSIC":
return time.ANSIC
case "UnixDate":
case "UNIXDATE":
return time.UnixDate
case "RubyDate":
case "RUBYDATE":
return time.RubyDate
case "RFC822":
return time.RFC822
Expand All @@ -134,24 +134,24 @@ func toTimeLayout(fmt string) string {
return time.RFC1123Z
case "RFC3339":
return time.RFC3339
case "RFC3339Nano":
case "RFC3339NANO":
return time.RFC3339Nano
// From the ones bellow, only time.DateTime will parse a complete date.
case "Kitchen":
// From the ones below, only time.DateTime will parse a complete date.
case "KITCHEN":
return time.Kitchen
case "Stamp":
case "STAMP":
return time.Stamp
case "StampMilli":
case "STAMPMILLI":
return time.StampMilli
case "StampMicro":
case "STAMPMICRO":
return time.StampMicro
case "StampNano":
case "STAMPNANO":
return time.StampNano
case "DateTime":
case "DATETIME":
return time.DateTime
case "DateOnly":
case "DATEONLY":
return time.DateOnly
case "TimeOnly":
case "TIMEONLY":
return time.TimeOnly
default:
return fmt
Expand Down
2 changes: 2 additions & 0 deletions internal/templates/funcmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ func TestGetFuncMap_toTimeLayout(t *testing.T) {
{"time.DateTime", args{"time.DateTime"}, time.DateTime},
{"time.DateOnly", args{"time.DateOnly"}, time.DateOnly},
{"time.TimeOnly", args{"time.TimeOnly"}, time.TimeOnly},
{"uppercase", args{"UNIXDATE"}, time.UnixDate},
{"lowercase", args{"rfc3339"}, time.RFC3339},
{"default", args{"MyFormat"}, "MyFormat"},
}
for _, tt := range tests {
Expand Down

0 comments on commit 329b3f1

Please sign in to comment.