Merge branch 'v2'

This commit is contained in:
gouguoyin 2022-04-17 22:51:39 +08:00
commit ff48989c67
5 changed files with 29 additions and 11 deletions

View File

@ -160,6 +160,8 @@ carbon.Parse("0000-00-00 00:00:00").ToDateTimeString() // 空字符串
carbon.Parse("0000-00-00").ToDateTimeString() // 空字符串
carbon.Parse("00:00:00").ToDateTimeString() // 空字符串
carbon.Parse("2020-08-05").ToString() // 2020-08-05 00:00:00 +0800 CST
carbon.Parse("20200805").ToString() // 2020-08-05 00:00:00 +0800 CST
carbon.Parse("2020-08-05 13:14:15").ToString() // 2020-08-05 13:14:15 +0800 CST
carbon.Parse("2020-08-05 13:14:15.999").ToString() // 2020-08-05 13:14:15.999 +0800 CST
carbon.Parse("2020-08-05 13:14:15.999999").ToString() // 2020-08-05 13:14:15.999999 +0800 CST
@ -168,9 +170,10 @@ carbon.Parse("2020-08-05T13:14:15+08:00").ToString() // 2020-08-05 13:14:15 +080
carbon.Parse("2020-08-05T13:14:15.999+08:00").ToString() // 2020-08-05 13:14:15.999 +0800 CST
carbon.Parse("2020-08-05T13:14:15.999999+08:00").ToString() // 2020-08-05 13:14:15.999999 +0800 CST
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToString() // 2020-08-05 13:14:15.999999999 +0800 CST
carbon.Parse("2020-08-05").ToString() // 2020-08-05 00:00:00 +0800 CST
carbon.Parse("20200805131415").ToString() // 2020-08-05 13:14:15 +0800 CST
carbon.Parse("20200805").ToString() // 2020-08-05 00:00:00 +0800 CST
carbon.Parse("20200805131415.999").ToString() // 2020-08-05 13:14:15.999 +0800 CST
carbon.Parse("20200805131415.999999").ToString() // 2020-08-05 13:14:15.999999 +0800 CST
carbon.Parse("20200805131415.999999999").ToString() // 2020-08-05 13:14:15.999999999 +0800 CST
```
##### 通过格式化字符将字符串解析成 Carbon 实例

View File

@ -160,6 +160,8 @@ carbon.Parse("0000-00-00 00:00:00").ToDateTimeString() // 空の文字列
carbon.Parse("0000-00-00").ToDateTimeString() // 空の文字列
carbon.Parse("00:00:00").ToDateTimeString() // 空の文字列
carbon.Parse("2020-08-05").ToString() // 2020-08-05 00:00:00 +0800 CST
carbon.Parse("20200805").ToString() // 2020-08-05 00:00:00 +0800 CST
carbon.Parse("2020-08-05 13:14:15").ToString() // 2020-08-05 13:14:15 +0800 CST
carbon.Parse("2020-08-05 13:14:15.999").ToString() // 2020-08-05 13:14:15.999 +0800 CST
carbon.Parse("2020-08-05 13:14:15.999999").ToString() // 2020-08-05 13:14:15.999999 +0800 CST
@ -168,9 +170,10 @@ carbon.Parse("2020-08-05T13:14:15+08:00").ToString() // 2020-08-05 13:14:15 +080
carbon.Parse("2020-08-05T13:14:15.999+08:00").ToString() // 2020-08-05 13:14:15.999 +0800 CST
carbon.Parse("2020-08-05T13:14:15.999999+08:00").ToString() // 2020-08-05 13:14:15.999999 +0800 CST
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToString() // 2020-08-05 13:14:15.999999999 +0800 CST
carbon.Parse("2020-08-05").ToString() // 2020-08-05 00:00:00 +0800 CST
carbon.Parse("20200805131415").ToString() // 2020-08-05 13:14:15 +0800 CST
carbon.Parse("20200805").ToString() // 2020-08-05 00:00:00 +0800 CST
carbon.Parse("20200805131415.999").ToString() // 2020-08-05 13:14:15.999 +0800 CST
carbon.Parse("20200805131415.999999").ToString() // 2020-08-05 13:14:15.999999 +0800 CST
carbon.Parse("20200805131415.999999999").ToString() // 2020-08-05 13:14:15.999999999 +0800 CST
```
##### 文字をフォーマットして文字列を Carbon オブジェクトに解析します

View File

