forked from takmongwai/go-strftime
/
strftime.go
213 lines (208 loc) · 5.11 KB
/
strftime.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
package strftime
import (
"fmt"
"strconv"
"strings"
"time"
)
func fslen(len int) string {
var lenrs []string
if len < 0 {
len = 1
}
if len > 9 {
len = 9
}
lenrs = append(lenrs, ".")
for lf := 0; lf < len; lf++ {
lenrs = append(lenrs, "9")
}
return strings.Join(lenrs, "")
}
// Strftime formats t to the directives in the given format string
// format is compatiable with ruby's Time#strftime
// see http://ruby-doc.org/core-2.2.2/Time.html#method-i-strftime
func Strftime(t *time.Time, format string) string {
var tFormat []string
app := func(f string, idx *int, step int) {
tFormat = append(tFormat, f)
*idx += step
}
b := []rune(format)
for i := 0; i < len(b); i++ {
if b[i] == '%' {
switch b[i+1] {
case '%': // litteral %
app("%", &i, 1)
continue
case 'Y', 'G': // year
app(t.Format("2006"), &i, 1)
continue
case 'C':
s, _ := strconv.Atoi(t.Format("2006"))
app(strconv.Itoa(s/100), &i, 1)
continue
case 'y', 'g':
app(t.Format("06"), &i, 1)
continue
case 'm': // month
app(t.Format("01"), &i, 1)
continue
case '_': // processing underlined with _m
if b[i+2] == 'm' {
app(fmt.Sprintf("%2s", t.Format("1")), &i, 2)
continue
}
case '-': // process - with -m, -d
if b[i+2] == 'm' {
app(t.Format("1"), &i, 2)
continue
} else if b[i+2] == 'd' {
app(t.Format("2"), &i, 2)
continue
}
case 'B':
app(t.Format("January"), &i, 1)
continue
case 'b', 'h':
app(t.Format("Jan"), &i, 1)
continue
case '^':
if b[i+2] == 'B' {
app(strings.ToUpper(t.Format("January")), &i, 2)
continue
} else if b[i+2] == 'b' {
app(strings.ToUpper(t.Format("Jan")), &i, 2)
continue
} else if b[i+2] == 'A' {
app(strings.ToUpper(t.Weekday().String()), &i, 2)
continue
} else if b[i+2] == 'a' {
app(strings.ToUpper(t.Weekday().String()[:3]), &i, 2)
continue
}
case 'd':
app(t.Format("02"), &i, 1)
continue
case 'e':
app(fmt.Sprintf("%2s", t.Format("2")), &i, 1)
continue
case 'j':
app(strconv.Itoa(t.YearDay()), &i, 1)
continue
case 'H':
app(t.Format("15"), &i, 1)
continue
case 'k':
hk, _ := strconv.Atoi(t.Format("15"))
app(fmt.Sprintf("%2s", strconv.Itoa(hk)), &i, 1)
continue
case 'I':
hki, _ := strconv.Atoi(t.Format("3"))
app(fmt.Sprintf("%02s", strconv.Itoa(hki)), &i, 1)
continue
case 'l':
hki, _ := strconv.Atoi(t.Format("3"))
app(fmt.Sprintf("%2s", strconv.Itoa(hki)), &i, 1)
continue
case 'P':
app(t.Format("pm"), &i, 1)
continue
case 'p':
app(t.Format("PM"), &i, 1)
continue
case 'M':
app(t.Format("04"), &i, 1)
continue
case 'S':
app(t.Format("05"), &i, 1)
continue
case 'L':
app(t.Format(".999"), &i, 1)
continue
case '1', '2', '3', '4', '5', '6', '7', '8', '9':
if b[i+2] == 'N' {
if l, err := strconv.Atoi(string(b[i+1])); err == nil {
app(t.Format(fslen(l)), &i, 2)
}
}
continue
case 'z':
app(t.Format("-0700"), &i, 1)
continue
case 'Z':
app(t.Format("MST"), &i, 1)
continue
case 'A':
app(t.Weekday().String(), &i, 1)
continue
case 'a':
app(t.Weekday().String()[:3], &i, 1)
continue
case 'w', 'u': // w sunday is 0,0..6 u monday is 1,1..7
wd := int(t.Weekday() - time.Sunday)
if wd == 0 && b[i+1] == 'u' {
app(strconv.Itoa(7), &i, 1)
} else {
app(strconv.Itoa(wd), &i, 1)
}
continue
case 'V', 'U': // todo(js): V is ISO8601. U is technically 0-based.
_, w := t.ISOWeek()
app(fmt.Sprintf("%02s", strconv.Itoa(w)), &i, 1)
continue
case 's':
app(strconv.FormatInt(t.Unix(), 10), &i, 1)
continue
case 'Q':
app(strconv.FormatInt(t.UnixNano()/1000000, 10), &i, 1)
continue
case 'n':
app("\n", &i, 1)
continue
case 't':
app("\t", &i, 1)
continue
// Combinations
case 'c': // %c - date and time (%a %b %e %T %Y)
app(fmt.Sprintf(
"%s %s %s %s:%s:%s %s",
t.Weekday().String()[:3],
t.Format("Jan"),
fmt.Sprintf("%2s", t.Format("2")),
t.Format("15"),
t.Format("04"),
t.Format("05"),
t.Format("2006")),
&i, 1)
continue
case 'D', 'x':
app(fmt.Sprintf("%s/%s/%s", t.Format("01"), t.Format("02"), t.Format("06")), &i, 1)
continue
case 'F': // %Y-%m-%d
app(fmt.Sprintf("%s-%s-%s", t.Format("2006"), t.Format("01"), t.Format("02")), &i, 1)
continue
case 'v': // %e-%b-%y
app(fmt.Sprintf("%s-%s-%s", fmt.Sprintf("%2s", t.Format("2")), t.Format("Jan"), t.Format("06")), &i, 1)
continue
case 'X', 'T':
app(fmt.Sprintf("%s:%s:%s", t.Format("15"), t.Format("04"), t.Format("05")), &i, 1)
continue
case 'r': // %r - 12-hour time (%I:%M:%S %p)
hki, _ := strconv.Atoi(t.Format("3"))
app(fmt.Sprintf("%02s:%s:%s %s",
strconv.Itoa(hki),
t.Format("04"),
t.Format("05"),
t.Format("PM")),
&i, 1)
continue
case 'R':
app(fmt.Sprintf("%s:%s", t.Format("15"), t.Format("04")), &i, 1)
continue
}
}
tFormat = append(tFormat, string(b[i]))
}
return strings.Join(tFormat, "")
}