-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
170 lines (160 loc) · 4 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
package timezh
import "bytes"
type format struct {
mixed bool
day uint8
dayPoint uint8
month uint8
monthPoint uint8
pm uint8
pmPoint uint8
buf *bytes.Buffer
}
func (f *format) init(s string) {
f.day = 0
f.dayPoint = 1
f.month = 0
f.monthPoint = 1
f.pm = 0
f.pmPoint = 1
f.reset()
}
func (f *format) reset() {
if f.buf == nil {
f.buf = new(bytes.Buffer)
} else {
f.buf.Reset()
}
}
// Format returns a textual representation of the time value formatted
// according to layout, which defines the format by showing how the reference
// time, defined to be
// Mon Jan 2 15:04:05 -0700 MST 2006
// would be displayed if it were the value; it serves as an example of the
// desired output. The same display rules will then be applied to the time
// value.
//
// A fractional second is represented by adding a period and zeros
// to the end of the seconds section of layout string, as in "15:04:05.000"
// to format a time stamp with millisecond precision.
//
// Predefined layouts ANSIC, UnixDate, RFC3339 and others describe standard
// and convenient representations of the reference time. For more information
// about the formats and the definition of the reference time, see the
// documentation for ANSIC and the other constants defined by this package.
func (t Time) Format(layout string) string {
t.f.mixed = false
return t.lookup(t.Time.Format(t.formatLayout(layout)))
}
// FormatMix the difference with Format is that it can be mixed in English and Chinese.
func (t Time) FormatMix(layout string) string {
t.f.mixed = true
return t.lookup(t.Time.Format(t.formatLayout(layout)))
}
// FormatLayout returns a standard textual formatted layout.
// Such as
// 星期一 一月 2 下午15:04:05 -0700 MST 2006
// returns
// Mon Jan 2 PM15:04:05 -0700 MST 2006
func FormatLayout(layout string) string {
return new(Time).formatLayout(layout)
}
func (t *Time) formatLayout(layout string) string {
t.f.init(layout)
j := 0
for i, r := range layout {
if i < j {
continue
}
switch r {
case 'M':
if len(layout) >= i+6 && layout[i:i+6] == "Monday" {
j = i + 6
if t.f.mixed {
t.f.day <<= 1
t.f.dayPoint <<= 1
}
t.f.buf.WriteString("Monday")
} else if len(layout) >= i+3 && layout[i:i+3] == "Mon" {
j = i + 3
if t.f.mixed {
t.f.day <<= 1
t.f.dayPoint <<= 1
}
t.f.buf.WriteString("Mon")
}
case 'J':
if len(layout) >= i+7 && layout[i:i+7] == "January" {
j = i + 7
if t.f.mixed {
t.f.month <<= 1
t.f.monthPoint <<= 1
}
t.f.buf.WriteString("January")
} else if len(layout) >= i+3 && layout[i:i+3] == "Jan" {
j = i + 3
if t.f.mixed {
t.f.month <<= 1
t.f.monthPoint <<= 1
}
t.f.buf.WriteString("Jan")
}
case 'P', 'p':
if len(layout) >= i+2 {
if layout[i:i+2] == "PM" || layout[i:i+2] == "pm" {
j = i + 2
if t.f.mixed {
t.f.pm <<= 1
t.f.pmPoint <<= 1
}
t.f.buf.WriteString("PM")
}
}
case '星':
if len(layout) >= i+9 && layout[i:i+9] == "星期一" {
j = i + 9
if t.f.mixed {
t.f.day = (t.f.day + 1) << 1
t.f.dayPoint <<= 1
}
t.f.buf.WriteString("Monday")
}
case '周':
if len(layout) >= i+6 && layout[i:i+6] == "周一" {
j = i + 6
if t.f.mixed {
t.f.day = (t.f.day + 1) << 1
t.f.dayPoint <<= 1
}
t.f.buf.WriteString("Mon")
}
case '一':
if len(layout) >= i+6 && layout[i:i+6] == "一月" {
j = i + 6
if t.f.mixed {
t.f.month = (t.f.month + 1) << 1
t.f.monthPoint <<= 1
}
t.f.buf.WriteString("Jan")
}
case '下':
if len(layout) >= i+6 && layout[i:i+6] == "下午" {
j = i + 6
if t.f.mixed {
t.f.pm = (t.f.pm + 1) << 1
t.f.pmPoint <<= 1
}
t.f.buf.WriteString("PM")
}
}
if j <= i {
t.f.buf.WriteRune(r)
}
}
return t.f.buf.String()
}
// FormatValue return a Chinese textual representation of the time value formatted
// according to layout.
func FormatValue(s string) string {
return new(Time).lookup(s)
}