优化 ParseByLayout 方法

This commit is contained in:
gouguoyin 2024-11-29 11:24:35 +08:00
parent 0558b9d3e9
commit f6452c93a4
2 changed files with 5 additions and 4 deletions

View File

@ -59,15 +59,16 @@ func ParseByFormat(value, format string, timezone ...string) Carbon {
// ParseByLayout parses a time string as a Carbon instance by layout.
// 通过布局模板将时间字符串解析成 Carbon 实例
func (c Carbon) ParseByLayout(value, layout string, timezone ...string) Carbon {
if len(value) == 0 {
c.Error = invalidValueError(value)
return c
}
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil {
return c
}
if len(value) == 0 {
return c
}
if len(layout) == 0 {
layout = defaultLayout
}

View File

@ -240,7 +240,7 @@ func TestCarbon_Issue232(t *testing.T) {
{
name: "case1",
carbon: Parse("0000-01-01 00:00:00"),
want: "0000-01-01 00:00:00",
want: "",
},
{
name: "case2",