OSDN Git Service

add bytom logs into files
[bytom/bytom.git] / vendor / github.com / lestrrat-go / strftime / writer.go
1 package strftime
2
3 import (
4         "strconv"
5         "strings"
6         "time"
7 )
8
9 type appender interface {
10         Append([]byte, time.Time) []byte
11 }
12
13 type appenderList []appender
14
15 // does the time.Format thing
16 type timefmtw struct {
17         s string
18 }
19
20 func timefmt(s string) *timefmtw {
21         return &timefmtw{s: s}
22 }
23
24 func (v timefmtw) Append(b []byte, t time.Time) []byte {
25         return t.AppendFormat(b, v.s)
26 }
27
28 func (v timefmtw) str() string {
29         return v.s
30 }
31
32 func (v timefmtw) canCombine() bool {
33         return true
34 }
35
36 func (v timefmtw) combine(w combiner) appender {
37         return timefmt(v.s + w.str())
38 }
39
40 type verbatimw struct {
41         s string
42 }
43
44 func verbatim(s string) *verbatimw {
45         return &verbatimw{s: s}
46 }
47
48 func (v verbatimw) Append(b []byte, _ time.Time) []byte {
49         return append(b, v.s...)
50 }
51
52 func (v verbatimw) canCombine() bool {
53         return canCombine(v.s)
54 }
55
56 func (v verbatimw) combine(w combiner) appender {
57         if _, ok := w.(*timefmtw); ok {
58                 return timefmt(v.s + w.str())
59         }
60         return verbatim(v.s + w.str())
61 }
62
63 func (v verbatimw) str() string {
64         return v.s
65 }
66
67 // These words below, as well as any decimal character
68 var combineExclusion = []string{
69         "Mon",
70         "Monday",
71         "Jan",
72         "January",
73         "MST",
74         "PM",
75         "pm",
76 }
77
78 func canCombine(s string) bool {
79         if strings.ContainsAny(s, "0123456789") {
80                 return false
81         }
82         for _, word := range combineExclusion {
83                 if strings.Contains(s, word) {
84                         return false
85                 }
86         }
87         return true
88 }
89
90 type combiner interface {
91         canCombine() bool
92         combine(combiner) appender
93         str() string
94 }
95
96 type century struct{}
97
98 func (v century) Append(b []byte, t time.Time) []byte {
99         n := t.Year() / 100
100         if n < 10 {
101                 b = append(b, '0')
102         }
103         return append(b, strconv.Itoa(n)...)
104 }
105
106 type weekday int
107
108 func (v weekday) Append(b []byte, t time.Time) []byte {
109         n := int(t.Weekday())
110         if n < int(v) {
111                 n += 7
112         }
113         return append(b, byte(n+48))
114 }
115
116 type weeknumberOffset int
117
118 func (v weeknumberOffset) Append(b []byte, t time.Time) []byte {
119         yd := t.YearDay()
120         offset := int(t.Weekday()) - int(v)
121         if offset < 0 {
122                 offset += 7
123         }
124
125         if yd < offset {
126                 return append(b, '0', '0')
127         }
128
129         n := ((yd - offset) / 7) + 1
130         if n < 10 {
131                 b = append(b, '0')
132         }
133         return append(b, strconv.Itoa(n)...)
134 }
135
136 type weeknumber struct{}
137
138 func (v weeknumber) Append(b []byte, t time.Time) []byte {
139         _, n := t.ISOWeek()
140         if n < 10 {
141                 b = append(b, '0')
142         }
143         return append(b, strconv.Itoa(n)...)
144 }
145
146 type dayofyear struct{}
147
148 func (v dayofyear) Append(b []byte, t time.Time) []byte {
149         n := t.YearDay()
150         if n < 10 {
151                 b = append(b, '0', '0')
152         } else if n < 100 {
153                 b = append(b, '0')
154         }
155         return append(b, strconv.Itoa(n)...)
156 }
157
158 type hourwblank bool
159
160 func (v hourwblank) Append(b []byte, t time.Time) []byte {
161         h := t.Hour()
162         if bool(v) && h > 12 {
163                 h = h - 12
164         }
165         if h < 10 {
166                 b = append(b, ' ')
167         }
168         return append(b, strconv.Itoa(h)...)
169 }