forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutputer.go
executable file
·547 lines (506 loc) · 15.4 KB
/
outputer.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
package carbon
import (
"bytes"
"strconv"
"strings"
)
// ToTimestamp outputs a timestamp with second(will be removed from v2.0, only keep Timestamp).
// 输出秒级时间戳(将在v2.0版本移除,只保留 Timestamp)
func (c Carbon) ToTimestamp() int64 {
if c.IsInvalid() {
return 0
}
return c.Timestamp()
}
// ToTimestampWithSecond outputs a timestamp with second(will be removed from v2.0, only keep TimestampWithSecond).
// 输出秒级时间戳(将在v2.0版本移除,只保留 TimestampWithSecond)
func (c Carbon) ToTimestampWithSecond() int64 {
if c.IsInvalid() {
return 0
}
return c.TimestampWithSecond()
}
// ToTimestampWithMillisecond outputs a timestamp with millisecond(will be removed from v2.0, only keep TimestampWithMillisecond).
// 输出毫秒级时间戳(将在v2.0版本移除,只保留 TimestampWithMillisecond)
func (c Carbon) ToTimestampWithMillisecond() int64 {
if c.IsInvalid() {
return 0
}
return c.TimestampWithMillisecond()
}
// ToTimestampWithMicrosecond outputs a timestamp with microsecond(will be removed from v2.0, only keep TimestampWithMicrosecond).
// 输出微秒级时间戳(将在v2.0版本移除,只保留 TimestampWithMicrosecond)
func (c Carbon) ToTimestampWithMicrosecond() int64 {
if c.IsInvalid() {
return 0
}
return c.TimestampWithMicrosecond()
}
// ToTimestampWithNanosecond outputs a timestamp with nanosecond(will be removed from v2.0, only keep TimestampWithNanosecond).
// 输出纳秒级时间戳(将在v2.0版本移除,只保留 TimestampWithNanosecond)
func (c Carbon) ToTimestampWithNanosecond() int64 {
if c.IsInvalid() {
return 0
}
return c.TimestampWithNanosecond()
}
// String outputs a string in date and time format, implement Stringer interface.
// 实现 Stringer 接口
func (c Carbon) String() string {
if c.IsInvalid() {
return ""
}
return c.ToDateTimeString()
}
// ToString outputs a string in "2006-01-02 15:04:05.999999999 -0700 MST" format.
// 输出"2006-01-02 15:04:05.999999999 -0700 MST"格式字符串
func (c Carbon) ToString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).String()
}
// ToMonthString outputs a string in month format, i18n is supported.
// 输出完整月份字符串,支持i18n
func (c Carbon) ToMonthString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
if len(c.lang.resources) == 0 {
c.lang.SetLocale(defaultLocale)
}
if months, ok := c.lang.resources["months"]; ok {
slice := strings.Split(months, "|")
if len(slice) == 12 {
return slice[c.Month()-1]
}
}
return ""
}
// ToShortMonthString outputs a string in short month format, i18n is supported.
// 输出缩写月份字符串,支持i18n
func (c Carbon) ToShortMonthString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
if len(c.lang.resources) == 0 {
c.lang.SetLocale(defaultLocale)
}
if months, ok := c.lang.resources["short_months"]; ok {
slice := strings.Split(months, "|")
if len(slice) == 12 {
return slice[c.Month()-1]
}
}
return ""
}
// ToWeekString outputs a string in week format, i18n is supported.
// 输出完整星期字符串,支持i18n
func (c Carbon) ToWeekString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
if len(c.lang.resources) == 0 {
c.lang.SetLocale(defaultLocale)
}
if months, ok := c.lang.resources["weeks"]; ok {
slice := strings.Split(months, "|")
if len(slice) == 7 {
return slice[c.Week()]
}
}
return ""
}
// ToShortWeekString outputs a string in short week format, i18n is supported.
// 输出缩写星期字符串,支持i18n
func (c Carbon) ToShortWeekString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
if len(c.lang.resources) == 0 {
c.lang.SetLocale(defaultLocale)
}
if months, ok := c.lang.resources["short_weeks"]; ok {
slice := strings.Split(months, "|")
if len(slice) == 7 {
return slice[c.Week()]
}
}
return ""
}
// ToDayDateTimeString outputs a string in day, date and time format.
// 输出天数日期时间字符串
func (c Carbon) ToDayDateTimeString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).Format(DayDateTimeFormat)
}
// ToDateTimeString outputs a string in date and time format.
// 输出日期时间字符串
func (c Carbon) ToDateTimeString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).Format(DateTimeFormat)
}
// ToShortDateTimeString outputs a string in short date and time format.
// 输出简写日期时间字符串
func (c Carbon) ToShortDateTimeString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).Format(ShortDateTimeFormat)
}
// ToDateString outputs a string in date format.
// 输出日期字符串
func (c Carbon) ToDateString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).Format(DateFormat)
}
// ToShortDateString outputs a string in short date format.
// 输出简写日期字符串
func (c Carbon) ToShortDateString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).Format(ShortDateFormat)
}
// ToTimeString outputs a string in time format.
// 输出时间字符串
func (c Carbon) ToTimeString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).Format(TimeFormat)
}
// ToShortTimeString outputs a string in short time format.
// 输出简写时间字符串
func (c Carbon) ToShortTimeString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).Format(ShortTimeFormat)
}
// ToAtomString outputs a string in ATOM format.
// 输出 ATOM 格式字符串
func (c Carbon) ToAtomString(timezone ...string) string {
if c.IsInvalid() {
return ""
}
return c.ToRfc3339String(timezone...)
}
// ToAnsicString outputs a string in ANSIC format.
// 输出 ANSIC 格式字符串
func (c Carbon) ToAnsicString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).Format(AnsicFormat)
}
// ToCookieString outputs a string in COOKIE format.
// 输出 COOKIE 格式字符串
func (c Carbon) ToCookieString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).Format(CookieFormat)
}
// ToRssString outputs a string in RSS format.
// 输出 RSS 格式字符串
func (c Carbon) ToRssString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).Format(RssFormat)
}
// ToW3cString outputs a string in W3C format.
// 输出 W3C 格式字符串
func (c Carbon) ToW3cString(timezone ...string) string {
if c.IsInvalid() {
return ""
}
return c.ToRfc3339String(timezone...)
}
// ToUnixDateString outputs a string in unix date format.
// 输出 UnixDate 格式字符串
func (c Carbon) ToUnixDateString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).Format(UnixDateFormat)
}
// ToRubyDateString outputs a string in ruby date format.
// 输出 RubyDate 格式字符串
func (c Carbon) ToRubyDateString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).Format(RubyDateFormat)
}
// ToKitchenString outputs a string in KITCHEN format.
// 输出 KITCHEN 格式字符串
func (c Carbon) ToKitchenString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).Format(KitchenFormat)
}
// ToIso8601String outputs a string in ISO8601 format.
// 输出 ISO8601 格式字符串
func (c Carbon) ToIso8601String(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).Format(Iso8601Format)
}
// ToRfc822String outputs a string in RFC822 format.
// 输出 RFC822 格式字符串
func (c Carbon) ToRfc822String(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).Format(RFC822Format)
}
// ToRfc822zString outputs a string in RFC822Z format.
// 输出 RFC822Z 格式字符串
func (c Carbon) ToRfc822zString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).Format(RFC822ZFormat)
}
// ToRfc850String outputs a string in RFC850 format.
// 输出 RFC850 格式字符串
func (c Carbon) ToRfc850String(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).Format(RFC850Format)
}
// ToRfc1036String outputs a string in RFC1036 format.
// 输出 RFC1036 格式字符串
func (c Carbon) ToRfc1036String(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).Format(RFC1036Format)
}
// ToRfc1123String outputs a string in RFC1123 format.
// 输出 RFC1123 格式字符串
func (c Carbon) ToRfc1123String(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).Format(RFC1123Format)
}
// ToRfc1123zString outputs a string in RFC1123z format.
// 输出 RFC1123z 格式字符串
func (c Carbon) ToRfc1123zString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).Format(RFC1123ZFormat)
}
// ToRfc2822String outputs a string in RFC2822 format.
// 输出 RFC2822 格式字符串
func (c Carbon) ToRfc2822String(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).Format(RFC2822Format)
}
// ToRfc3339String outputs a string in RFC3339 format.
// 输出 RFC3339 格式字符串
func (c Carbon) ToRfc3339String(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).Format(RFC3339Format)
}
// ToRfc7231String outputs a string in RFC7231 format.
// 输出 RFC7231 格式字符串
func (c Carbon) ToRfc7231String(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).Format(RFC7231Format)
}
// ToLayoutString outputs a string by layout.
// 输出指定布局的时间字符串
func (c Carbon) ToLayoutString(layout string, timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.loc).Format(layout)
}
// Layout outputs a string by layout, it is short for ToLayoutString.
// 输出指定布局的时间字符串, 是 ToLayoutString 的简写
func (c Carbon) Layout(layout string, timezone ...string) string {
if c.IsInvalid() {
return ""
}
return c.ToLayoutString(layout, timezone...)
}
// ToFormatString outputs a string by format.
// 输出指定格式的时间字符串
func (c Carbon) ToFormatString(format string, timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
buffer := bytes.NewBuffer(nil)
for i := 0; i < len(format); i++ {
if layout, ok := formats[byte(format[i])]; ok {
buffer.WriteString(c.Time.In(c.loc).Format(layout))
} else {
switch format[i] {
case '\\': // 原样输出,不解析
buffer.WriteByte(format[i+1])
i++
continue
case 'W': // ISO-8601 格式数字表示的年份中的第几周,取值范围 1-52
buffer.WriteString(strconv.Itoa(c.WeekOfYear()))
case 'N': // ISO-8601 格式数字表示的星期中的第几天,取值范围 1-7
buffer.WriteString(strconv.Itoa(c.DayOfWeek()))
case 'S': // 月份中第几天的英文缩写后缀,如st, nd, rd, th
suffix := "th"
switch c.Day() {
case 1, 21, 31:
suffix = "st"
case 2, 22:
suffix = "nd"
case 3, 23:
suffix = "rd"
}
buffer.WriteString(suffix)
case 'L': // 是否为闰年,如果是闰年为 1,否则为 0
if c.IsLeapYear() {
buffer.WriteString("1")
} else {
buffer.WriteString("0")
}
case 'G': // 数字表示的小时,24 小时格式,没有前导零,取值范围 0-23
buffer.WriteString(strconv.Itoa(c.Hour()))
case 'U': // 秒级时间戳,如 1611818268
buffer.WriteString(strconv.FormatInt(c.Timestamp(), 10))
case 'u': // 数字表示的毫秒,如 999
buffer.WriteString(strconv.Itoa(c.Millisecond()))
case 'w': // 数字表示的星期中的第几天,取值范围 0-6
buffer.WriteString(strconv.Itoa(c.DayOfWeek() - 1))
case 't': // 指定的月份有几天,取值范围 28-31
buffer.WriteString(strconv.Itoa(c.DaysInMonth()))
case 'z': // 年份中的第几天,取值范围 0-365
buffer.WriteString(strconv.Itoa(c.DayOfYear() - 1))
case 'e': // 当前位置,如 UTC,GMT,Atlantic/Azores
buffer.WriteString(c.Location())
case 'Q': // 当前季度,取值范围 1-4
buffer.WriteString(strconv.Itoa(c.Quarter()))
case 'C': // 当前世纪数,取值范围 0-99
buffer.WriteString(strconv.Itoa(c.Century()))
default:
buffer.WriteByte(format[i])
}
}
}
return buffer.String()
}
// Format outputs a string by format, it is short for ToFormatString.
// 输出指定格式的时间字符串, 是 ToFormatString 的简写
func (c Carbon) Format(format string, timezone ...string) string {
if c.IsInvalid() {
return ""
}
return c.ToFormatString(format, timezone...)
}