-
Notifications
You must be signed in to change notification settings - Fork 1
/
to.go
561 lines (476 loc) · 16.1 KB
/
to.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
package datebin
import (
"bytes"
"fmt"
"strconv"
"time"
)
// 默认返回
// output string
func (this Datebin) String() string {
return this.ToDatetimeString()
}
// GoString implements fmt.GoStringer and formats t to be printed in Go source
// code.
func (this Datebin) GoString() string {
if this.IsInvalid() {
return ""
}
return this.time.In(this.loc).GoString()
}
// 输出标准时间
// output std time
func (this Datebin) ToStdTime(timezone ...string) time.Time {
if this.IsInvalid() {
return this.time
}
if len(timezone) > 0 {
this = this.SetTimezone(timezone[0])
}
return this.time.In(this.loc)
}
// 返回字符
// output string
func (this Datebin) ToString(timezone ...string) string {
if this.IsInvalid() {
return ""
}
if len(timezone) > 0 {
this = this.SetTimezone(timezone[0])
}
return this.time.In(this.loc).String()
}
// 返回星座名称
// output Star name
func (this Datebin) ToStarString(timezone ...string) string {
if this.IsInvalid() {
return ""
}
if len(timezone) > 0 {
this = this.SetTimezone(timezone[0])
}
// 月份和天数
month := this.Month()
day := this.Day()
// 星座英文名称
star := ""
switch {
// 摩羯座
case month == 12 && day >= 22, month == 1 && day <= 19:
star = "Capricorn"
// 水瓶座
case month == 1 && day >= 20, month == 2 && day <= 18:
star = "Aquarius"
// 双鱼座
case month == 2 && day >= 19, month == 3 && day <= 20:
star = "Pisces"
// 白羊座
case month == 3 && day >= 21, month == 4 && day <= 20:
star = "Aries"
// 金牛座
case month == 4 && day >= 21, month == 5 && day <= 20:
star = "Taurus"
// 双子座
case month == 5 && day >= 21, month == 6 && day <= 21:
star = "Gemini"
// 巨蟹座
case month == 6 && day >= 22, month == 7 && day <= 22:
star = "Cancer"
// 狮子座
case month == 7 && day >= 23, month == 8 && day <= 22:
star = "Leo"
// 处女座
case month == 8 && day >= 23, month == 9 && day <= 22:
star = "Virgo"
// 天秤座
case month == 9 && day >= 23, month == 10 && day <= 23:
star = "Libra"
// 天蝎座
case month == 10 && day >= 24, month == 11 && day <= 22:
star = "Scorpio"
// 射手座
case month == 11 && day >= 23, month == 12 && day <= 21:
star = "Sagittarius"
}
return star
}
// 返回当前季节,以气象划分
// output Season name
func (this Datebin) ToSeasonString(timezone ...string) string {
if this.IsInvalid() {
return ""
}
if len(timezone) > 0 {
this = this.SetTimezone(timezone[0])
}
// 月份
month := this.Month()
name := ""
switch {
// 春季
case month == 3 || month == 4 || month == 5:
name = "Spring"
// 夏季
case month == 6 || month == 7 || month == 8:
name = "Summer"
// 秋季
case month == 9 || month == 10 || month == 11:
name = "Autumn"
// 冬季
case month == 12 || month == 1 || month == 2:
name = "Winter"
}
return name
}
// 周几
// output Weekday name
func (this Datebin) ToWeekdayString(timezone ...string) string {
if this.IsInvalid() {
return ""
}
if len(timezone) > 0 {
this = this.SetTimezone(timezone[0])
}
weekday := this.Weekday()
return Weekdays[weekday]
}
// 使用布局字符输出时间格式
// output datetime with layout
func (this Datebin) Layout(layout string, timezone ...string) string {
if this.IsInvalid() {
return ""
}
if len(timezone) > 0 {
this = this.SetTimezone(timezone[0])
}
return this.time.In(this.loc).Format(layout)
}
// 使用布局字符输出时间格式
// output datetime with layout
func (this Datebin) ToLayoutString(layout string, timezone ...string) string {
return this.Layout(layout, timezone...)
}
// 输出格式化字符的时间字符串
// output datetime with sign
func (this Datebin) Format(layout string, timezone ...string) string {
if this.IsInvalid() {
return ""
}
if len(timezone) > 0 {
this = this.SetTimezone(timezone[0])
}
var buffer bytes.Buffer
// 字符解析
for i := 0; i < len(layout); i++ {
val, ok := ToFormats[layout[i:i+1]]
if ok {
buffer.WriteString(this.time.In(this.loc).Format(val))
} else {
switch layout[i] {
case '\\':
buffer.WriteByte(layout[i+1])
i++
continue
case 'W': // ISO-8601 格式数字表示的年份中的第几周,取值范围 1-52
buffer.WriteString(strconv.Itoa(this.WeekOfYear()))
case 'N': // ISO-8601 格式数字表示的星期中的第几天,取值范围 1-7
buffer.WriteString(strconv.Itoa(this.DayOfWeek()))
case 'S': // 月份中第几天的英文缩写后缀,如st, nd, rd, th
suffix := "th"
switch this.Day() {
case 1, 21, 31:
suffix = "st"
case 2, 22:
suffix = "nd"
case 3, 23:
suffix = "rd"
}
buffer.WriteString(suffix)
case 'G': // 数字表示的小时,24 小时格式,没有前导零
buffer.WriteString(strconv.Itoa(this.Hour()))
case 'U': // 秒级时间戳
buffer.WriteString(strconv.FormatInt(this.Timestamp(), 10))
case 'u': // 数字表示的微秒,补位为固定6位
buffer.WriteString(fmt.Sprintf("%06d", this.Microsecond()))
case 'w': // 数字表示的星期中的第几天
buffer.WriteString(strconv.Itoa(this.DayOfWeek()))
case 't': // 指定的月份有几天
buffer.WriteString(strconv.Itoa(this.DaysInMonth()))
case 'z': // 年份中的第几天
buffer.WriteString(strconv.Itoa(this.DayOfYear() - 1))
case 'e': // 当前位置
buffer.WriteString(this.GetLocationString())
case 'Q': // 当前季度
buffer.WriteString(strconv.Itoa(this.Quarter()))
case 'C': // 当前百年数
buffer.WriteString(strconv.Itoa(this.Century()))
case 'o': // 当前年数
buffer.WriteString(strconv.Itoa(this.Year()))
case 'L': // 是否为闰年
if this.IsLeapYear() {
buffer.WriteString("ly")
} else {
buffer.WriteString("nly")
}
default:
buffer.WriteByte(layout[i])
}
}
}
return buffer.String()
}
// 输出格式化字符的时间字符串
// output datetime with sign
func (this Datebin) ToFormatString(layout string, timezone ...string) string {
return this.Format(layout, timezone...)
}
// 输出 Ansic 格式字符串
// output datetime with Ansic layout
func (this Datebin) ToAnsicString(timezone ...string) string {
return this.ToLayoutString(AnsicFormat, timezone...)
}
// 输出 UnixDate 格式字符串
// output datetime with UnixDate layout
func (this Datebin) ToUnixDateString(timezone ...string) string {
return this.ToLayoutString(UnixDateFormat, timezone...)
}
// 输出 RubyDate 格式字符串
// output datetime with RubyDate layout
func (this Datebin) ToRubyDateString(timezone ...string) string {
return this.ToLayoutString(RubyDateFormat, timezone...)
}
// 输出 Kitchen 格式字符串
// output datetime with Kitchen layout
func (this Datebin) ToKitchenString(timezone ...string) string {
return this.ToLayoutString(KitchenFormat, timezone...)
}
// 输出 RFC822 格式字符串
// output datetime with RFC822 layout
func (this Datebin) ToRFC822String(timezone ...string) string {
return this.ToLayoutString(RFC822Format, timezone...)
}
// 输出 RFC822Z 格式字符串
// output datetime with RFC822Z layout
func (this Datebin) ToRFC822ZString(timezone ...string) string {
return this.ToLayoutString(RFC822ZFormat, timezone...)
}
// 输出 RFC850 格式字符串
// output datetime with RFC850 layout
func (this Datebin) ToRFC850String(timezone ...string) string {
return this.ToLayoutString(RFC850Format, timezone...)
}
// 输出 RFC1123 格式字符串
// output datetime with RFC1123 layout
func (this Datebin) ToRFC1123String(timezone ...string) string {
return this.ToLayoutString(RFC1123Format, timezone...)
}
// 输出 RFC1123Z 格式字符串
// output datetime with RFC1123Z layout
func (this Datebin) ToRFC1123ZString(timezone ...string) string {
return this.ToLayoutString(RFC1123ZFormat, timezone...)
}
// 输出 Rss 格式字符串
// output datetime with Rss layout
func (this Datebin) ToRssString(timezone ...string) string {
return this.ToLayoutString(RssFormat, timezone...)
}
// 输出 RFC2822 格式字符串
// output datetime with RFC2822 layout
func (this Datebin) ToRFC2822String(timezone ...string) string {
return this.ToLayoutString(RFC2822Format, timezone...)
}
// 输出 RFC3339 格式字符串
// output datetime with RFC3339 layout
func (this Datebin) ToRFC3339String(timezone ...string) string {
return this.ToLayoutString(RFC3339Format, timezone...)
}
// 输出 RFC3339Milli 格式字符串
// output datetime with RFC3339Milli layout
func (this Datebin) ToRFC3339MilliString(timezone ...string) string {
return this.ToLayoutString(RFC3339MilliFormat, timezone...)
}
// 输出 RFC3339Milli 格式字符串
// output datetime with RFC3339Milli layout
func (this Datebin) ToRFC3339MicroString(timezone ...string) string {
return this.ToLayoutString(RFC3339MicroFormat, timezone...)
}
// 输出 RFC3339Nano 格式字符串
// output datetime with RFC3339Nano layout
func (this Datebin) ToRFC3339NanoString(timezone ...string) string {
return this.ToLayoutString(RFC3339NanoFormat, timezone...)
}
// 输出 W3C 格式字符串
// output datetime with W3C layout
func (this Datebin) ToW3CString(timezone ...string) string {
return this.ToLayoutString(W3CFormat, timezone...)
}
// 输出 ATOM 格式字符串
// output datetime with ATOM layout
func (this Datebin) ToAtomString(timezone ...string) string {
return this.ToLayoutString(AtomFormat, timezone...)
}
// 输出 Stamp 格式字符串
// output datetime with Stamp layout
func (this Datebin) ToStampString(timezone ...string) string {
return this.ToLayoutString(StampFormat, timezone...)
}
// 输出 StampMilli 格式字符串
// output datetime with StampMilli layout
func (this Datebin) ToStampMilliString(timezone ...string) string {
return this.ToLayoutString(StampMilliFormat, timezone...)
}
// 输出 StampMicro 格式字符串
// output datetime with StampMicro layout
func (this Datebin) ToStampMicroString(timezone ...string) string {
return this.ToLayoutString(StampMicroFormat, timezone...)
}
// 输出 StampNano 格式字符串
// output datetime with StampNano layout
func (this Datebin) ToStampNanoString(timezone ...string) string {
return this.ToLayoutString(StampNanoFormat, timezone...)
}
// 输出 Cookie 格式字符串
// output datetime with Cookie layout
func (this Datebin) ToCookieString(timezone ...string) string {
return this.ToLayoutString(CookieFormat, timezone...)
}
// 输出 RFC1036 格式字符串
// output datetime with RFC1036 layout
func (this Datebin) ToRFC1036String(timezone ...string) string {
return this.ToLayoutString(RFC1036Format, timezone...)
}
// 输出 RFC7231 格式字符串
// output datetime with RFC7231 layout
func (this Datebin) ToRFC7231String(timezone ...string) string {
return this.ToLayoutString(RFC7231Format, timezone...)
}
// 输出 ISO8601 格式字符串
// output datetime with ISO8601 layout
func (this Datebin) ToISO8601String(timezone ...string) string {
return this.ToLayoutString(ISO8601Format, timezone...)
}
// 输出 ISO8601Milli 格式字符串
// output datetime with ISO8601Milli layout
func (this Datebin) ToISO8601MilliString(timezone ...string) string {
return this.ToLayoutString(ISO8601MilliFormat, timezone...)
}
// 输出 ISO8601Micro 格式字符串
// output datetime with ISO8601Micro layout
func (this Datebin) ToISO8601MicroString(timezone ...string) string {
return this.ToLayoutString(ISO8601MicroFormat, timezone...)
}
// 输出 ISO8601Nano 格式字符串
// output datetime with ISO8601Nano layout
func (this Datebin) ToISO8601NanoString(timezone ...string) string {
return this.ToLayoutString(ISO8601NanoFormat, timezone...)
}
// 输出 ISO8601Zulu 格式字符串
// output datetime with ISO8601Zulu layout
func (this Datebin) ToISO8601ZuluString(timezone ...string) string {
return this.ToLayoutString(ISO8601ZuluFormat, timezone...)
}
// 输出 ISO8601ZuluMilli 格式字符串
// output datetime with ISO8601ZuluMilli layout
func (this Datebin) ToISO8601ZuluMilliString(timezone ...string) string {
return this.ToLayoutString(ISO8601ZuluMilliFormat, timezone...)
}
// 输出 ISO8601ZuluMicro 格式字符串
// output datetime with ISO8601ZuluMicro layout
func (this Datebin) ToISO8601ZuluMicroString(timezone ...string) string {
return this.ToLayoutString(ISO8601ZuluMicroFormat, timezone...)
}
// 输出 ISO8601ZuluNano 格式字符串
// output datetime with ISO8601ZuluNano layout
func (this Datebin) ToISO8601ZuluNanoString(timezone ...string) string {
return this.ToLayoutString(ISO8601ZuluNanoFormat, timezone...)
}
// 输出 DayDateTime 格式字符串
// output datetime with DayDateTime layout
func (this Datebin) ToDayDateTimeString(timezone ...string) string {
return this.ToLayoutString(DayDateTimeFormat, timezone...)
}
// 输出 FormattedDate 格式字符串
// output datetime with FormattedDate layout
func (this Datebin) ToFormattedDateString(timezone ...string) string {
return this.ToLayoutString(FormattedDateFormat, timezone...)
}
// 输出 FormattedDayDate 格式字符串
// output datetime with FormattedDayDate layout
func (this Datebin) ToFormattedDayDateString(timezone ...string) string {
return this.ToLayoutString(FormattedDayDateFormat, timezone...)
}
// 输出 Datetime 格式字符串
// output datetime with Datetime layout
func (this Datebin) ToDatetimeString(timezone ...string) string {
return this.ToLayoutString(DatetimeFormat, timezone...)
}
// 输出 DatetimeNano 格式字符串
// output datetime with DatetimeNano layout
func (this Datebin) ToDatetimeNanoString(timezone ...string) string {
return this.ToLayoutString(DatetimeNanoFormat, timezone...)
}
// 输出 DatetimeMicro 格式字符串
// output datetime with DatetimeMicro layout
func (this Datebin) ToDatetimeMicroString(timezone ...string) string {
return this.ToLayoutString(DatetimeMicroFormat, timezone...)
}
// 输出 DatetimeMilli 格式字符串
// output datetime with DatetimeMilli layout
func (this Datebin) ToDatetimeMilliString(timezone ...string) string {
return this.ToLayoutString(DatetimeMilliFormat, timezone...)
}
// 输出日期 Date 格式字符串
// output datetime with Date layout
func (this Datebin) ToDateString(timezone ...string) string {
return this.ToLayoutString(DateFormat, timezone...)
}
// 输出时间 Time 格式字符串
// output datetime with Time layout
func (this Datebin) ToTimeString(timezone ...string) string {
return this.ToLayoutString(TimeFormat, timezone...)
}
// 输出时间 HourMinute 格式字符串
// output datetime with HourMinute layout
func (this Datebin) ToHourMinuteString(timezone ...string) string {
return this.ToLayoutString(HourMinuteFormat, timezone...)
}
// 输出时间 Hour 格式字符串
// output datetime with Hour layout
func (this Datebin) ToHourString(timezone ...string) string {
return this.ToLayoutString(HourFormat, timezone...)
}
// 输出 ShortDatetime 格式字符串
// output datetime with ShortDatetime layout
func (this Datebin) ToShortDatetimeString(timezone ...string) string {
return this.ToLayoutString(ShortDatetimeFormat, timezone...)
}
// 输出 ShortDatetimeNano 格式字符串
// output datetime with ShortDatetimeNano layout
func (this Datebin) ToShortDatetimeNanoString(timezone ...string) string {
return this.ToLayoutString(ShortDatetimeNanoFormat, timezone...)
}
// 输出 ShortDatetimeMicro 格式字符串
// output datetime with ShortDatetimeMicro layout
func (this Datebin) ToShortDatetimeMicroString(timezone ...string) string {
return this.ToLayoutString(ShortDatetimeMicroFormat, timezone...)
}
// 输出 ShortDatetimeMilli 格式字符串
// output datetime with ShortDatetimeMilli layout
func (this Datebin) ToShortDatetimeMilliString(timezone ...string) string {
return this.ToLayoutString(ShortDatetimeMilliFormat, timezone...)
}
// 输出 ShortDate 格式字符串
// output datetime with ShortDate layout
func (this Datebin) ToShortDateString(timezone ...string) string {
return this.ToLayoutString(ShortDateFormat, timezone...)
}
// 输出 ShortTime 格式字符串
// output datetime with ShortTime layout
func (this Datebin) ToShortTimeString(timezone ...string) string {
return this.ToLayoutString(ShortTimeFormat, timezone...)
}
// 输出 ShortHourMinute 格式字符串
// output datetime with ShortHourMinute layout
func (this Datebin) ToShortHourMinuteString(timezone ...string) string {
return this.ToLayoutString(ShortHourMinuteFormat, timezone...)
}