Skip to content

Commit ed78f86

Browse files
committed
Add GoString and ToIso8601ZuluXXXString methods
1 parent 54898bf commit ed78f86

File tree

3 files changed

+271
-361
lines changed

3 files changed

+271
-361
lines changed

outputer.go

+85-16
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ func (c Carbon) String() string {
2121
return c.ToDateTimeString(c.Location())
2222
}
2323

24+
// GoString implements fmt.GoStringer and formats c to be printed in Go source code.
25+
// 实现 fmt.GoStringer 接口,并格式化 c 以在 Go 源代码中打印
26+
func (c Carbon) GoString() string {
27+
if c.IsInvalid() {
28+
return ""
29+
}
30+
return c.StdTime().GoString()
31+
}
32+
2433
// ToString outputs a string in "2006-01-02 15:04:05.999999999 -0700 MST" layout.
2534
// 输出 "2006-01-02 15:04:05.999999999 -0700 MST" 格式字符串
2635
func (c Carbon) ToString(timezone ...string) string {
@@ -569,6 +578,54 @@ func (c Carbon) ToIso8601NanoString(timezone ...string) string {
569578
return c.StdTime().Format(ISO8601NanoLayout)
570579
}
571580

581+
// ToIso8601ZuluString outputs a string in "2006-01-02T15:04:05Z" layout.
582+
// 输出 "2006-01-02T15:04:05Z" 格式字符串
583+
func (c Carbon) ToIso8601ZuluString(timezone ...string) string {
584+
if len(timezone) > 0 {
585+
c.loc, c.Error = getLocationByTimezone(timezone[0])
586+
}
587+
if c.IsInvalid() {
588+
return ""
589+
}
590+
return c.StdTime().Format(ISO8601ZuluLayout)
591+
}
592+
593+
// ToIso8601ZuluMilliString outputs a string in "2006-01-02T15:04:05.999Z" layout.
594+
// 输出 "2006-01-02T15:04:05.999Z" 格式字符串
595+
func (c Carbon) ToIso8601ZuluMilliString(timezone ...string) string {
596+
if len(timezone) > 0 {
597+
c.loc, c.Error = getLocationByTimezone(timezone[0])
598+
}
599+
if c.IsInvalid() {
600+
return ""
601+
}
602+
return c.StdTime().Format(ISO8601ZuluMilliLayout)
603+
}
604+
605+
// ToIso8601ZuluMicroString outputs a string in "2006-01-02T15:04:05.999999Z" layout.
606+
// 输出 "2006-01-02T15:04:05.999999Z" 格式字符串
607+
func (c Carbon) ToIso8601ZuluMicroString(timezone ...string) string {
608+
if len(timezone) > 0 {
609+
c.loc, c.Error = getLocationByTimezone(timezone[0])
610+
}
611+
if c.IsInvalid() {
612+
return ""
613+
}
614+
return c.StdTime().Format(ISO8601ZuluMicroLayout)
615+
}
616+
617+
// ToIso8601ZuluNanoString outputs a string in "2006-01-02T15:04:05.999999999Z" layout.
618+
// 输出 "2006-01-02T15:04:05.999999999Z" 格式字符串
619+
func (c Carbon) ToIso8601ZuluNanoString(timezone ...string) string {
620+
if len(timezone) > 0 {
621+
c.loc, c.Error = getLocationByTimezone(timezone[0])
622+
}
623+
if c.IsInvalid() {
624+
return ""
625+
}
626+
return c.StdTime().Format(ISO8601ZuluNanoLayout)
627+
}
628+
572629
// ToRfc822String outputs a string in "02 Jan 06 15:04 MST" layout.
573630
// 输出 "02 Jan 06 15:04 MST" 格式字符串
574631
func (c Carbon) ToRfc822String(timezone ...string) string {
@@ -713,27 +770,45 @@ func (c Carbon) ToRfc7231String(timezone ...string) string {
713770
return c.StdTime().Format(RFC7231Layout)
714771
}
715772

716-
// ToLayoutString outputs a string by layout.
717-
// 输出指定布局模板的时间字符串
718-
func (c Carbon) ToLayoutString(layout string, timezone ...string) string {
773+
// ToFormattedDateString outputs a string in "Jan 2, 2006" layout.
774+
// 输出 "Jan 2, 2006" 格式字符串
775+
func (c Carbon) ToFormattedDateString(timezone ...string) string {
719776
if len(timezone) > 0 {
720777
c.loc, c.Error = getLocationByTimezone(timezone[0])
721778
}
722779
if c.IsInvalid() {
723780
return ""
724781
}
725-
return c.StdTime().Format(layout)
782+
return c.StdTime().Format(FormattedDateLayout)
726783
}
727784

728-
// Layout outputs a string by layout, it is shorthand for ToLayoutString.
729-
// 输出指定布局模板的时间字符串, 是 ToLayoutString 的简写
785+
// ToFormattedDayDateString outputs a string in "Mon, Jan 2, 2006" layout.
786+
// 输出 "Jan 2, 2006" 格式字符串
787+
func (c Carbon) ToFormattedDayDateString(timezone ...string) string {
788+
if len(timezone) > 0 {
789+
c.loc, c.Error = getLocationByTimezone(timezone[0])
790+
}
791+
if c.IsInvalid() {
792+
return ""
793+
}
794+
return c.StdTime().Format(FormattedDayDateLayout)
795+
}
796+
797+
// Layout outputs a string by layout.
798+
// 输出指定布局模板的时间字符串
730799
func (c Carbon) Layout(layout string, timezone ...string) string {
731-
return c.ToLayoutString(layout, timezone...)
800+
if len(timezone) > 0 {
801+
c.loc, c.Error = getLocationByTimezone(timezone[0])
802+
}
803+
if c.IsInvalid() {
804+
return ""
805+
}
806+
return c.StdTime().Format(layout)
732807
}
733808

734-
// ToFormatString outputs a string by format.
809+
// Format outputs a string by format.
735810
// 输出指定格式模板的时间字符串
736-
func (c Carbon) ToFormatString(format string, timezone ...string) string {
811+
func (c Carbon) Format(format string, timezone ...string) string {
737812
if len(timezone) > 0 {
738813
c.loc, c.Error = getLocationByTimezone(timezone[0])
739814
}
@@ -824,16 +899,10 @@ func (c Carbon) ToFormatString(format string, timezone ...string) string {
824899
return buffer.String()
825900
}
826901

827-
// Format outputs a string by format, it is shorthand for ToFormatString.
828-
// 输出指定格式模板的时间字符串, 是 ToFormatString 的简写
829-
func (c Carbon) Format(format string, timezone ...string) string {
830-
return c.ToFormatString(format, timezone...)
831-
}
832-
833902
// Deprecated: it will be removed in the future, use StdTime instead.
834903
//
835904
// ToStdTime converts Carbon to standard time.Time.
836-
// 将 Carbon 转换成标准 time.Time,未来将移除,请用 StdTime 替换
905+
// 将 Carbon 转换成标准 time.Time
837906
func (c Carbon) ToStdTime() time.Time {
838907
return c.StdTime()
839908
}

outputer_bench_test.go

+36-8
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,34 @@ func BenchmarkCarbon_ToIso8601NanoString(b *testing.B) {
303303
}
304304
}
305305

306+
func BenchmarkCarbon_ToIso8601ZuluString(b *testing.B) {
307+
now := Now()
308+
for n := 0; n < b.N; n++ {
309+
now.ToIso8601ZuluString()
310+
}
311+
}
312+
313+
func BenchmarkCarbon_ToIso8601ZuluMilliString(b *testing.B) {
314+
now := Now()
315+
for n := 0; n < b.N; n++ {
316+
now.ToIso8601ZuluMilliString()
317+
}
318+
}
319+
320+
func BenchmarkCarbon_ToIso8601ZuluMicroString(b *testing.B) {
321+
now := Now()
322+
for n := 0; n < b.N; n++ {
323+
now.ToIso8601ZuluMicroString()
324+
}
325+
}
326+
327+
func BenchmarkCarbon_ToIso8601ZuluNanoString(b *testing.B) {
328+
now := Now()
329+
for n := 0; n < b.N; n++ {
330+
now.ToIso8601ZuluNanoString()
331+
}
332+
}
333+
306334
func BenchmarkCarbon_ToRfc822String(b *testing.B) {
307335
now := Now()
308336
for n := 0; n < b.N; n++ {
@@ -387,24 +415,24 @@ func BenchmarkCarbon_ToRfc7231String(b *testing.B) {
387415
}
388416
}
389417

390-
func BenchmarkCarbon_ToLayoutString(b *testing.B) {
418+
func BenchmarkCarbon_ToFormattedDateString(b *testing.B) {
391419
now := Now()
392420
for n := 0; n < b.N; n++ {
393-
now.ToLayoutString("2006-01-02", "2020-08-05")
421+
now.ToFormattedDateString()
394422
}
395423
}
396424

397-
func BenchmarkCarbon_Layout(b *testing.B) {
425+
func BenchmarkCarbon_ToFormattedDayDateString(b *testing.B) {
398426
now := Now()
399427
for n := 0; n < b.N; n++ {
400-
now.Layout("2006-01-02", "2020-08-05")
428+
now.ToFormattedDayDateString()
401429
}
402430
}
403431

404-
func BenchmarkCarbon_ToFormatString(b *testing.B) {
432+
func BenchmarkCarbon_Layout(b *testing.B) {
405433
now := Now()
406434
for n := 0; n < b.N; n++ {
407-
now.ToFormatString("2006-01-02", "Y-m-d")
435+
now.Layout("2006-01-02", "2020-08-05")
408436
}
409437
}
410438

@@ -415,9 +443,9 @@ func BenchmarkCarbon_Format(b *testing.B) {
415443
}
416444
}
417445

418-
func BenchmarkCarbon_ToStdTime(b *testing.B) {
446+
func BenchmarkCarbon_GoTime(b *testing.B) {
419447
now := Now()
420448
for n := 0; n < b.N; n++ {
421-
now.ToStdTime()
449+
now.GoString()
422450
}
423451
}

0 commit comments

Comments
 (0)