This repository has been archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
strftime_pure.go
277 lines (267 loc) · 8.97 KB
/
strftime_pure.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
package strftime
import (
"strconv"
"time"
)
// StrftimePure is a locale-unaware implementation of strftime(3). It does not
// correctly account for locale-specific conversion specifications, so formats
// like `%c` may behave differently from the underlying platform. Additionally,
// the `%E` and `%O` modifiers are passed through as raw strings.
//
// The implementation of locale-specific conversions attempts to mirror the
// strftime(3) implementation in glibc 2.15 under LC_TIME=C.
func StrftimePure(format string, t time.Time) string {
buf := make([]byte, 0, 512)
for i := 0; i < len(format); {
c := format[i]
if c != '%' {
buf = append(buf, c)
i++
continue
}
i++
if i == len(format) {
buf = append(buf, '%')
break
}
b := format[i]
switch b {
default:
buf = append(buf, '%', b)
case 'a':
// The abbreviated weekday name according to the current locale.
buf = append(buf, t.Format("Mon")...)
case 'A':
// The full weekday name according to the current locale.
buf = append(buf, t.Format("Monday")...)
case 'b':
// The abbreviated month name according to the current locale.
buf = append(buf, t.Format("Jan")...)
case 'B':
// The full month name according to the current locale.
buf = append(buf, t.Month().String()...)
case 'C':
// The century number (year/100) as a 2-digit integer. (SU)
buf = zero2d(buf, int(t.Year())/100)
case 'c':
// The preferred date and time representation for the current locale.
buf = append(buf, t.Format("Mon Jan 2 15:04:05 2006")...)
case 'd':
// The day of the month as a decimal number (range 01 to 31).
buf = zero2d(buf, t.Day())
case 'D':
// Equivalent to %m/%d/%y. (Yecch—for Americans only. Americans should note that in other countries %d/%m/%y is rather com‐ mon. This means that in international context this format is ambiguous and should not be used.) (SU)
buf = zero2d(buf, int(t.Month()))
buf = append(buf, '/')
buf = zero2d(buf, t.Day())
buf = append(buf, '/')
buf = zero2d(buf, t.Year()%100)
case 'E':
// Modifier: use alternative format, see below. (SU)
if i+1 < len(format) {
i++
buf = append(buf, '%', 'E', format[i])
} else {
buf = append(buf, "%E"...)
}
case 'e':
// Like %d, the day of the month as a decimal number, but a leading zero is replaced by a space. (SU)
buf = twoD(buf, t.Day())
case 'F':
// Equivalent to %Y-%m-%d (the ISO 8601 date format). (C99)
buf = zero4d(buf, t.Year())
buf = append(buf, '-')
buf = zero2d(buf, int(t.Month()))
buf = append(buf, '-')
buf = zero2d(buf, t.Day())
case 'G':
// The ISO 8601 week-based year (see NOTES) with century as a decimal number. The 4-digit year corresponding to the ISO week number (see %V). This has the same format and value as %Y, except that if the ISO week number belongs to the previous or next year, that year is used instead. (TZ)
year, _ := t.ISOWeek()
buf = zero4d(buf, year)
case 'g':
// Like %G, but without century, that is, with a 2-digit year (00-99). (TZ)
year, _ := t.ISOWeek()
buf = zero2d(buf, year%100)
case 'h':
// Equivalent to %b. (SU)
buf = append(buf, t.Format("Jan")...)
case 'H':
// The hour as a decimal number using a 24-hour clock (range 00 to 23).
buf = zero2d(buf, t.Hour())
case 'I':
// The hour as a decimal number using a 12-hour clock (range 01 to 12).
buf = zero2d(buf, t.Hour()%12)
case 'j':
// The day of the year as a decimal number (range 001 to 366).
buf = zero3d(buf, t.YearDay())
case 'k':
// The hour (24-hour clock) as a decimal number (range 0 to 23); single digits are preceded by a blank. (See also %H.) (TZ)
buf = twoD(buf, t.Hour())
case 'l':
// The hour (12-hour clock) as a decimal number (range 1 to 12); single digits are preceded by a blank. (See also %I.) (TZ)
buf = twoD(buf, t.Hour()%12)
case 'm':
// The month as a decimal number (range 01 to 12).
buf = zero2d(buf, int(t.Month()))
case 'M':
// The minute as a decimal number (range 00 to 59).
buf = zero2d(buf, t.Minute())
case 'n':
// A newline character. (SU)
buf = append(buf, '\n')
case 'O':
// Modifier: use alternative format, see below. (SU)
if i+1 < len(format) {
i++
buf = append(buf, '%', 'O', format[i])
} else {
buf = append(buf, "%O"...)
}
case 'p':
// Either "AM" or "PM" according to the given time value, or the corresponding strings for the current locale. Noon is treated as "PM" and midnight as "AM".
buf = appendAMPM(buf, t.Hour())
case 'P':
// Like %p but in lowercase: "am" or "pm" or a corresponding string for the current locale. (GNU)
buf = appendampm(buf, t.Hour())
case 'r':
// The time in a.m. or p.m. notation. In the POSIX locale this is equivalent to %I:%M:%S %p. (SU)
h := t.Hour()
buf = zero2d(buf, h%12)
buf = append(buf, ':')
buf = zero2d(buf, t.Minute())
buf = append(buf, ':')
buf = zero2d(buf, t.Second())
buf = append(buf, ' ')
buf = appendAMPM(buf, h)
case 'R':
// The time in 24-hour notation (%H:%M). (SU) For a version including the seconds, see %T below.
buf = zero2d(buf, t.Hour())
buf = append(buf, ':')
buf = zero2d(buf, t.Minute())
case 's':
// The number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). (TZ)
buf = strconv.AppendInt(buf, t.Unix(), 10)
case 'S':
// The second as a decimal number (range 00 to 60). (The range is up to 60 to allow for occasional leap seconds.)
buf = zero2d(buf, t.Second())
case 't':
// A tab character. (SU)
buf = append(buf, '\t')
case 'T':
// The time in 24-hour notation (%H:%M:%S). (SU)
buf = zero2d(buf, t.Hour())
buf = append(buf, ':')
buf = zero2d(buf, t.Minute())
buf = append(buf, ':')
buf = zero2d(buf, t.Second())
case 'u':
// The day of the week as a decimal, range 1 to 7, Monday being 1. See also %w. (SU)
day := byte(t.Weekday())
if day == 0 {
day = 7
}
buf = append(buf, '0'+day)
case 'U':
// The week number of the current year as a decimal number, range 00 to 53, starting with the first Sunday as the first day of week 01. See also %V and %W.
buf = zero2d(buf, (t.YearDay()-int(t.Weekday())+7)/7)
case 'V':
// The ISO 8601 week number (see NOTES) of the current year as a decimal number, range 01 to 53, where week 1 is the first week that has at least 4 days in the new year. See also %U and %W. (SU)
_, week := t.ISOWeek()
buf = zero2d(buf, week)
case 'w':
// The day of the week as a decimal, range 0 to 6, Sunday being 0. See also %u.
buf = strconv.AppendInt(buf, int64(t.Weekday()), 10)
case 'W':
// The week number of the current year as a decimal number, range 00 to 53, starting with the first Monday as the first day of week 01.
buf = zero2d(buf, (t.YearDay()-(int(t.Weekday())-1+7)%7+7)/7)
case 'x':
// The preferred date representation for the current locale without the time.
buf = zero2d(buf, int(t.Month()))
buf = append(buf, '/')
buf = zero2d(buf, t.Day())
buf = append(buf, '/')
buf = zero2d(buf, t.Year()%100)
case 'X':
// The preferred time representation for the current locale without the date.
buf = zero2d(buf, t.Hour())
buf = append(buf, ':')
buf = zero2d(buf, t.Minute())
buf = append(buf, ':')
buf = zero2d(buf, t.Second())
case 'y':
// The year as a decimal number without a century (range 00 to 99).
buf = zero2d(buf, t.Year()%100)
case 'Y':
// The year as a decimal number including the century.
buf = zero4d(buf, t.Year())
case 'z':
// The +hhmm or -hhmm numeric timezone (that is, the hour and minute offset from UTC). (SU)
buf = append(buf, t.Format("-0700")...)
case 'Z':
// The timezone or name or abbreviation.
buf = append(buf, t.Format("MST")...)
case '+':
// The date and time in date(1) format. (TZ) (Not supported in glibc2.)
buf = append(buf, t.Format("Mon Jan _2 15:04:05 MST 2006")...)
case '%':
// A literal '%' character.
buf = append(buf, '%')
}
i++
}
return string(buf)
}
// helper function to append %2d ints
func twoD(p []byte, n int) []byte {
if n < 10 {
return append(p, ' ', '0'+byte(n))
}
return strconv.AppendInt(p, int64(n), 10)
}
// helper function to append %02d ints
func zero2d(p []byte, n int) []byte {
if n < 10 {
return append(p, '0', '0'+byte(n))
}
return strconv.AppendInt(p, int64(n), 10)
}
// helper function to append %03d ints
func zero3d(p []byte, n int) []byte {
switch {
case n < 10:
p = append(p, "00"...)
case n < 100:
p = append(p, '0')
}
return strconv.AppendInt(p, int64(n), 10)
}
// helper function to append %04d ints
func zero4d(p []byte, n int) []byte {
switch {
case n < 10:
p = append(p, "000"...)
case n < 100:
p = append(p, "00"...)
case n < 1000:
p = append(p, '0')
}
return strconv.AppendInt(p, int64(n), 10)
}
func appendampm(p []byte, h int) []byte {
var m string
if h < 12 {
m = "am"
} else {
m = "pm"
}
return append(p, m...)
}
func appendAMPM(p []byte, h int) []byte {
var m string
if h < 12 {
m = "AM"
} else {
m = "PM"
}
return append(p, m...)
}