@ -163,6 +163,8 @@ carbon.Parse("0000-00-00 00:00:00").ToDateTimeString() // empty string
carbon.Parse("0000-00-00").ToDateTimeString() // empty string
carbon.Parse("00:00:00").ToDateTimeString() // empty string
carbon.Parse("2020-08-05").ToString() // 2020-08-05 00:00:00 +0800 CST
carbon.Parse("20200805").ToString() // 2020-08-05 00:00:00 +0800 CST
carbon.Parse("2020-08-05 13:14:15").ToString() // 2020-08-05 13:14:15 +0800 CST
carbon.Parse("2020-08-05 13:14:15.999").ToString() // 2020-08-05 13:14:15.999 +0800 CST
carbon.Parse("2020-08-05 13:14:15.999999").ToString() // 2020-08-05 13:14:15.999999 +0800 CST
@ -171,9 +173,10 @@ carbon.Parse("2020-08-05T13:14:15+08:00").ToString() // 2020-08-05 13:14:15 +080
carbon.Parse("2020-08-05T13:14:15.999+08:00").ToString() // 2020-08-05 13:14:15.999 +0800 CST
carbon.Parse("2020-08-05T13:14:15.999999+08:00").ToString() // 2020-08-05 13:14:15.999999 +0800 CST
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToString() // 2020-08-05 13:14:15.999999999 +0800 CST
carbon.Parse("2020-08-05").ToString() // 2020-08-05 00:00:00 +0800 CST
carbon.Parse("20200805131415").ToString() // 2020-08-05 13:14:15 +0800 CST
carbon.Parse("20200805").ToString() // 2020-08-05 00:00:00 +0800 CST
carbon.Parse("20200805131415.999").ToString() // 2020-08-05 13:14:15.999 +0800 CST
carbon.Parse("20200805131415.999999").ToString() // 2020-08-05 13:14:15.999999 +0800 CST
carbon.Parse("20200805131415.999999999").ToString() // 2020-08-05 13:14:15.999999999 +0800 CST
```
##### Parse a string as a Carbon instance by format

View File

@ -11,16 +11,22 @@ import (
func (c Carbon) Parse(value string, timezone ...string) Carbon {
layout := DateTimeFormat
if _, err := strconv.ParseInt(value, 10, 64); err == nil {
switch len(value) {
case 8:
switch {
case len(value) == 8:
layout = ShortDateFormat
case 14:
case len(value) == 14:
layout = ShortDateTimeFormat
}
} else {
switch {
case len(value) == 10 && strings.Count(value, "-") == 2:
layout = DateFormat
case len(value) == 18 && strings.Index(value, ".") == 14:
layout = ShortDateTimeMilliFormat
case len(value) == 21 && strings.Index(value, ".") == 14:
layout = ShortDateTimeMicroFormat
case len(value) == 24 && strings.Index(value, ".") == 14:
layout = ShortDateTimeNanoFormat
case len(value) == 25 && strings.Index(value, "T") == 10:
layout = RFC3339Format
case len(value) == 29 && strings.Index(value, "T") == 10 && strings.Index(value, ".") == 19:

View File

@ -21,13 +21,16 @@ func TestCarbon_Parse(t *testing.T) {
{"0000-00-00 00:00:00", ""},
{"2020-08-05 13:14:15", "2020-08-05 13:14:15 +0800 CST"},
{"20200805131415", "2020-08-05 13:14:15 +0800 CST"},
{"20200805", "2020-08-05 00:00:00 +0800 CST"},
{"2020-08-05", "2020-08-05 00:00:00 +0800 CST"},
{"2020-08-05T13:14:15+08:00", "2020-08-05 13:14:15 +0800 CST"},
{"2020-08-05T13:14:15.999+08:00", "2020-08-05 13:14:15.999 +0800 CST"},
{"2020-08-05T13:14:15.999999+08:00", "2020-08-05 13:14:15.999999 +0800 CST"},
{"2020-08-05T13:14:15.999999999+08:00", "2020-08-05 13:14:15.999999999 +0800 CST"},
{"20200805131415", "2020-08-05 13:14:15 +0800 CST"},
{"20200805131415.999", "2020-08-05 13:14:15.999 +0800 CST"},
{"20200805131415.999999", "2020-08-05 13:14:15.999999 +0800 CST"},
{"20200805131415.999999999", "2020-08-05 13:14:15.999999999 +0800 CST"},
{"20200805", "2020-08-05 00:00:00 +0800 CST"},
}
for index, test := range tests {