Skip to content

Commit

Permalink
rename ToAnsicString() to ToANSICString()
Browse files Browse the repository at this point in the history
add ToIso8601MilliString(),ToIso8601MicroString(),ToIso8601NanoString() methods
  • Loading branch information
gouguoyin committed Apr 14, 2022
1 parent d75deb8 commit 546e6c5
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 13 deletions.
11 changes: 9 additions & 2 deletions README.cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ carbon.Parse("2020-08-05 13:14:15").ToTimeString() // 13:14:15
carbon.Parse("2020-08-05 13:14:15").ToShortTimeString() // 131415

// 输出 Ansic 格式字符串
carbon.Parse("2020-08-05 13:14:15").ToAnsicString() // Wed Aug 5 13:14:15 2020
carbon.Parse("2020-08-05 13:14:15").ToANSICString() // Wed Aug 5 13:14:15 2020
// 输出 Atom 格式字符串
carbon.Parse("2020-08-05 13:14:15").ToAtomString() // 2020-08-05T13:14:15+08:00
// 输出 UnixDate 格式字符串
Expand All @@ -845,7 +845,14 @@ carbon.Parse("2020-08-05 13:14:15").ToRssString() // Wed, 05 Aug 2020 13:14:15 +
carbon.Parse("2020-08-05 13:14:15").ToW3cString() // 2020-08-05T13:14:15+08:00

// 输出 ISO8601 格式字符串
carbon.Parse("2020-08-05 13:14:15").ToIso8601String() // 2020-08-05T13:14:15+08:00
carbon.Parse("2020-08-05 13:14:15.999999999").ToIso8601String() // 2020-08-05T13:14:15+08:00
// 输出 ISO8601Milli 格式字符串
carbon.Parse("2020-08-05 13:14:15.999999999").ToIso8601MilliString() // 2020-08-05T13:14:15.999+08:00
// 输出 ISO8601Micro 格式字符串
carbon.Parse("2020-08-05 13:14:15.999999999").ToIso8601MicroString() // 2020-08-05T13:14:15.999999+08:00
// 输出 ISO8601Nano 格式字符串
carbon.Parse("2020-08-05 13:14:15.999999999").ToIso8601NanoString() // 2020-08-05T13:14:15.999999999+08:00

// 输出 RFC822 格式字符串
carbon.Parse("2020-08-05 13:14:15").ToRfc822String() // 05 Aug 20 13:14 CST
// 输出 RFC822Z 格式字符串
Expand Down
13 changes: 10 additions & 3 deletions README.jp.md
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ carbon.Parse("2020-08-05 13:14:15").ToTimeString() // 13:14:15
carbon.Parse("2020-08-05 13:14:15").ToShortTimeString() // 131415

// Ansic フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToAnsicString() // Wed Aug 5 13:14:15 2020
carbon.Parse("2020-08-05 13:14:15").ToANSICString() // Wed Aug 5 13:14:15 2020
// Atom フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToAtomString() // 2020-08-05T13:14:15+08:00
// UnixDate フォーマット文字列を出力
Expand All @@ -840,8 +840,15 @@ carbon.Parse("2020-08-05 13:14:15").ToRssString() // Wed, 05 Aug 2020 13:14:15 +
// W3C フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToW3cString() // 2020-08-05T13:14:15+08:00

// ISO8601 フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToIso8601String() // 2020-08-05T13:14:15+08:00
// ISO8601 フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15.999999999").ToIso8601String() // 2020-08-05T13:14:15+08:00
// ISO8601Milli フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15.999999999").ToIso8601MilliString() // 2020-08-05T13:14:15.999+08:00
// ISO8601Micro フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15.999999999").ToIso8601MicroString() // 2020-08-05T13:14:15.999999+08:00
// ISO8601Nano フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15.999999999").ToIso8601NanoString() // 2020-08-05T13:14:15.999999999+08:00

// RFC822 フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToRfc822String() // 05 Aug 20 13:14 CST
// RFC822Z フォーマット文字列を出力
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ carbon.Parse("2020-08-05 13:14:15").ToTimeString() // 13:14:15
carbon.Parse("2020-08-05 13:14:15").ToShortTimeString() // 131415

// Output a string in Ansic format
carbon.Parse("2020-08-05 13:14:15").ToAnsicString() // Wed Aug 5 13:14:15 2020
carbon.Parse("2020-08-05 13:14:15").ToANSICString() // Wed Aug 5 13:14:15 2020
// Output a string in Atom format
carbon.Parse("2020-08-05 13:14:15").ToAtomString() // 2020-08-05T13:14:15+08:00
// Output a string in unix date format
Expand All @@ -838,7 +838,14 @@ carbon.Parse("2020-08-05 13:14:15").ToRssString() // Wed, 05 Aug 2020 13:14:15 +
carbon.Parse("2020-08-05 13:14:15").ToW3cString() // 2020-08-05T13:14:15+08:00

