diff --git a/parser.go b/parser.go index 647de9fe..36a3f45d 100755 --- a/parser.go +++ b/parser.go @@ -8,7 +8,7 @@ import ( // Parse parses a standard time string as a Carbon instance. // 将标准格式时间字符串解析成 Carbon 实例 func (c Carbon) Parse(value string, timezone ...string) Carbon { - if value == "" { + if len(value) == 0 { c.Error = invalidValueError(value) return c } @@ -65,9 +65,12 @@ func (c Carbon) ParseByLayout(value, layout string, timezone ...string) Carbon { if c.Error != nil { return c } - if value == "" { + if len(value) == 0 { return c } + if len(layout) == 0 { + layout = defaultLayout + } if layout == "timestamp" { timestamp, _ := strconv.ParseInt(value, 10, 64) return c.CreateFromTimestamp(timestamp) diff --git a/parser_unit_test.go b/parser_unit_test.go index 6a47a40d..bbb12c14 100755 --- a/parser_unit_test.go +++ b/parser_unit_test.go @@ -1,6 +1,7 @@ package carbon import ( + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -223,6 +224,7 @@ func TestCarbon_Issue206(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + fmt.Println("err", tt.carbon.Error) assert.Equalf(t, tt.want, tt.carbon.ToString(PRC), "Parse()") }) }