-
Notifications
You must be signed in to change notification settings - Fork 1
/
datebin.go
304 lines (251 loc) · 5.08 KB
/
datebin.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package datebin
import (
"errors"
"time"
)
var (
// 解析的格式字符
// parse format list
PaseFormats = map[string]string{
"D": "Mon",
"d": "02",
"N": "Monday",
"j": "2",
"l": "Monday",
"z": "__2",
"F": "January",
"m": "01",
"M": "Jan",
"n": "1",
"Y": "2006",
"y": "06",
"a": "pm",
"A": "PM",
"g": "3",
"G": "=G=15",
"h": "03",
"H": "15",
"i": "04",
"s": "05",
"u": "000000",
"O": "-0700",
"P": "-07:00",
"T": "MST",
"c": "2006-01-02T15:04:05Z07:00",
"r": "Mon, 02 Jan 2006 15:04:05 -0700",
}
// 输出的格式字符
// output format list
ToFormats = map[string]string{
"D": "Mon",
"d": "02",
"j": "2",
"l": "Monday",
"F": "January",
"m": "01",
"M": "Jan",
"n": "1",
"Y": "2006",
"y": "06",
"a": "pm",
"A": "PM",
"g": "3",
"h": "03",
"H": "15",
"i": "04",
"s": "05",
"O": "-0700",
"P": "-07:00",
"T": "MST",
"c": "2006-01-02T15:04:05Z07:00",
"r": "Mon, 02 Jan 2006 15:04:05 -0700",
}
// 月份
// Month list
Months = map[int]time.Month{
1: time.January,
2: time.February,
3: time.March,
4: time.April,
5: time.May,
6: time.June,
7: time.July,
8: time.August,
9: time.September,
10: time.October,
11: time.November,
12: time.December,
}
// 周列表
// Weekday list
Weekdays = []string{
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
}
)
// 默认
// default Datebin
var defaultDatebin = NewDatebin()
/**
* 日期 / Datebin
*
* @create 2022-3-6
* @author deatil
*/
type Datebin struct {
// 时间 / Time
time time.Time
// 周开始 / week Start
weekStartAt time.Weekday
// 时区 / Location
loc *time.Location
// 错误 / error list
Errors []error
}
// New Datebin
func NewDatebin() Datebin {
return Datebin{
loc: time.Local,
weekStartAt: time.Monday,
}
}
// New Datebin
func New() Datebin {
return NewDatebin()
}
// 设置时间
// set Time
func (this Datebin) WithTime(time time.Time) Datebin {
this.time = time
return this
}
// 获取时间
// Get Time
func (this Datebin) GetTime() time.Time {
return this.time
}
// 设置周开始时间
// set Start Week
func (this Datebin) WithWeekStartAt(weekday time.Weekday) Datebin {
this.weekStartAt = weekday
return this
}
// 获取周开始时间
// Get Start Week
func (this Datebin) GetWeekStartAt() time.Weekday {
return this.weekStartAt
}
// 设置时区
// set Location struct
func (this Datebin) WithLocation(loc *time.Location) Datebin {
this.loc = loc
return this
}
// 获取时区
// Get Location struct
func (this Datebin) GetLocation() *time.Location {
return this.loc
}
// 获取时区字符
// Get Location String
func (this Datebin) GetLocationString() string {
return this.loc.String()
}
// 设置时区
// Set Timezone
func (this Datebin) SetTimezone(timezone string) Datebin {
loc, err := time.LoadLocation(timezone)
if err != nil {
return this.AppendError(err)
}
this.loc = loc
return this
}
// 全局设置时区
// Set global Timezone
func SetTimezone(timezone string) {
defaultDatebin = defaultDatebin.SetTimezone(timezone)
}
// 获取时区 Zone 名称
// Get Timezone string
func (this Datebin) GetTimezone() string {
name, _ := this.time.Zone()
return name
}
// 获取距离UTC时区的偏移量,单位秒
// Get Zone Offset
func (this Datebin) GetOffset() int {
_, offset := this.time.Zone()
return offset
}
// 设置 UTC
// set UTC timezone
func (this Datebin) UTC() Datebin {
this.loc = time.UTC
return this
}
// 设置 Local
// set Local timezone
func (this Datebin) Local() Datebin {
this.loc = time.Local
return this
}
// FixedZone 设置时区
// FixedZone returns a Location that always uses
// the given zone name and offset (seconds east of UTC).
func (this Datebin) FixedZone(name string, offset int) Datebin {
this.loc = time.FixedZone(name, offset)
return this
}
// 覆盖错误信息
// set Errors
func (this Datebin) WithErrors(errs []error) Datebin {
this.Errors = errs
return this
}
// 获取错误信息
// Get Errors
func (this Datebin) GetErrors() []error {
return this.Errors
}
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (this Datebin) MarshalBinary() ([]byte, error) {
enc := []byte{
byte(this.weekStartAt),
}
tt := this.time.In(this.loc)
timeBytes, err := tt.MarshalBinary()
if err != nil {
return nil, err
}
enc = append(enc, timeBytes...)
return enc, nil
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (this *Datebin) UnmarshalBinary(data []byte) error {
buf := data
if len(buf) == 0 {
return errors.New("Datebin.UnmarshalBinary: no data")
}
weekStartAt := buf[0]
err := (&this.time).UnmarshalBinary(buf[1:])
if err != nil {
return err
}
this.loc = this.time.Location()
this.weekStartAt = time.Weekday(weekStartAt)
return nil
}
// GobEncode implements the gob.GobEncoder interface.
func (this Datebin) GobEncode() ([]byte, error) {
return this.MarshalBinary()
}
// GobDecode implements the gob.GobDecoder interface.
func (this *Datebin) GobDecode(data []byte) error {
return this.UnmarshalBinary(data)
}