/
go_strftime.go
171 lines (156 loc) · 3.83 KB
/
go_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
package go_strftime
import(
"fmt"
"time"
"regexp"
)
func Strftime(layout string, t time.Time) string {
// Implemented most useful of http://www.cplusplus.com/reference/ctime/strftime/
// Skipped: %U, %W (not ISO week number) and modifiers E and O
// How to use : fmt.Println( Strftime("%Y-%m-%d", time.Now()) )
// Prepared data: time zone, utc offset, week based year, week number
time_zone, utc_offset := t.Zone()
minutes_offset := utc_offset/60
h := minutes_offset/60
m := minutes_offset%60
sign := "+"
if utc_offset<0{
sign = "-"
}
iso_offset := fmt.Sprintf("%v%v%02v", sign, h, m)
week_based_year, week_num := t.ISOWeek()
// Group patterns
patterns_regexp := regexp.MustCompile("%[DxcFrRTX]{1}")
patterns_positions := patterns_regexp.FindAllStringIndex(layout, 1000)
var pattern string;
var newstr string;
pos := 0
for _, p := range patterns_positions{
newstr += layout[pos:p[0]]
pos = p[1]
pattern = layout[p[0]:p[1]]
switch pattern{
case "%D", "%x":
newstr += "%m/%d/%y"
case "%c":
newstr += "%a %b %e %H:%M:%S %Y"
case "%F":
newstr += "%Y-%m-%d"
case "%r":
newstr += "%I:%M:%S %p"
case "%R":
newstr += "%H:%M"
case "%T", "%X":
newstr += "%H:%M"
default:
newstr += ""
}
}
if len(newstr) > 0{
newstr += layout[pos:]
layout = newstr
}
// Single patterns
patterns_regexp = regexp.MustCompile("%[ZzCjGgyYmBbhdeVAauwHIMSpnt%]{1}")
patterns_positions = patterns_regexp.FindAllStringIndex(layout, 1000)
newstr = ""
pos = 0
for _, p := range patterns_positions{
newstr += layout[pos:p[0]]
pos = p[1]
pattern = layout[p[0]:p[1]]
switch pattern{
case "%Z":
// Timezone
newstr += time_zone
case "%z":
// UTC offset
newstr += iso_offset
case "%C":
// Century
newstr += fmt.Sprintf("%v", int(t.Year()/100))
case "%j":
// Day of year
newstr += fmt.Sprintf("%03v", t.YearDay())
case "%G":
// Week based year (4 digits)
newstr += fmt.Sprintf("%04v", week_based_year)
case "%g":
// Week based year (last 2 digits)
newstr += fmt.Sprintf("%04v", week_based_year)[2:4]
case "%y":
// Year (last 2 digits)
newstr += fmt.Sprintf("%04v", t.Year())[2:4]
case "%Y":
// Year
newstr += fmt.Sprintf("%04v", t.Year())
case "%m":
// Month (2 digit)
newstr += fmt.Sprintf("%02v", int(t.Month()))
case "%B":
// Month full name
newstr += t.Month().String()
case "%b", "%h":
// Month short name
switch t.Month(){
case time.June, time.July, time.September:
newstr += t.Month().String()[:4]
default:
newstr += t.Month().String()[:3]
}
case "%d":
// Day of month (2 digit)
newstr += fmt.Sprintf("%02v", t.Day())
case "%e":
// Day of month
newstr += fmt.Sprintf("%v", t.Day())
case "%V":
// ISO 8601 week number
newstr += fmt.Sprintf("%v", week_num)
case "%A":
// Weekday
newstr += t.Weekday().String()
case "%a":
// Weekday short name
newstr += t.Weekday().String()[:3]
case "%u":
// Weekday number (1-7)
newstr += fmt.Sprintf("%v", int(t.Weekday()))
case "%w":
// Weekday numder (0-6)
newstr += fmt.Sprintf("%v", int(t.Weekday())-1)
case "%H":
// Hour (00-24)
newstr += fmt.Sprintf("%02v", t.Hour())
case "%I":
// Hour (00-12)
newstr += fmt.Sprintf("%02v", t.Hour() - (t.Hour()/12)*12)
case "%M":
// Minute (00-59)
newstr += fmt.Sprintf("%02v", t.Minute())
case "%S":
// Second (00-59)
newstr += fmt.Sprintf("%02v", t.Second())
case "%p":
// AM/PM
if t.Hour()/12 > 0{
newstr += "PM"
} else {
newstr += "AM"
}
case "%n":
newstr += "\n"
case "%t":
newstr += "\t"
case "%%":
newstr += "%"
default:
newstr += ""
}
}
if len(newstr) > 0{
newstr += layout[pos:]
layout = newstr
}
return layout
}