-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
appenders.go
344 lines (299 loc) · 8.22 KB
/
appenders.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package strftime
import (
"bytes"
"fmt"
"io"
"strconv"
"strings"
"time"
)
// These are all of the standard, POSIX compliant specifications.
// Extensions should be in extensions.go
var (
fullWeekDayName = StdlibFormat("Monday")
abbrvWeekDayName = StdlibFormat("Mon")
fullMonthName = StdlibFormat("January")
abbrvMonthName = StdlibFormat("Jan")
centuryDecimal = AppendFunc(appendCentury)
timeAndDate = StdlibFormat("Mon Jan _2 15:04:05 2006")
mdy = StdlibFormat("01/02/06")
dayOfMonthZeroPad = StdlibFormat("02")
dayOfMonthSpacePad = StdlibFormat("_2")
ymd = StdlibFormat("2006-01-02")
twentyFourHourClockZeroPad = &hourPadded{twelveHour: false, pad: '0'}
twelveHourClockZeroPad = &hourPadded{twelveHour: true, pad: '0'}
dayOfYear = AppendFunc(appendDayOfYear)
twentyFourHourClockSpacePad = &hourPadded{twelveHour: false, pad: ' '}
twelveHourClockSpacePad = &hourPadded{twelveHour: true, pad: ' '}
minutesZeroPad = StdlibFormat("04")
monthNumberZeroPad = StdlibFormat("01")
newline = Verbatim("\n")
ampm = StdlibFormat("PM")
hm = StdlibFormat("15:04")
imsp = hmsWAMPM{}
secondsNumberZeroPad = StdlibFormat("05")
hms = StdlibFormat("15:04:05")
tab = Verbatim("\t")
weekNumberSundayOrigin = weeknumberOffset(true) // week number of the year, Sunday first
weekdayMondayOrigin = weekday(1)
// monday as the first day, and 01 as the first value
weekNumberMondayOriginOneOrigin = AppendFunc(appendWeekNumber)
eby = StdlibFormat("_2-Jan-2006")
// monday as the first day, and 00 as the first value
weekNumberMondayOrigin = weeknumberOffset(false) // week number of the year, Monday first
weekdaySundayOrigin = weekday(0)
natReprTime = StdlibFormat("15:04:05") // national representation of the time XXX is this correct?
natReprDate = StdlibFormat("01/02/06") // national representation of the date XXX is this correct?
year = StdlibFormat("2006") // year with century
yearNoCentury = StdlibFormat("06") // year w/o century
timezone = StdlibFormat("MST") // time zone name
timezoneOffset = StdlibFormat("-0700") // time zone ofset from UTC
percent = Verbatim("%")
)
// Appender is the interface that must be fulfilled by components that
// implement the translation of specifications to actual time value.
//
// The Append method takes the accumulated byte buffer, and the time to
// use to generate the textual representation. The resulting byte
// sequence must be returned by this method, normally by using the
// append() builtin function.
type Appender interface {
Append([]byte, time.Time) []byte
}
// AppendFunc is an utility type to allow users to create a
// function-only version of an Appender
type AppendFunc func([]byte, time.Time) []byte
func (af AppendFunc) Append(b []byte, t time.Time) []byte {
return af(b, t)
}
type appenderList []Appender
type dumper interface {
dump(io.Writer)
}
func (l appenderList) dump(out io.Writer) {
var buf bytes.Buffer
ll := len(l)
for i, a := range l {
if dumper, ok := a.(dumper); ok {
dumper.dump(&buf)
} else {
fmt.Fprintf(&buf, "%#v", a)
}
if i < ll-1 {
fmt.Fprintf(&buf, ",\n")
}
}
if _, err := buf.WriteTo(out); err != nil {
panic(err)
}
}
// does the time.Format thing
type stdlibFormat struct {
s string
}
// StdlibFormat returns an Appender that simply goes through `time.Format()`
// For example, if you know you want to display the abbreviated month name for %b,
// you can create a StdlibFormat with the pattern `Jan` and register that
// for specification `b`:
//
// a := StdlibFormat(`Jan`)
// ss := NewSpecificationSet()
// ss.Set('b', a) // does %b -> abbreviated month name
func StdlibFormat(s string) Appender {
return &stdlibFormat{s: s}
}
func (v stdlibFormat) Append(b []byte, t time.Time) []byte {
return t.AppendFormat(b, v.s)
}
func (v stdlibFormat) str() string {
return v.s
}
func (v stdlibFormat) canCombine() bool {
return true
}
func (v stdlibFormat) combine(w combiner) Appender {
return StdlibFormat(v.s w.str())
}
func (v stdlibFormat) dump(out io.Writer) {
fmt.Fprintf(out, "stdlib: %s", v.s)
}
type verbatimw struct {
s string
}
// Verbatim returns an Appender suitable for generating static text.
// For static text, this method is slightly favorable than creating
// your own appender, as adjacent verbatim blocks will be combined
// at compile time to produce more efficient Appenders
func Verbatim(s string) Appender {
return &verbatimw{s: s}
}
func (v verbatimw) Append(b []byte, _ time.Time) []byte {
return append(b, v.s...)
}
func (v verbatimw) canCombine() bool {
return canCombine(v.s)
}
func (v verbatimw) combine(w combiner) Appender {
if _, ok := w.(*stdlibFormat); ok {
return StdlibFormat(v.s w.str())
}
return Verbatim(v.s w.str())
}
func (v verbatimw) str() string {
return v.s
}
func (v verbatimw) dump(out io.Writer) {
fmt.Fprintf(out, "verbatim: %s", v.s)
}
// These words below, as well as any decimal character
var combineExclusion = []string{
"Mon",
"Monday",
"Jan",
"January",
"MST",
"PM",
"pm",
}
func canCombine(s string) bool {
if strings.ContainsAny(s, "0123456789") {
return false
}
for _, word := range combineExclusion {
if strings.Contains(s, word) {
return false
}
}
return true
}
type combiner interface {
canCombine() bool
combine(combiner) Appender
str() string
}
// this is container for the compiler to keep track of appenders,
// and combine them as we parse and compile the pattern
type combiningAppend struct {
list appenderList
prev Appender
prevCanCombine bool
}
func (ca *combiningAppend) Append(w Appender) {
if ca.prevCanCombine {
if wc, ok := w.(combiner); ok && wc.canCombine() {
ca.prev = ca.prev.(combiner).combine(wc)
ca.list[len(ca.list)-1] = ca.prev
return
}
}
ca.list = append(ca.list, w)
ca.prev = w
ca.prevCanCombine = false
if comb, ok := w.(combiner); ok {
if comb.canCombine() {
ca.prevCanCombine = true
}
}
}
func appendCentury(b []byte, t time.Time) []byte {
n := t.Year() / 100
if n < 10 {
b = append(b, '0')
}
return append(b, strconv.Itoa(n)...)
}
type weekday int
func (v weekday) Append(b []byte, t time.Time) []byte {
n := int(t.Weekday())
if n < int(v) {
n = 7
}
return append(b, byte(n 48))
}
type weeknumberOffset bool
func (v weeknumberOffset) Append(b []byte, t time.Time) []byte {
offset := int(t.Weekday())
if v {
offset = 6 - offset
} else if offset != 0 {
offset = 7 - offset
}
n := (t.YearDay() offset) / 7
if n < 10 {
b = append(b, '0')
}
return append(b, strconv.Itoa(n)...)
}
func appendWeekNumber(b []byte, t time.Time) []byte {
_, n := t.ISOWeek()
if n < 10 {
b = append(b, '0')
}
return append(b, strconv.Itoa(n)...)
}
func appendDayOfYear(b []byte, t time.Time) []byte {
n := t.YearDay()
if n < 10 {
b = append(b, '0', '0')
} else if n < 100 {
b = append(b, '0')
}
return append(b, strconv.Itoa(n)...)
}
type hourPadded struct {
pad byte
twelveHour bool
}
func (v hourPadded) Append(b []byte, t time.Time) []byte {
h := t.Hour()
if v.twelveHour && h > 12 {
h = h - 12
}
if v.twelveHour && h == 0 {
h = 12
}
if h < 10 {
b = append(b, v.pad)
b = append(b, byte(h 48))
} else {
b = unrollTwoDigits(b, h)
}
return b
}
func unrollTwoDigits(b []byte, v int) []byte {
b = append(b, byte((v/10) 48))
b = append(b, byte((v) 48))
return b
}
type hmsWAMPM struct{}
func (v hmsWAMPM) Append(b []byte, t time.Time) []byte {
h := t.Hour()
var am bool
if h == 0 {
b = append(b, '1')
b = append(b, '2')
am = true
} else {
switch {
case h == 12:
// no op
case h > 12:
h = h - 12
default:
am = true
}
b = unrollTwoDigits(b, h)
}
b = append(b, ':')
b = unrollTwoDigits(b, t.Minute())
b = append(b, ':')
b = unrollTwoDigits(b, t.Second())
b = append(b, ' ')
if am {
b = append(b, 'A')
} else {
b = append(b, 'P')
}
b = append(b, 'M')
return b
}