-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgostrftime.go
229 lines (215 loc) · 6.06 KB
/
gostrftime.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
// Copyright (c) 2014-2016 Eli Janssen
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
// Package gostrftime formats time.Time using strftime(3) conventions.
package gostrftime
import (
"bytes"
"fmt"
"strconv"
"strings"
"time"
)
func strftime(b *bytes.Buffer, c byte, t time.Time) error {
switch c {
case 'A':
b.WriteString(t.Weekday().String())
case 'a':
b.WriteString(t.Weekday().String()[:3])
case 'B':
b.WriteString(t.Month().String())
case 'b':
b.WriteString(t.Month().String()[:3])
case 'C':
fmt.Fprintf(b, "%02d", t.Year())
case 'D':
y, m, d := t.Date()
fmt.Fprintf(b, "%02d/%02d/%02d", m, d, y%100)
case 'd':
fmt.Fprintf(b, "%02d", t.Day())
case 'e':
fmt.Fprintf(b, "%2d", t.Day())
case 'F':
y, m, d := t.Date()
fmt.Fprintf(b, "%04d-%02d-%02d", y, m, d)
case 'f':
usec := t.Nanosecond() / 1e3
fmt.Fprintf(b, "%06d", int(usec))
case 'H':
fmt.Fprintf(b, "%02d", t.Hour())
case 'h':
b.WriteString(t.Month().String()[:3])
case 'I':
hr := t.Hour() % 12
if hr == 0 {
hr = 12
}
fmt.Fprintf(b, "%02d", hr)
case 'j':
fmt.Fprintf(b, "%03d", t.YearDay())
case 'k':
fmt.Fprintf(b, "%2d", t.Hour())
case 'L':
msec := t.Nanosecond() / 1e6
fmt.Fprintf(b, "%03d", int(msec))
case 'l':
hr := t.Hour() % 12
if hr == 0 {
hr = 12
}
fmt.Fprintf(b, "%2d", hr)
case 'M':
fmt.Fprintf(b, "%02d", t.Minute())
case 'm':
fmt.Fprintf(b, "%02d", t.Month())
case 'N':
fmt.Fprintf(b, "%09d", t.Nanosecond())
case 'n':
b.WriteByte('\n')
case 'P':
if t.Hour() < 12 {
b.WriteString("am")
} else {
b.WriteString("pm")
}
case 'p':
if t.Hour() < 12 {
b.WriteString("AM")
} else {
b.WriteString("PM")
}
case 'R':
h, m, _ := t.Clock()
fmt.Fprintf(b, "%02d:%02d", h, m)
case 'r':
h, m, s := t.Clock()
var tm string
if h < 12 {
tm = "AM"
} else {
tm = "PM"
}
hr := h % 12
if hr == 0 {
hr = 12
}
fmt.Fprintf(b, "%02d:%02d:%02d %s", hr, m, s, tm)
case 'S':
fmt.Fprintf(b, "%02d", t.Second())
case 's':
b.WriteString(strconv.FormatInt(t.Unix(), 10))
case 'T':
h, m, s := t.Clock()
fmt.Fprintf(b, "%02d:%02d:%02d", h, m, s)
case 't':
b.WriteByte('\t')
case 'v':
fmt.Fprintf(b, "%2d-%s-%04d", t.Day(), t.Month().String()[:3], t.Year())
case 'w':
fmt.Fprintf(b, "%d", t.Weekday())
case 'Y':
fmt.Fprintf(b, "%04d", t.Year())
case 'y':
fmt.Fprintf(b, "%02d", t.Year()%100)
case 'Z':
zone, _ := t.Zone()
b.WriteString(zone)
case 'z':
_, offset := t.Zone()
allMinutes := int(offset / 60)
fmt.Fprintf(b, "%+03d%02d", int(allMinutes/60), int(allMinutes%60))
default:
return fmt.Errorf("No valid replacement")
}
return nil
}
// Format returns a textual representation of the time value
// formatting according to format. format supports most of the formatting
// methods defined in strftime(3), minus the GNU libc extensions and POSIX
// locale extensions.
//
// List of accepted format expansion values:
// %A full weekday name (Sunday)
// %a abbreviated weekday name (Sun)
// %B full month name (September)
// %b abbreviated month name (Sep)
// %C (year / 100) as number. Single digits are preceded by zero (20)
// %D equivalent to %m/%d/%y (09/21/14)
// %d day of month as number. Single digits are preceded by zero (21)
// %e day of month as number. Single digits are preceded by a blank (21)
// %f microsecond as a six digit decimal number, zero-padded on the left (001234)
// %F equivalent to %Y-%m-%d (2014-09-21)
// %H the hour (24 hour clock) as a number. Single digits are preceded by zero (15)
// %h same as %b
// %I the hour (12 hour clock) as a number. Single digits are preceded by zero (03)
// %j the day of the year as a decimal number. Single digits are preced by zeros (264)
// %k the hour (24 hour clock) as a number. Single digits are preceded by a blank (15)
// %L millisecond as a three digit decimal number, zero-padded on the left (001)
// %l the hour (12 hour clock) as a number. Single digits are preceded by blank ( 3)
// %M the minute as a decimal number. Single digits are preceded by a zero (32)
// %m the month as a decimal number. Single digits are preceded by a zero (09)
// %N nanosecond as a 9 digit decimal number, zero-padded on the left (001234567)
// %n a newline (\n)
// %P am or pm as appropriate
// %p AM or PM as appropriate
// %R equivalent to %H:%M
// %r equivalent to %I:%M:%S %p
// %S the second as a number. Single digits are preceded by a zero (05)
// %s the number of seconds since the Epoch, UTC
// %T equivalent to %H:%M:%S
// %t a tab (\t)
// %v equivalent to %e-%b-%Y
// %w the weekday (Sunday as first day of the week) as a number. (0)
// %Y the year with century as a number (2014)
// %y year without century as a number. Single digits are preceded by zero (14)
// %Z time zone name (UTC)
// %z the time zone offset from UTC (-0700)
func Format(format string, t time.Time) string {
if strings.IndexByte(format, '%') < 0 {
return format
}
outBuf := &bytes.Buffer{}
rr := strings.NewReader(format)
for {
// avoid a tiny bit of overhead by using ReadByte instead of ReadRune.
// ReadRune does a comparison check against utf8.RuneSelf on every
// "read", and we only care about that check /right after/ seeing a
// '%', as we won't call format in that case.
r, err := rr.ReadByte()
if err != nil {
break
}
if r != '%' {
outBuf.WriteByte(r)
continue
}
nr, err := rr.ReadByte()
if err != nil {
// got a percent, but then end of string
// just append % and finish
outBuf.WriteByte('%')
break
}
if nr == '%' {
outBuf.WriteByte('%')
continue
} else if nr >= 0x80 {
// if part of a utf8 continuation (value >= utf8.RuneSelf, which is 0x80),
// then just write out and continue
outBuf.WriteByte('%')
outBuf.WriteByte(nr)
continue
}
err = strftime(outBuf, nr, t)
if err != nil {
outBuf.WriteByte('%')
outBuf.WriteByte(nr)
continue
}
}
return outBuf.String()
}
// Strftime is an alias for Format
func Strftime(format string, t time.Time) string {
return Format(format, t)
}