diff --git a/gen/config.go b/gen/config.go index d7143f4..119bf07 100644 --- a/gen/config.go +++ b/gen/config.go @@ -8,6 +8,7 @@ import ( "time" "github.com/alehechka/json2go/jenshared" + "github.com/alehechka/json2go/utils" ) // Config presents Gen configurations. @@ -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) } diff --git a/utils/timeFormat.go b/utils/timeFormat.go new file mode 100644 index 0000000..d703e7d --- /dev/null +++ b/utils/timeFormat.go @@ -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 +}