Skip to content

Commit

Permalink
Merge pull request #8 from alehechka/feature/refactor-time-format
Browse files Browse the repository at this point in the history
Refactor Time Format
  • Loading branch information
alehechka authored Jul 1, 2022
2 parents 52f4aa7 + 7456023 commit 19cf20b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 41 deletions.
43 changes: 2 additions & 41 deletions gen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/alehechka/json2go/jenshared"
"github.com/alehechka/json2go/utils"
)

// Config presents Gen configurations.
Expand Down Expand Up @@ -65,45 +66,5 @@ func (c *Config) prepareOutputFileName() {
}

func (c *Config) getTimeFormat() string {

if len(c.TimeFormat) == 0 {
return time.RFC3339
}

switch c.TimeFormat {
case "Layout":
return time.Layout
case "ANSIC":
return time.ANSIC
case "UnixDate":
return time.UnixDate
case "RubyDate":
return time.RubyDate
case "RFC822":
return time.RFC822
case "RFC822Z":
return time.RFC822Z
case "RFC850":
return time.RFC850
case "RFC1123":
return time.RFC1123
case "RFC1123Z":
return time.RFC1123Z
case "RFC3339":
return time.RFC3339
case "RFC3339Nano":
return time.RFC3339Nano
case "Kitchen":
return time.Kitchen
case "Stamp":
return time.Stamp
case "StampMilli":
return time.StampMilli
case "StampMicro":
return time.StampMicro
case "StampNano":
return time.StampNano
default:
return c.TimeFormat
}
return utils.GetTimeFormat(c.TimeFormat, time.RFC3339)
}
36 changes: 36 additions & 0 deletions utils/timeFormat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package utils

import "time"

// TimeFormatMap map of time formats
var TimeFormatMap = map[string]string{
"Layout": time.Layout,
"ANSIC": time.ANSIC,
"UnixDate": time.UnixDate,
"RubyDate": time.RubyDate,
"RFC822": time.RFC822,
"RFC822Z": time.RFC822Z,
"RFC850": time.RFC850,
"RFC1123": time.RFC1123,
"RFC1123Z": time.RFC1123Z,
"RFC3339": time.RFC3339,
"RFC3339Nano": time.RFC3339Nano,
"Kitchen": time.Kitchen,
"Stamp": time.Stamp,
"StampMilli": time.StampMilli,
"StampMicro": time.StampMicro,
"StampNano": time.StampNano,
}

// GetTimeFormat attempts to find a standard time format, else will return the fallback on empty strings or unaltered.
func GetTimeFormat(format string, fallback string) string {
if len(format) == 0 {
return fallback
}

if standard, exists := TimeFormatMap[format]; exists {
return standard
}

return format
}

0 comments on commit 19cf20b

Please sign in to comment.