// Output a string in ISO8601 format
carbon.Parse("2020-08-05 13:14:15").ToIso8601String() // 2020-08-05T13:14:15+08:00
carbon.Parse("2020-08-05 13:14:15.999999999").ToIso8601String() // 2020-08-05T13:14:15+08:00
// Output a string in ISO8601Milli format
carbon.Parse("2020-08-05 13:14:15.999999999").ToIso8601MilliString() // 2020-08-05T13:14:15.999+08:00
// Output a string in ISO8601Micro format
carbon.Parse("2020-08-05 13:14:15.999999999").ToIso8601MicroString() // 2020-08-05T13:14:15.999999+08:00
// Output a string in ISO8601Nano format
carbon.Parse("2020-08-05 13:14:15.999999999").ToIso8601NanoString() // 2020-08-05T13:14:15.999999999+08:00

// Output a string in RFC822 format
carbon.Parse("2020-08-05 13:14:15").ToRfc822String() // 05 Aug 20 13:14 CST
// Output a string in RFC822Z format
Expand Down
42 changes: 39 additions & 3 deletions outputer.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,16 +270,16 @@ 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" format.
// 输出 "Mon Jan _2 15:04:05 2006" 格式字符串
func (c Carbon) ToAnsicString(timezone ...string) string {
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)
return c.time.In(c.loc).Format(ANSICFormat)
}

// ToCookieString outputs a string in "Monday, 02-Jan-2006 15:04:05 MST" format.
Expand Down Expand Up @@ -360,6 +360,42 @@ func (c Carbon) ToIso8601String(timezone ...string) string {
return c.time.In(c.loc).Format(ISO8601Format)
}

// ToIso8601MilliString outputs a string in "2006-01-02T15:04:05.999-07:00" format.
// 输出 "2006-01-02T15:04:05.999-07:00" 格式字符串
func (c Carbon) ToIso8601MilliString(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(ISO8601MilliFormat)
}

// ToIso8601MicroString outputs a string in "2006-01-02T15:04:05.999999-07:00" format.
// 输出 "2006-01-02T15:04:05.999999-07:00" 格式字符串
func (c Carbon) ToIso8601MicroString(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(ISO8601MicroFormat)
}

// ToIso8601NanoString outputs a string in "2006-01-02T15:04:05.999999999-07:00" format.
// 输出 "2006-01-02T15:04:05.999999999-07:00" 格式字符串
func (c Carbon) ToIso8601NanoString(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(ISO8601NanoFormat)
}

// ToRfc822String outputs a string in "02 Jan 06 15:04 MST" format.
// 输出 "02 Jan 06 15:04 MST" 格式字符串
func (c Carbon) ToRfc822String(timezone ...string) string {
Expand Down
96 changes: 93 additions & 3 deletions outputer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ func TestCarbon_ToAtomString(t *testing.T) {
}
}

func TestCarbon_ToAnsicString(t *testing.T) {
func TestCarbon_ToANSICString(t *testing.T) {
assert := assert.New(t)

tests := []struct {
Expand All @@ -649,13 +649,13 @@ func TestCarbon_ToAnsicString(t *testing.T) {
for index, test := range tests {
c := Parse(test.input, PRC)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToAnsicString(), "Current test index is "+strconv.Itoa(index))
assert.Equal(test.expected, c.ToANSICString(), "Current test index is "+strconv.Itoa(index))
}

for index, test := range tests {
c := Parse(test.input, PRC)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToAnsicString(PRC), "Current test index is "+strconv.Itoa(index))
assert.Equal(test.expected, c.ToANSICString(PRC), "Current test index is "+strconv.Itoa(index))
}
}

Expand Down Expand Up @@ -869,6 +869,96 @@ func TestCarbon_ToIso8601String(t *testing.T) {
}
}

func TestCarbon_ToIso8601MilliString(t *testing.T) {
assert := assert.New(t)

tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},

{"2020-08-05 13:14:15.999999999", "2020-08-05T13:14:15.999+08:00"},
{"2020-08-05", "2020-08-05T00:00:00+08:00"},
}

for index, test := range tests {
c := Parse(test.input, PRC)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToIso8601MilliString(), "Current test index is "+strconv.Itoa(index))
}

for index, test := range tests {
c := Parse(test.input, PRC)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToIso8601MilliString(PRC), "Current test index is "+strconv.Itoa(index))
}
}

func TestCarbon_ToIso8601MicroString(t *testing.T) {
assert := assert.New(t)

tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},

{"2020-08-05 13:14:15.999999999", "2020-08-05T13:14:15.999999+08:00"},
{"2020-08-05", "2020-08-05T00:00:00+08:00"},
}

for index, test := range tests {
c := Parse(test.input, PRC)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToIso8601MicroString(), "Current test index is "+strconv.Itoa(index))
}

for index, test := range tests {
c := Parse(test.input, PRC)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToIso8601MicroString(PRC), "Current test index is "+strconv.Itoa(index))
}
}

func TestCarbon_ToIso8601NanoString(t *testing.T) {
assert := assert.New(t)

tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},

{"2020-08-05 13:14:15.999999999", "2020-08-05T13:14:15.999999999+08:00"},
{"2020-08-05", "2020-08-05T00:00:00+08:00"},
}

for index, test := range tests {
c := Parse(test.input, PRC)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToIso8601NanoString(), "Current test index is "+strconv.Itoa(index))
}

for index, test := range tests {
c := Parse(test.input, PRC)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToIso8601NanoString(PRC), "Current test index is "+strconv.Itoa(index))
}
}

func TestCarbon_ToRfc822String(t *testing.T) {
assert := assert.New(t)

Expand Down

0 comments on commit 546e6c5

Please sign in to comment.