-
Notifications
You must be signed in to change notification settings - Fork 3
/
strftime.go
278 lines (256 loc) · 5.97 KB
/
strftime.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Package tuesday implements a Strftime function that is compatible with Ruby's Time.strftime.
package tuesday
//go:generate ruby testdata/gen.rb
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
"unicode/utf8"
)
// Strftime is compatible with Ruby's Time.strftime.
//
// See https://ruby-doc.org/core-2.4.1/Time.html#method-i-strftime
//
// Strftime returns an error for compatibility with other formatting functions and for future compatibility,
// but in the current implementation this error is always nil.
func Strftime(format string, t time.Time) (string, error) {
return re.ReplaceAllStringFunc(format, func(directive string) string {
var (
pad, w = '0', 2
m = re.FindAllStringSubmatch(directive, 1)[0]
flags, width = m[1], m[2]
conversion, _ = utf8.DecodeRuneInString(m[3])
c = convert(t, conversion, flags, width)
)
if s, ok := c.(string); ok {
return applyFlags(flags, s)
}
if f, ok := defaultPadding[conversion]; ok {
pad, w = f.c, f.w
}
if len(width) > 0 {
w, _ = strconv.Atoi(width) // nolint: gas
}
switch flags {
case "-":
w = 0
case "_":
pad = '-'
case "0":
pad = '0'
}
var fm string
switch {
// Hardcode the defaults:
case pad == '-' && w == 2:
fm = "%2d"
case pad == '0' && w == 2:
fm = "%02d"
case pad == '0' && w == 3:
fm = "%03d"
case pad == '-':
fm = fmt.Sprintf("%%%dd", w)
default:
fm = fmt.Sprintf("%%%c%dd", pad, w)
}
s := fmt.Sprintf(fm, c)
return applyFlags(flags, s)
}), nil
}
var re = regexp.MustCompile(`%([-_^#0]|:{1,3})?(\d+)?[EO]?([a-zA-Z\+nt%])`)
// Test whether a string is uppercase, for purpose of applying the # case reversal flag.
// This is ASCII-only and is foiled by spaces and punctuation, but is sufficient for this context.
var isUpperRE = regexp.MustCompile(`^[[:upper:]]+$`).MatchString
var amPmTable = map[bool]string{true: "AM", false: "PM"}
var amPmLowerTable = map[bool]string{true: "am", false: "pm"}
// Default padding character and width, by conversion rune.
// The default default is pad='0', width=2
var defaultPadding = map[rune]struct {
c rune
w int
}{
'e': {'-', 2},
'f': {'0', 6},
'j': {'0', 3},
'k': {'-', 2},
'L': {'0', 3},
'l': {'-', 2},
'N': {'0', 9},
'u': {'-', 0},
'w': {'-', 0},
'Y': {'0', 4},
}
func applyFlags(flags, s string) string {
switch flags {
case "^":
return strings.ToUpper(s)
case "#":
if isUpperRE(s) {
return strings.ToLower(s)
}
return strings.ToUpper(s)
default:
return s
}
}
func convert(t time.Time, c rune, flags, width string) interface{} { // nolint: gocyclo
switch c {
// Date
case 'Y':
return t.Year()
case 'y':
return t.Year() % 100
case 'C':
return t.Year() / 100
case 'm':
return t.Month()
case 'B':
return t.Month().String()
case 'b', 'h':
return t.Month().String()[:3]
case 'd', 'e':
return t.Day()
case 'j':
return t.YearDay()
// Time
case 'H', 'k':
return t.Hour()
case 'I', 'l':
return (t.Hour()+11)%12 + 1
case 'M':
return t.Minute()
case 'S':
return t.Second()
case 'L':
return t.Nanosecond() / 1e6
case 'N':
ns := t.Nanosecond()
if len(width) > 0 {
w, _ := strconv.Atoi(width) // nolint: gas
if w <= 9 {
return fmt.Sprintf("%09d", ns)[:w]
}
return fmt.Sprintf(fmt.Sprintf("%%09d%%0%dd", w-9), ns, 0)
}
return ns
case 'P':
return amPmLowerTable[t.Hour() < 12]
case 'p':
return amPmTable[t.Hour() < 12]
// Time zone
case 'z':
_, offset := t.Zone()
sign := '+'
if offset < 0 {
offset, sign = -offset, '-'
}
var (
h = offset / 3600
m = (offset / 60) % 60
s = offset % 60
)
if flags == ":::" {
switch {
case s != 0:
flags = "::"
case m != 0:
flags = ":"
default:
flags = "H" // not a real flag; only used to talk to next switch
}
}
switch flags {
case "H":
return fmt.Sprintf("%c%02d", sign, h)
case ":":
return fmt.Sprintf("%c%02d:%02d", sign, h, m)
case "::":
return fmt.Sprintf("%c%02d:%02d:%02d", sign, h, m, s)
default:
return fmt.Sprintf("%c%02d%02d", sign, h, m)
}
case 'Z':
z, _ := t.Zone()
return z
// Weekday
case 'A':
return t.Weekday().String()
case 'a':
return t.Weekday().String()[:3]
case 'u':
return (t.Weekday()+6)%7 + 1
case 'w':
return t.Weekday()
// ISO week and year
case 'G':
y, _ := t.ISOWeek()
return y
case 'g':
y, _ := t.ISOWeek()
return y % 100
case 'V':
_, wn := t.ISOWeek()
return wn
// Ruby week
case 'U':
// day of year of first day of week (might be negative)
d := t.YearDay() - int(t.Weekday())
return (d + 6) / 7
case 'W':
// day of year of first day of (Monday-based) week
d := t.YearDay() - int(t.Weekday()) + 1
if t.Weekday() == time.Sunday {
d -= 7
}
return (d + 6) / 7
// Epoch seconds
case 's':
return t.Unix()
case 'Q':
return t.UnixNano() / 1000
// Literals
case 'n':
return "\n"
case 't':
return "\t"
case '%':
return "%"
// Combinations
case 'c':
// date and time (%a %b %e %T %Y)
h, m, s := t.Clock()
return fmt.Sprintf("%s %s %2d %02d:%02d:%02d %04d", t.Weekday().String()[:3], t.Month().String()[:3], t.Day(), h, m, s, t.Year())
case 'D', 'x':
// Date (%m/%d/%y)
y, m, d := t.Date()
return fmt.Sprintf("%02d/%02d/%02d", m, d, y%100)
case 'F':
// The ISO 8601 date format (%Y-%m-%d)
y, m, d := t.Date()
return fmt.Sprintf("%04d-%02d-%02d", y, m, d)
case 'v':
// VMS date (%e-%^b-%4Y)
return fmt.Sprintf("%2d-%s-%04d", t.Day(), strings.ToUpper(t.Month().String()[:3]), t.Year())
case 'r':
// 12-hour time (%I:%M:%S %p)
h, m, s := t.Clock()
h12 := (h+11)%12 + 1
return fmt.Sprintf("%02d:%02d:%02d %s", h12, m, s, amPmTable[h < 12])
case 'R':
// 24-hour time (%H:%M)
h, m, _ := t.Clock()
return fmt.Sprintf("%02d:%02d", h, m)
case 'T', 'X':
// 24-hour time (%H:%M:%S)
h, m, s := t.Clock()
return fmt.Sprintf("%02d:%02d:%02d", h, m, s)
case '+':
// date(1) (%a %b %e %H:%M:%S %Z %Y)
s, _ := Strftime("%a %b %e %H:%M:%S %Z %Y", t) // nolint: gas
return s
default:
return string([]rune{'%', c})
}
}