diff --git a/README.cn.md b/README.cn.md index ca2f1ff0..2eb8f039 100644 --- a/README.cn.md +++ b/README.cn.md @@ -913,6 +913,7 @@ fmt.Sprintf("%s", carbon.Parse("2020-08-05 13:14:15")) // 2020-08-05 13:14:15 carbon.Parse("2020-08-05 13:14:15").ToString() // 2020-08-05 13:14:15.999999 +0800 CST // 输出指定布局的字符串,Layout()是ToLayoutString()的简写 +carbon.Parse("2020-08-05 13:14:15").Layout(carbon.ISO8601Layout) // 2020-08-05T13:14:15+08:00 carbon.Parse("2020-08-05 13:14:15").Layout("20060102150405") // 20200805131415 carbon.Parse("2020-08-05 13:14:15").Layout("2006年01月02日 15时04分05秒") // 2020年08月05日 13时14分15秒 carbon.Parse("2020-08-05 13:14:15").Layout("It is 2006-01-02 15:04:05") // It is 2020-08-05 13:14:15 diff --git a/README.jp.md b/README.jp.md index 2d659a8a..1cbda38e 100644 --- a/README.jp.md +++ b/README.jp.md @@ -909,6 +909,7 @@ fmt.Sprintf("%s", carbon.Parse("2020-08-05 13:14:15")) // 2020-08-05 13:14:15 carbon.Parse("2020-08-05 13:14:15").ToString() // 2020-08-05 13:14:15.999999 +0800 CST // レイアウトを指定する文字列を出力, Layout() は 是ToLayoutString() の略記です +carbon.Parse("2020-08-05 13:14:15").Layout(carbon.ISO8601Layout) // 2020-08-05T13:14:15+08:00 carbon.Parse("2020-08-05 13:14:15").Layout("20060102150405") // 20200805131415 carbon.Parse("2020-08-05 13:14:15").Layout("2006年01月02日 15时04分05秒") // 2020年08月05日 13时14分15秒 carbon.Parse("2020-08-05 13:14:15").Layout("It is 2006-01-02 15:04:05") // It is 2020-08-05 13:14:15 diff --git a/README.md b/README.md index 00095ce6..8c80c4e0 100644 --- a/README.md +++ b/README.md @@ -908,6 +908,7 @@ fmt.Sprintf("%s", carbon.Parse("2020-08-05 13:14:15")) // 2020-08-05 13:14:15 carbon.Parse("2020-08-05 13:14:15").ToString() // 2020-08-05 13:14:15.999999 +0800 CST // Output a string by layout, Layout() is shorthand for ToLayoutString() +carbon.Parse("2020-08-05 13:14:15").Layout(carbon.ISO8601Layout) // 2020-08-05T13:14:15+08:00 carbon.Parse("2020-08-05 13:14:15").Layout("20060102150405") // 20200805131415 carbon.Parse("2020-08-05 13:14:15").Layout("2006年01月02日 15时04分05秒") // 2020年08月05日 13时14分15秒 carbon.Parse("2020-08-05 13:14:15").Layout("It is 2006-01-02 15:04:05") // It is 2020-08-05 13:14:15 diff --git a/carbon.go b/carbon.go index 5b2f120a..351e221c 100644 --- a/carbon.go +++ b/carbon.go @@ -122,44 +122,44 @@ const ( SecondsPerMinute = 60 // 每分钟60秒 ) -// formats constant -// 时间格式化常量 +// layouts constant +// 时间布局常量 const ( - ANSICFormat = time.ANSIC - UnixDateFormat = time.UnixDate - RubyDateFormat = time.RubyDate - RFC822Format = time.RFC822 - RFC822ZFormat = time.RFC822Z - RFC850Format = time.RFC850 - RFC1123Format = time.RFC1123 - RFC1123ZFormat = time.RFC1123Z - RssFormat = time.RFC1123Z - KitchenFormat = time.Kitchen - RFC2822Format = time.RFC1123Z - CookieFormat = "Monday, 02-Jan-2006 15:04:05 MST" - RFC3339Format = "2006-01-02T15:04:05Z07:00" - RFC3339MilliFormat = "2006-01-02T15:04:05.999Z07:00" - RFC3339MicroFormat = "2006-01-02T15:04:05.999999Z07:00" - RFC3339NanoFormat = "2006-01-02T15:04:05.999999999Z07:00" - ISO8601Format = "2006-01-02T15:04:05-07:00" - ISO8601MilliFormat = "2006-01-02T15:04:05.999-07:00" - ISO8601MicroFormat = "2006-01-02T15:04:05.999999-07:00" - ISO8601NanoFormat = "2006-01-02T15:04:05.999999999-07:00" - RFC1036Format = "Mon, 02 Jan 06 15:04:05 -0700" - RFC7231Format = "Mon, 02 Jan 2006 15:04:05 GMT" - DayDateTimeFormat = "Mon, Jan 2, 2006 3:04 PM" - DateTimeFormat = "2006-01-02 15:04:05" - DateTimeMilliFormat = "2006-01-02 15:04:05.999" - DateTimeMicroFormat = "2006-01-02 15:04:05.999999" - DateTimeNanoFormat = "2006-01-02 15:04:05.999999999" - DateFormat = "2006-01-02" - TimeFormat = "15:04:05" - ShortDateTimeFormat = "20060102150405" - ShortDateTimeMilliFormat = "20060102150405.999" - ShortDateTimeMicroFormat = "20060102150405.999999" - ShortDateTimeNanoFormat = "20060102150405.999999999" - ShortDateFormat = "20060102" - ShortTimeFormat = "150405" + ANSICLayout = time.ANSIC + UnixDateLayout = time.UnixDate + RubyDateLayout = time.RubyDate + RFC822Layout = time.RFC822 + RFC822ZLayout = time.RFC822Z + RFC850Layout = time.RFC850 + RFC1123Layout = time.RFC1123 + RFC1123ZLayout = time.RFC1123Z + RssLayout = time.RFC1123Z + KitchenLayout = time.Kitchen + RFC2822Layout = time.RFC1123Z + CookieLayout = "Monday, 02-Jan-2006 15:04:05 MST" + RFC3339Layout = "2006-01-02T15:04:05Z07:00" + RFC3339MilliLayout = "2006-01-02T15:04:05.999Z07:00" + RFC3339MicroLayout = "2006-01-02T15:04:05.999999Z07:00" + RFC3339NanoLayout = "2006-01-02T15:04:05.999999999Z07:00" + ISO8601Layout = "2006-01-02T15:04:05-07:00" + ISO8601MilliLayout = "2006-01-02T15:04:05.999-07:00" + ISO8601MicroLayout = "2006-01-02T15:04:05.999999-07:00" + ISO8601NanoLayout = "2006-01-02T15:04:05.999999999-07:00" + RFC1036Layout = "Mon, 02 Jan 06 15:04:05 -0700" + RFC7231Layout = "Mon, 02 Jan 2006 15:04:05 GMT" + DayDateTimeLayout = "Mon, Jan 2, 2006 3:04 PM" + DateTimeLayout = "2006-01-02 15:04:05" + DateTimeMilliLayout = "2006-01-02 15:04:05.999" + DateTimeMicroLayout = "2006-01-02 15:04:05.999999" + DateTimeNanoLayout = "2006-01-02 15:04:05.999999999" + DateLayout = "2006-01-02" + TimeLayout = "15:04:05" + ShortDateTimeLayout = "20060102150405" + ShortDateTimeMilliLayout = "20060102150405.999" + ShortDateTimeMicroLayout = "20060102150405.999999" + ShortDateTimeNanoLayout = "20060102150405.999999999" + ShortDateLayout = "20060102" + ShortTimeLayout = "150405" ) // Carbon defines a Carbon struct. @@ -222,9 +222,9 @@ func (c Carbon) Tomorrow(timezone ...string) Carbon { } if c.IsZero() { c.time = c.Now().Carbon2Time().AddDate(0, 0, 1) - } else { - c.time = c.Carbon2Time().AddDate(0, 0, 1) + return c } + c.time = c.Carbon2Time().AddDate(0, 0, 1) return c } @@ -245,9 +245,9 @@ func (c Carbon) Yesterday(timezone ...string) Carbon { } if c.IsZero() { c.time = c.Now().Carbon2Time().AddDate(0, 0, -1) - } else { - c.time = c.Carbon2Time().AddDate(0, 0, -1) + return c } + c.time = c.Carbon2Time().AddDate(0, 0, -1) return c } diff --git a/carbon_test.go b/carbon_test.go index 03917521..2ba372cd 100755 --- a/carbon_test.go +++ b/carbon_test.go @@ -11,11 +11,11 @@ func TestCarbon_Now(t *testing.T) { assert := assert.New(t) actual1 := Now().ToDateString() - expected1 := time.Now().Format(DateFormat) + expected1 := time.Now().Format(DateLayout) assert.Equal(expected1, actual1) actual2 := Now(Local).ToDateString() - expected2 := time.Now().In(time.Local).Format(DateFormat) + expected2 := time.Now().In(time.Local).Format(DateLayout) assert.Equal(expected2, actual2) } @@ -23,12 +23,12 @@ func TestCarbon_Yesterday(t *testing.T) { assert := assert.New(t) c1 := Yesterday() - expected1 := time.Now().AddDate(0, 0, -1).Format(DateFormat) + expected1 := time.Now().AddDate(0, 0, -1).Format(DateLayout) assert.Nil(c1.Error) assert.Equal(expected1, c1.ToDateString()) c2 := Yesterday(Local) - expected2 := time.Now().In(time.Local).AddDate(0, 0, -1).Format(DateFormat) + expected2 := time.Now().In(time.Local).AddDate(0, 0, -1).Format(DateLayout) assert.Nil(c2.Error) assert.Equal(expected2, c2.ToDateString()) @@ -41,12 +41,12 @@ func TestCarbon_Tomorrow(t *testing.T) { assert := assert.New(t) c1 := Tomorrow() - expected1 := time.Now().AddDate(0, 0, 1).Format(DateFormat) + expected1 := time.Now().AddDate(0, 0, 1).Format(DateLayout) assert.Nil(c1.Error) assert.Equal(expected1, c1.ToDateString()) c2 := Tomorrow(Local) - expected2 := time.Now().In(time.Local).AddDate(0, 0, 1).Format(DateFormat) + expected2 := time.Now().In(time.Local).AddDate(0, 0, 1).Format(DateLayout) assert.Nil(c2.Error) assert.Equal(expected2, c2.ToDateString()) @@ -58,7 +58,7 @@ func TestCarbon_Tomorrow(t *testing.T) { func TestCarbon_Time2Carbon(t *testing.T) { assert := assert.New(t) - expected := time.Now().Format(DateTimeFormat) + expected := time.Now().Format(DateTimeLayout) actual := Time2Carbon(time.Now()).ToDateTimeString() assert.Equal(expected, actual) } @@ -66,8 +66,8 @@ func TestCarbon_Time2Carbon(t *testing.T) { func TestCarbon_Carbon2Time(t *testing.T) { assert := assert.New(t) - expected := time.Now().Format(DateTimeFormat) - actual := Now().Carbon2Time().Format(DateTimeFormat) + expected := time.Now().Format(DateTimeLayout) + actual := Now().Carbon2Time().Format(DateTimeLayout) assert.Equal(expected, actual) } diff --git a/outputer.go b/outputer.go index 25b9cf68..cca2196f 100644 --- a/outputer.go +++ b/outputer.go @@ -12,7 +12,7 @@ func (c Carbon) String() string { return c.ToDateTimeString() } -// ToString outputs a string in "2006-01-02 15:04:05.999999999 -0700 MST" format. +// ToString outputs a string in "2006-01-02 15:04:05.999999999 -0700 MST" layout. // 输出 "2006-01-02 15:04:05.999999999 -0700 MST" 格式字符串 func (c Carbon) ToString(timezone ...string) string { if len(timezone) > 0 { @@ -24,7 +24,7 @@ func (c Carbon) ToString(timezone ...string) string { return c.Carbon2Time().String() } -// ToMonthString outputs a string in month format like "January", i18n is supported. +// ToMonthString outputs a string in month layout like "January", i18n is supported. // 输出完整月份字符串,支持i18n func (c Carbon) ToMonthString(timezone ...string) string { if len(timezone) > 0 { @@ -45,7 +45,7 @@ func (c Carbon) ToMonthString(timezone ...string) string { return "" } -// ToShortMonthString outputs a string in short month format like "Jan", i18n is supported. +// ToShortMonthString outputs a string in short month layout like "Jan", i18n is supported. // 输出缩写月份字符串,支持i18n func (c Carbon) ToShortMonthString(timezone ...string) string { if len(timezone) > 0 { @@ -66,7 +66,7 @@ func (c Carbon) ToShortMonthString(timezone ...string) string { return "" } -// ToWeekString outputs a string in week format like "Sunday", i18n is supported. +// ToWeekString outputs a string in week layout like "Sunday", i18n is supported. // 输出完整星期字符串,支持i18n func (c Carbon) ToWeekString(timezone ...string) string { if len(timezone) > 0 { @@ -87,7 +87,7 @@ func (c Carbon) ToWeekString(timezone ...string) string { return "" } -// ToShortWeekString outputs a string in short week format like "Sun", i18n is supported. +// ToShortWeekString outputs a string in short week layout like "Sun", i18n is supported. // 输出缩写星期字符串,支持i18n func (c Carbon) ToShortWeekString(timezone ...string) string { if len(timezone) > 0 { @@ -108,7 +108,7 @@ func (c Carbon) ToShortWeekString(timezone ...string) string { return "" } -// ToDayDateTimeString outputs a string in "Mon, Jan 2, 2006 3:04 PM" format. +// ToDayDateTimeString outputs a string in "Mon, Jan 2, 2006 3:04 PM" layout. // 输出 "Mon, Jan 2, 2006 3:04 PM" 格式字符串 func (c Carbon) ToDayDateTimeString(timezone ...string) string { if len(timezone) > 0 { @@ -117,10 +117,10 @@ func (c Carbon) ToDayDateTimeString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(DayDateTimeFormat) + return c.Carbon2Time().Format(DayDateTimeLayout) } -// ToDateTimeString outputs a string in "2006-01-02 15:04:05" format. +// ToDateTimeString outputs a string in "2006-01-02 15:04:05" layout. // 输出 "2006-01-02 15:04:05" 格式字符串 func (c Carbon) ToDateTimeString(timezone ...string) string { if len(timezone) > 0 { @@ -129,10 +129,10 @@ func (c Carbon) ToDateTimeString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(DateTimeFormat) + return c.Carbon2Time().Format(DateTimeLayout) } -// ToDateTimeMilliString outputs a string in "2006-01-02 15:04:05.999" format. +// ToDateTimeMilliString outputs a string in "2006-01-02 15:04:05.999" layout. // 输出 "2006-01-02 15:04:05.999" 格式字符串 func (c Carbon) ToDateTimeMilliString(timezone ...string) string { if len(timezone) > 0 { @@ -141,10 +141,10 @@ func (c Carbon) ToDateTimeMilliString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(DateTimeMilliFormat) + return c.Carbon2Time().Format(DateTimeMilliLayout) } -// ToDateTimeMicroString outputs a string in "2006-01-02 15:04:05.999999" format. +// ToDateTimeMicroString outputs a string in "2006-01-02 15:04:05.999999" layout. // 输出 "2006-01-02 15:04:05.999999" 格式字符串 func (c Carbon) ToDateTimeMicroString(timezone ...string) string { if len(timezone) > 0 { @@ -153,10 +153,10 @@ func (c Carbon) ToDateTimeMicroString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(DateTimeMicroFormat) + return c.Carbon2Time().Format(DateTimeMicroLayout) } -// ToDateTimeNanoString outputs a string in "2006-01-02 15:04:05.999999999" format. +// ToDateTimeNanoString outputs a string in "2006-01-02 15:04:05.999999999" layout. // 输出 "2006-01-02 15:04:05.999999999" 格式字符串 func (c Carbon) ToDateTimeNanoString(timezone ...string) string { if len(timezone) > 0 { @@ -165,10 +165,10 @@ func (c Carbon) ToDateTimeNanoString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(DateTimeNanoFormat) + return c.Carbon2Time().Format(DateTimeNanoLayout) } -// ToShortDateTimeString outputs a string in "20060102150405" format. +// ToShortDateTimeString outputs a string in "20060102150405" layout. // 输出 "20060102150405" 格式字符串 func (c Carbon) ToShortDateTimeString(timezone ...string) string { if len(timezone) > 0 { @@ -177,10 +177,10 @@ func (c Carbon) ToShortDateTimeString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(ShortDateTimeFormat) + return c.Carbon2Time().Format(ShortDateTimeLayout) } -// ToShortDateTimeMilliString outputs a string in "20060102150405.999" format. +// ToShortDateTimeMilliString outputs a string in "20060102150405.999" layout. // 输出 "20060102150405.999" 格式字符串 func (c Carbon) ToShortDateTimeMilliString(timezone ...string) string { if len(timezone) > 0 { @@ -189,10 +189,10 @@ func (c Carbon) ToShortDateTimeMilliString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(ShortDateTimeMilliFormat) + return c.Carbon2Time().Format(ShortDateTimeMilliLayout) } -// ToShortDateTimeMicroString outputs a string in "20060102150405.999999" format. +// ToShortDateTimeMicroString outputs a string in "20060102150405.999999" layout. // 输出 "20060102150405.999999" 格式字符串 func (c Carbon) ToShortDateTimeMicroString(timezone ...string) string { if len(timezone) > 0 { @@ -201,10 +201,10 @@ func (c Carbon) ToShortDateTimeMicroString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(ShortDateTimeMicroFormat) + return c.Carbon2Time().Format(ShortDateTimeMicroLayout) } -// ToShortDateTimeNanoString outputs a string in "20060102150405.999999999" format. +// ToShortDateTimeNanoString outputs a string in "20060102150405.999999999" layout. // 输出 "20060102150405.999999999" 格式字符串 func (c Carbon) ToShortDateTimeNanoString(timezone ...string) string { if len(timezone) > 0 { @@ -213,10 +213,10 @@ func (c Carbon) ToShortDateTimeNanoString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(ShortDateTimeNanoFormat) + return c.Carbon2Time().Format(ShortDateTimeNanoLayout) } -// ToDateString outputs a string in "2006-01-02" format. +// ToDateString outputs a string in "2006-01-02" layout. // 输出 "2006-01-02" 格式字符串 func (c Carbon) ToDateString(timezone ...string) string { if len(timezone) > 0 { @@ -225,10 +225,10 @@ func (c Carbon) ToDateString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(DateFormat) + return c.Carbon2Time().Format(DateLayout) } -// ToShortDateString outputs a string in "20060102" format. +// ToShortDateString outputs a string in "20060102" layout. // 输出 "20060102" 格式字符串 func (c Carbon) ToShortDateString(timezone ...string) string { if len(timezone) > 0 { @@ -237,10 +237,10 @@ func (c Carbon) ToShortDateString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(ShortDateFormat) + return c.Carbon2Time().Format(ShortDateLayout) } -// ToTimeString outputs a string in "15:04:05" format. +// ToTimeString outputs a string in "15:04:05" layout. // 输出 "15:04:05" 格式字符串 func (c Carbon) ToTimeString(timezone ...string) string { if len(timezone) > 0 { @@ -249,10 +249,10 @@ func (c Carbon) ToTimeString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(TimeFormat) + return c.Carbon2Time().Format(TimeLayout) } -// ToShortTimeString outputs a string in "150405" format. +// ToShortTimeString outputs a string in "150405" layout. // 输出 "150405" 格式字符串 func (c Carbon) ToShortTimeString(timezone ...string) string { if len(timezone) > 0 { @@ -261,16 +261,16 @@ func (c Carbon) ToShortTimeString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(ShortTimeFormat) + return c.Carbon2Time().Format(ShortTimeLayout) } -// ToAtomString outputs a string in "2006-01-02T15:04:05Z07:00" format. +// ToAtomString outputs a string in "2006-01-02T15:04:05Z07:00" layout. // 输出 "2006-01-02T15:04:05Z07:00" 格式字符串 func (c Carbon) ToAtomString(timezone ...string) string { return c.ToRfc3339String(timezone...) } -// ToANSICString outputs a string in "Mon Jan _2 15:04:05 2006" format. +// ToANSICString outputs a string in "Mon Jan _2 15:04:05 2006" layout. // 输出 "Mon Jan _2 15:04:05 2006" 格式字符串 func (c Carbon) ToANSICString(timezone ...string) string { if len(timezone) > 0 { @@ -279,10 +279,10 @@ func (c Carbon) ToANSICString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(ANSICFormat) + return c.Carbon2Time().Format(ANSICLayout) } -// ToCookieString outputs a string in "Monday, 02-Jan-2006 15:04:05 MST" format. +// ToCookieString outputs a string in "Monday, 02-Jan-2006 15:04:05 MST" layout. // 输出 "Monday, 02-Jan-2006 15:04:05 MST" 格式字符串 func (c Carbon) ToCookieString(timezone ...string) string { if len(timezone) > 0 { @@ -291,7 +291,7 @@ func (c Carbon) ToCookieString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(CookieFormat) + return c.Carbon2Time().Format(CookieLayout) } // ToRssString outputs a string in "Mon, 02 Jan 2006 15:04:05 -0700" format. @@ -303,16 +303,16 @@ func (c Carbon) ToRssString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(RssFormat) + return c.Carbon2Time().Format(RssLayout) } -// ToW3cString outputs a string in "2006-01-02T15:04:05Z07:00" format. +// ToW3cString outputs a string in "2006-01-02T15:04:05Z07:00" layout. // 输出 "2006-01-02T15:04:05Z07:00" 格式字符串 func (c Carbon) ToW3cString(timezone ...string) string { return c.ToRfc3339String(timezone...) } -// ToUnixDateString outputs a string in "Mon Jan _2 15:04:05 MST 2006" format. +// ToUnixDateString outputs a string in "Mon Jan _2 15:04:05 MST 2006" layout. // 输出 "Mon Jan _2 15:04:05 MST 2006" 格式字符串 func (c Carbon) ToUnixDateString(timezone ...string) string { if len(timezone) > 0 { @@ -321,10 +321,10 @@ func (c Carbon) ToUnixDateString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(UnixDateFormat) + return c.Carbon2Time().Format(UnixDateLayout) } -// ToRubyDateString outputs a string in "Mon Jan 02 15:04:05 -0700 2006" format. +// ToRubyDateString outputs a string in "Mon Jan 02 15:04:05 -0700 2006" layout. // 输出 "Mon Jan 02 15:04:05 -0700 2006" 格式字符串 func (c Carbon) ToRubyDateString(timezone ...string) string { if len(timezone) > 0 { @@ -333,10 +333,10 @@ func (c Carbon) ToRubyDateString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(RubyDateFormat) + return c.Carbon2Time().Format(RubyDateLayout) } -// ToKitchenString outputs a string in "3:04PM" format. +// ToKitchenString outputs a string in "3:04PM" layout. // 输出 "3:04PM" 格式字符串 func (c Carbon) ToKitchenString(timezone ...string) string { if len(timezone) > 0 { @@ -345,10 +345,10 @@ func (c Carbon) ToKitchenString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(KitchenFormat) + return c.Carbon2Time().Format(KitchenLayout) } -// ToIso8601String outputs a string in "2006-01-02T15:04:05-07:00" format. +// ToIso8601String outputs a string in "2006-01-02T15:04:05-07:00" layout. // 输出 "2006-01-02T15:04:05-07:00" 格式字符串 func (c Carbon) ToIso8601String(timezone ...string) string { if len(timezone) > 0 { @@ -357,10 +357,10 @@ func (c Carbon) ToIso8601String(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(ISO8601Format) + return c.Carbon2Time().Format(ISO8601Layout) } -// ToIso8601MilliString outputs a string in "2006-01-02T15:04:05.999-07:00" format. +// ToIso8601MilliString outputs a string in "2006-01-02T15:04:05.999-07:00" layout. // 输出 "2006-01-02T15:04:05.999-07:00" 格式字符串 func (c Carbon) ToIso8601MilliString(timezone ...string) string { if len(timezone) > 0 { @@ -369,10 +369,10 @@ func (c Carbon) ToIso8601MilliString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(ISO8601MilliFormat) + return c.Carbon2Time().Format(ISO8601MilliLayout) } -// ToIso8601MicroString outputs a string in "2006-01-02T15:04:05.999999-07:00" format. +// ToIso8601MicroString outputs a string in "2006-01-02T15:04:05.999999-07:00" layout. // 输出 "2006-01-02T15:04:05.999999-07:00" 格式字符串 func (c Carbon) ToIso8601MicroString(timezone ...string) string { if len(timezone) > 0 { @@ -381,10 +381,10 @@ func (c Carbon) ToIso8601MicroString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(ISO8601MicroFormat) + return c.Carbon2Time().Format(ISO8601MicroLayout) } -// ToIso8601NanoString outputs a string in "2006-01-02T15:04:05.999999999-07:00" format. +// ToIso8601NanoString outputs a string in "2006-01-02T15:04:05.999999999-07:00" layout. // 输出 "2006-01-02T15:04:05.999999999-07:00" 格式字符串 func (c Carbon) ToIso8601NanoString(timezone ...string) string { if len(timezone) > 0 { @@ -393,10 +393,10 @@ func (c Carbon) ToIso8601NanoString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(ISO8601NanoFormat) + return c.Carbon2Time().Format(ISO8601NanoLayout) } -// ToRfc822String outputs a string in "02 Jan 06 15:04 MST" format. +// ToRfc822String outputs a string in "02 Jan 06 15:04 MST" layout. // 输出 "02 Jan 06 15:04 MST" 格式字符串 func (c Carbon) ToRfc822String(timezone ...string) string { if len(timezone) > 0 { @@ -405,10 +405,10 @@ func (c Carbon) ToRfc822String(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(RFC822Format) + return c.Carbon2Time().Format(RFC822Layout) } -// ToRfc822zString outputs a string in "02 Jan 06 15:04 -0700" format. +// ToRfc822zString outputs a string in "02 Jan 06 15:04 -0700" layout. // 输出 "02 Jan 06 15:04 -0700" 格式字符串 func (c Carbon) ToRfc822zString(timezone ...string) string { if len(timezone) > 0 { @@ -417,10 +417,10 @@ func (c Carbon) ToRfc822zString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(RFC822ZFormat) + return c.Carbon2Time().Format(RFC822ZLayout) } -// ToRfc850String outputs a string in "Monday, 02-Jan-06 15:04:05 MST" format. +// ToRfc850String outputs a string in "Monday, 02-Jan-06 15:04:05 MST" layout. // 输出 "Monday, 02-Jan-06 15:04:05 MST" 格式字符串 func (c Carbon) ToRfc850String(timezone ...string) string { if len(timezone) > 0 { @@ -429,10 +429,10 @@ func (c Carbon) ToRfc850String(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(RFC850Format) + return c.Carbon2Time().Format(RFC850Layout) } -// ToRfc1036String outputs a string in "Mon, 02 Jan 06 15:04:05 -0700" format. +// ToRfc1036String outputs a string in "Mon, 02 Jan 06 15:04:05 -0700" layout. // 输出 "Mon, 02 Jan 06 15:04:05 -0700" 格式字符串 func (c Carbon) ToRfc1036String(timezone ...string) string { if len(timezone) > 0 { @@ -441,10 +441,10 @@ func (c Carbon) ToRfc1036String(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(RFC1036Format) + return c.Carbon2Time().Format(RFC1036Layout) } -// ToRfc1123String outputs a string in "Mon, 02 Jan 2006 15:04:05 MST" format. +// ToRfc1123String outputs a string in "Mon, 02 Jan 2006 15:04:05 MST" layout. // 输出 "Mon, 02 Jan 2006 15:04:05 MST" 格式字符串 func (c Carbon) ToRfc1123String(timezone ...string) string { if len(timezone) > 0 { @@ -453,10 +453,10 @@ func (c Carbon) ToRfc1123String(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(RFC1123Format) + return c.Carbon2Time().Format(RFC1123Layout) } -// ToRfc1123zString outputs a string in "Mon, 02 Jan 2006 15:04:05 -0700" format. +// ToRfc1123zString outputs a string in "Mon, 02 Jan 2006 15:04:05 -0700" layout. // 输出 "Mon, 02 Jan 2006 15:04:05 -0700" 格式字符串 func (c Carbon) ToRfc1123zString(timezone ...string) string { if len(timezone) > 0 { @@ -465,10 +465,10 @@ func (c Carbon) ToRfc1123zString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(RFC1123ZFormat) + return c.Carbon2Time().Format(RFC1123ZLayout) } -// ToRfc2822String outputs a string in "Mon, 02 Jan 2006 15:04:05 -0700" format. +// ToRfc2822String outputs a string in "Mon, 02 Jan 2006 15:04:05 -0700" layout. // 输出 "Mon, 02 Jan 2006 15:04:05 -0700" 格式字符串 func (c Carbon) ToRfc2822String(timezone ...string) string { if len(timezone) > 0 { @@ -477,10 +477,10 @@ func (c Carbon) ToRfc2822String(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(RFC2822Format) + return c.Carbon2Time().Format(RFC2822Layout) } -// ToRfc3339String outputs a string in "2006-01-02T15:04:05Z07:00" format. +// ToRfc3339String outputs a string in "2006-01-02T15:04:05Z07:00" layout. // 输出 "2006-01-02T15:04:05Z07:00" 格式字符串 func (c Carbon) ToRfc3339String(timezone ...string) string { if len(timezone) > 0 { @@ -489,10 +489,10 @@ func (c Carbon) ToRfc3339String(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(RFC3339Format) + return c.Carbon2Time().Format(RFC3339Layout) } -// ToRfc3339MilliString outputs a string in "2006-01-02T15:04:05.999Z07:00" format. +// ToRfc3339MilliString outputs a string in "2006-01-02T15:04:05.999Z07:00" layout. // 输出 "2006-01-02T15:04:05.999Z07:00" 格式字符串 func (c Carbon) ToRfc3339MilliString(timezone ...string) string { if len(timezone) > 0 { @@ -501,10 +501,10 @@ func (c Carbon) ToRfc3339MilliString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(RFC3339MilliFormat) + return c.Carbon2Time().Format(RFC3339MilliLayout) } -// ToRfc3339MicroString outputs a string in "2006-01-02T15:04:05.999999Z07:00" format. +// ToRfc3339MicroString outputs a string in "2006-01-02T15:04:05.999999Z07:00" layout. // 输出 "2006-01-02T15:04:05.999999Z07:00" 格式字符串 func (c Carbon) ToRfc3339MicroString(timezone ...string) string { if len(timezone) > 0 { @@ -513,10 +513,10 @@ func (c Carbon) ToRfc3339MicroString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(RFC3339MicroFormat) + return c.Carbon2Time().Format(RFC3339MicroLayout) } -// ToRfc3339NanoString outputs a string in "2006-01-02T15:04:05.999999999Z07:00" format. +// ToRfc3339NanoString outputs a string in "2006-01-02T15:04:05.999999999Z07:00" layout. // 输出 "2006-01-02T15:04:05.999999999Z07:00" 格式字符串 func (c Carbon) ToRfc3339NanoString(timezone ...string) string { if len(timezone) > 0 { @@ -525,10 +525,10 @@ func (c Carbon) ToRfc3339NanoString(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(RFC3339NanoFormat) + return c.Carbon2Time().Format(RFC3339NanoLayout) } -// ToRfc7231String outputs a string in "Mon, 02 Jan 2006 15:04:05 GMT" format. +// ToRfc7231String outputs a string in "Mon, 02 Jan 2006 15:04:05 GMT" layout. // 输出 "Mon, 02 Jan 2006 15:04:05 GMT" 格式字符串 func (c Carbon) ToRfc7231String(timezone ...string) string { if len(timezone) > 0 { @@ -537,7 +537,7 @@ func (c Carbon) ToRfc7231String(timezone ...string) string { if c.IsInvalid() { return "" } - return c.Carbon2Time().Format(RFC7231Format) + return c.Carbon2Time().Format(RFC7231Layout) } // ToLayoutString outputs a string by layout. diff --git a/parser.go b/parser.go index 00d40cf0..7ed1d99d 100755 --- a/parser.go +++ b/parser.go @@ -9,32 +9,32 @@ import ( // Parse parses a standard string as a Carbon instance. // 将标准格式时间字符串解析成 Carbon 实例 func (c Carbon) Parse(value string, timezone ...string) Carbon { - layout := DateTimeFormat + layout := DateTimeLayout if _, err := strconv.ParseInt(value, 10, 64); err == nil { switch { case len(value) == 8: - layout = ShortDateFormat + layout = ShortDateLayout case len(value) == 14: - layout = ShortDateTimeFormat + layout = ShortDateTimeLayout } } else { switch { case len(value) == 10 && strings.Count(value, "-") == 2: - layout = DateFormat + layout = DateLayout case len(value) == 18 && strings.Index(value, ".") == 14: - layout = ShortDateTimeMilliFormat + layout = ShortDateTimeMilliLayout case len(value) == 21 && strings.Index(value, ".") == 14: - layout = ShortDateTimeMicroFormat + layout = ShortDateTimeMicroLayout case len(value) == 24 && strings.Index(value, ".") == 14: - layout = ShortDateTimeNanoFormat + layout = ShortDateTimeNanoLayout case len(value) == 25 && strings.Index(value, "T") == 10: - layout = RFC3339Format + layout = RFC3339Layout case len(value) == 29 && strings.Index(value, "T") == 10 && strings.Index(value, ".") == 19: - layout = RFC3339MilliFormat + layout = RFC3339MilliLayout case len(value) == 32 && strings.Index(value, "T") == 10 && strings.Index(value, ".") == 19: - layout = RFC3339MicroFormat + layout = RFC3339MicroLayout case len(value) == 35 && strings.Index(value, "T") == 10 && strings.Index(value, ".") == 19: - layout = RFC3339NanoFormat + layout = RFC3339NanoLayout } } carbon := c.ParseByLayout(value, layout, timezone...) diff --git a/parser_test.go b/parser_test.go index fe82b26c..0e83d2df 100755 --- a/parser_test.go +++ b/parser_test.go @@ -112,7 +112,7 @@ func TestCarbon_ParseByLayout(t *testing.T) { } func TestError_Parse(t *testing.T) { - date, layout, format, timezone := "2020-08-50", "2006-01-02", DateFormat, "xxx" + date, layout, format, timezone := "2020-08-50", "2006-01-02", DateLayout, "xxx" assert.NotNil(t, Parse(date).Error, "It should catch an exception in Parse()") assert.NotNil(t, ParseByLayout(date, layout, timezone).Error, "It should catch an exception in ParseByLayout()") assert.NotNil(t, ParseByFormat(date, format, timezone).Error, "It should catch an exception in ParseByFormat()")