-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathformatting.go
220 lines (190 loc) · 6.43 KB
/
formatting.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package gostradamus
import (
"regexp"
"strings"
"time"
"github.com/dustin/go-humanize"
)
// All FormatTokens for parsing and formatting with DateTime
const (
YearFull = FormatToken("YYYY")
YearShort = FormatToken("YY")
MonthFull = FormatToken("MMMM")
MonthAbbr = FormatToken("MMM")
MonthZeroPadded = FormatToken("MM")
MonthShort = FormatToken("M")
DayOfYearZeroPadded = FormatToken("DDDD")
DayOfMonthZeroPadded = FormatToken("DD")
DayOfMonthShort = FormatToken("D")
DayOfMonthOrdinal = FormatToken("Do")
DayOfWeekFullName = FormatToken("dddd")
DayOfWeekAbbr = FormatToken("ddd")
TwentyFourHourZeroPadded = FormatToken("HH")
TwelveHourZeroPadded = FormatToken("hh")
TwelveHour = FormatToken("h")
AMPMUpper = FormatToken("A")
AMPMLower = FormatToken("a")
MinuteZeroPadded = FormatToken("mm")
Minute = FormatToken("m")
SecondZeroPadded = FormatToken("ss")
Second = FormatToken("s")
MicroSecond = FormatToken("S")
TimezoneFullName = FormatToken("ZZZ")
TimezoneWithColon = FormatToken("zz")
TimezoneWithoutColon = FormatToken("Z")
GoLongMonth = goFormatToken("January")
GoMonth = goFormatToken("Jan")
GoNumMonth = goFormatToken("1")
GoZeroMonth = goFormatToken("01")
GoLongWeekDay = goFormatToken("Monday")
GoWeekDay = goFormatToken("Mon")
GoDay = goFormatToken("2")
GoDayOrdinal = goFormatToken("Do")
GoZeroDay = goFormatToken("02")
GoZeroYearDay = goFormatToken("002")
GoHour = goFormatToken("15")
GoHour12 = goFormatToken("3")
GoZeroHour12 = goFormatToken("03")
GoMinute = goFormatToken("4")
GoZeroMinute = goFormatToken("04")
GoSecond = goFormatToken("5")
GoZeroSecond = goFormatToken("05")
GoLongYear = goFormatToken("2006")
GoYear = goFormatToken("06")
GoPM = goFormatToken("PM")
Gopm = goFormatToken("pm")
GoTZ = goFormatToken("MST")
GoMillisecond = goFormatToken("000")
GoMicrosecond = goFormatToken("000000")
GoNanoSecond = goFormatToken("000000000")
GoISO8601TZ = goFormatToken("Z0700") // prints Z for UTC
GoISO8601SecondsTZ = goFormatToken("Z070000")
GoISO8601ShortTZ = goFormatToken("Z07")
GoISO8601ColonTZ = goFormatToken("Z07:00") // prints Z for UTC
GoISO8601ColonSecondsTZ = goFormatToken("Z07:00:00")
GoNumTZ = goFormatToken("-0700") // always numeric
GoNumSecondsTz = goFormatToken("-070000")
GoNumShortTZ = goFormatToken("-07") // always numeric
GoNumColonTZ = goFormatToken("-07:00") // always numeric
GoNumColonSecondsTZ = goFormatToken("-07:00:00")
)
// FormatToken helps to hold all possible tokens for parsing and formatting
type FormatToken string
type formatTokens []FormatToken
type goFormatToken string
var (
formatTokenMap = map[FormatToken]goFormatToken{
YearFull: GoLongYear,
YearShort: GoYear,
MonthFull: GoLongMonth,
MonthAbbr: GoMonth,
MonthZeroPadded: GoZeroMonth,
MonthShort: GoNumMonth,
DayOfYearZeroPadded: GoZeroYearDay,
DayOfMonthZeroPadded: GoZeroDay,
DayOfMonthShort: GoDay,
DayOfWeekFullName: GoLongWeekDay,
DayOfMonthOrdinal: GoDayOrdinal,
DayOfWeekAbbr: GoWeekDay,
TwentyFourHourZeroPadded: GoHour,
TwelveHourZeroPadded: GoZeroHour12,
TwelveHour: GoHour12,
AMPMUpper: GoPM,
AMPMLower: Gopm,
MinuteZeroPadded: GoZeroMinute,
Minute: GoMinute,
SecondZeroPadded: GoZeroSecond,
Second: GoSecond,
MicroSecond: GoMicrosecond,
TimezoneFullName: GoTZ,
TimezoneWithColon: GoISO8601ColonTZ,
TimezoneWithoutColon: GoISO8601TZ,
}
allFormatTokens = formatTokens{
YearFull,
YearShort,
MonthFull,
MonthAbbr,
MonthZeroPadded,
MonthShort,
DayOfYearZeroPadded,
DayOfMonthZeroPadded,
DayOfMonthOrdinal,
DayOfMonthShort,
DayOfWeekFullName,
DayOfWeekAbbr,
TwentyFourHourZeroPadded,
TwelveHourZeroPadded,
TwelveHour,
AMPMUpper,
AMPMLower,
MinuteZeroPadded,
Minute,
SecondZeroPadded,
Second,
MicroSecond,
TimezoneFullName,
TimezoneWithColon,
TimezoneWithoutColon,
}
)
func (fts formatTokens) toStringSlice() []string {
var stringSlice []string
for _, formatToken := range fts {
stringSlice = append(stringSlice, string(formatToken))
}
return stringSlice
}
func formatTokenRegex() string {
return strings.Join(allFormatTokens.toStringSlice(), "|")
}
// translateFormat translates all mapped formats to Go specific language
func translateFormat(format string) string {
re := regexp.MustCompile(formatTokenRegex())
format = string(re.ReplaceAllFunc(
[]byte(format),
func(bytes []byte) []byte {
if goFormatToken, ok := formatTokenMap[FormatToken(bytes)]; ok {
return []byte(goFormatToken)
}
panic(FormatTokenIsNotMapped(string(bytes)))
},
))
return format
}
// parseToTime parses the value with given format to a time.Time
// error if the value could not be parsed
//
// parseToTime panics if the formatToken is not mapped correctly
func parseToTime(value string, format string, timezone Timezone) (time.Time, error) {
re := regexp.MustCompile(formatTokenRegex())
format = string(re.ReplaceAllFunc(
[]byte(format),
func(bytes []byte) []byte {
if FormatToken(bytes) == FormatToken(DayOfMonthOrdinal) {
// todo: prone to accidental replacements of said matches when used incidentally outside of 'Do' context
replacer := strings.NewReplacer(
"th", "",
"rd", "",
"st", "",
"nd", "",
)
value = replacer.Replace(value)
return []byte(DayOfMonthShort)
}
return bytes
},
))
format = translateFormat(format)
return time.ParseInLocation(format, value, timezone.Location())
}
// formatFromTime formats value as time.Time with given format to a string
func formatFromTime(value time.Time, format string) string {
format = translateFormat(format)
v := value.Format(format)
v = strings.ReplaceAll(v, string(GoDayOrdinal), dayOrdinal(value))
return v
}
func dayOrdinal(t time.Time) string {
return humanize.Ordinal(t.Day())
}