-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
233 lines (209 loc) · 4.7 KB
/
format.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
package ptime
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/blackchip-org/zc/v5/pkg/ptime/locale"
)
var formatTable = map[string]func(*locale.Locale, string, time.Time) string{
"weekday": formatWeekday,
"year": formatYear,
"month": formatMonth,
"day": formatDay,
"hour": formatHour,
"minute": formatMinute,
"second": formatSecond,
"period": formatPeriod,
"zone": formatZone,
"offset": formatOffset,
"zone-offset": formatZoneOffset,
"offset-zone": formatOffsetZone,
}
const (
badField = "!(BADFIELD)"
badFormat = "!(BADFORMAT)"
)
func Format(loc *locale.Locale, layout string, t time.Time) string {
src := []rune(layout)
i := 0
var result strings.Builder
scanFormat := func() string {
var format strings.Builder
i++
for i < len(layout) {
if src[i] == ']' {
return format.String()
}
format.WriteRune(src[i])
i++
}
return format.String()
}
scanField := func() (string, string) {
var name strings.Builder
var format string
i++
for i < len(layout) {
if src[i] == '/' {
format = scanFormat()
break
}
if src[i] == ']' {
break
}
name.WriteRune(src[i])
i++
}
if src[i] != ']' {
return "", ""
}
return name.String(), format
}
for i < len(layout) {
if src[i] == '[' {
name, format := scanField()
fn, ok := formatTable[name]
if !ok {
result.WriteString(badField)
} else {
result.WriteString(fn(loc, format, t))
}
} else {
result.WriteRune(src[i])
}
i++
}
return strings.TrimSpace(result.String())
}
func formatWeekday(loc *locale.Locale, format string, t time.Time) string {
switch format {
case "", "wide":
return loc.DayNamesWide[t.Weekday()]
case "abbr":
return loc.DayNamesAbbr[t.Weekday()]
}
return badFormat
}
func formatYear(loc *locale.Locale, format string, t time.Time) string {
switch format {
case "":
return fmt.Sprintf("%04d", t.Year())
case "2":
return fmt.Sprintf("%02d", t.Year()%100)
}
return badFormat
}
func formatMonth(loc *locale.Locale, format string, t time.Time) string {
switch format {
case "":
return strconv.Itoa(int(t.Month()))
case "2":
return fmt.Sprintf("%2d", int(t.Month()))
case "02":
return fmt.Sprintf("%02d", int(t.Month()))
case "abbr":
return loc.MonthNamesAbbr[int(t.Month())-1]
case "wide", "name":
return loc.MonthNamesWide[int(t.Month())-1]
}
return badFormat
}
func formatDay(loc *locale.Locale, format string, t time.Time) string {
switch format {
case "":
return strconv.Itoa(t.Day())
case "2":
return fmt.Sprintf("%2d", t.Day())
case "02":
return fmt.Sprintf("%02d", t.Day())
case "year":
return fmt.Sprintf("%03d", t.YearDay())
}
return badFormat
}
func formatHour(loc *locale.Locale, format string, t time.Time) string {
switch format {
case "", "24":
return strconv.Itoa(t.Hour())
case "12":
h := t.Hour()
if h > 12 {
h = h - 12
}
if h == 0 {
h = 12
}
return strconv.Itoa(h)
}
return badFormat
}
func formatMinute(loc *locale.Locale, format string, t time.Time) string {
switch format {
case "":
return fmt.Sprintf("%02d", t.Minute())
}
return badFormat
}
func formatSecond(loc *locale.Locale, format string, t time.Time) string {
switch format {
case "", "0":
return fmt.Sprintf("%02d", t.Second())
}
_, err := strconv.Atoi(format)
if err != nil {
return badFormat
}
s := float64(t.Second()) + (float64(t.Nanosecond()) * 1e-9)
spec := "%." + format + "f"
return fmt.Sprintf(spec, s)
}
func formatPeriod(loc *locale.Locale, format string, t time.Time) string {
period := locale.AM
if t.Hour() >= 12 {
period = locale.PM
}
switch format {
case "", "abbr":
return loc.PeriodNamesAbbr.Main(period)
case "alt", "abbr-alt":
return loc.PeriodNamesAbbr.Alt(period)
case "narrow":
return loc.PeriodNamesNarrow.Main(period)
}
return badFormat
}
func formatZone(loc *locale.Locale, format string, t time.Time) string {
zone, _ := t.Zone()
switch format {
case "":
return zone
}
return badFormat
}
func formatOffset(loc *locale.Locale, format string, t time.Time) string {
_, offset := t.Zone()
switch format {
case "":
return FormatOffset(offset, "")
case ":":
return FormatOffset(offset, ":")
}
return badFormat
}
func formatZoneOffset(loc *locale.Locale, format string, t time.Time) string {
zone := formatZone(loc, "", t)
offset := formatOffset(loc, format, t)
if o, ok := loc.Offsets[loc.Key(zone)]; ok && o == 0 {
return zone
}
return zone + " " + offset
}
func formatOffsetZone(loc *locale.Locale, format string, t time.Time) string {
zone := formatZone(loc, "", t)
offset := formatOffset(loc, format, t)
if o, ok := loc.Offsets[loc.Key(zone)]; ok && o == 0 {
return zone
}
return offset + " " + zone
}