Skip to content

Commit 19cf20b

Browse files
authored
Merge pull request #8 from alehechka/feature/refactor-time-format
Refactor Time Format
2 parents 52f4aa7 + 7456023 commit 19cf20b

File tree

2 files changed

+38
-41
lines changed

2 files changed

+38
-41
lines changed

gen/config.go

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/alehechka/json2go/jenshared"
11+
"github.com/alehechka/json2go/utils"
1112
)
1213

1314
// Config presents Gen configurations.
@@ -65,45 +66,5 @@ func (c *Config) prepareOutputFileName() {
6566
}
6667

6768
func (c *Config) getTimeFormat() string {
68-
69-
if len(c.TimeFormat) == 0 {
70-
return time.RFC3339
71-
}
72-
73-
switch c.TimeFormat {
74-
case "Layout":
75-
return time.Layout
76-
case "ANSIC":
77-
return time.ANSIC
78-
case "UnixDate":
79-
return time.UnixDate
80-
case "RubyDate":
81-
return time.RubyDate
82-
case "RFC822":
83-
return time.RFC822
84-
case "RFC822Z":
85-
return time.RFC822Z
86-
case "RFC850":
87-
return time.RFC850
88-
case "RFC1123":
89-
return time.RFC1123
90-
case "RFC1123Z":
91-
return time.RFC1123Z
92-
case "RFC3339":
93-
return time.RFC3339
94-
case "RFC3339Nano":
95-
return time.RFC3339Nano
96-
case "Kitchen":
97-
return time.Kitchen
98-
case "Stamp":
99-
return time.Stamp
100-
case "StampMilli":
101-
return time.StampMilli
102-
case "StampMicro":
103-
return time.StampMicro
104-
case "StampNano":
105-
return time.StampNano
106-
default:
107-
return c.TimeFormat
108-
}
69+
return utils.GetTimeFormat(c.TimeFormat, time.RFC3339)
10970
}

utils/timeFormat.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package utils
2+
3+
import "time"
4+
5+
// TimeFormatMap map of time formats
6+
var TimeFormatMap = map[string]string{
7+
"Layout": time.Layout,
8+
"ANSIC": time.ANSIC,
9+
"UnixDate": time.UnixDate,
10+
"RubyDate": time.RubyDate,
11+
"RFC822": time.RFC822,
12+
"RFC822Z": time.RFC822Z,
13+
"RFC850": time.RFC850,
14+
"RFC1123": time.RFC1123,
15+
"RFC1123Z": time.RFC1123Z,
16+
"RFC3339": time.RFC3339,
17+
"RFC3339Nano": time.RFC3339Nano,
18+
"Kitchen": time.Kitchen,
19+
"Stamp": time.Stamp,
20+
"StampMilli": time.StampMilli,
21+
"StampMicro": time.StampMicro,
22+
"StampNano": time.StampNano,
23+
}
24+
25+
// GetTimeFormat attempts to find a standard time format, else will return the fallback on empty strings or unaltered.
26+
func GetTimeFormat(format string, fallback string) string {
27+
if len(format) == 0 {
28+
return fallback
29+
}
30+
31+
if standard, exists := TimeFormatMap[format]; exists {
32+
return standard
33+
}
34+
35+
return format
36+
}

0 commit comments

Comments
 (0)