Skip to content

Commit 5f97cf2

Browse files
committed
Optimize ParseByLayout method when the timezone is empty
1 parent 00a5f8a commit 5f97cf2

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

parser.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
// Parse parses a standard time string as a Carbon instance.
99
// 将标准格式时间字符串解析成 Carbon 实例
1010
func (c Carbon) Parse(value string, timezone ...string) Carbon {
11-
if value == "" {
11+
if len(value) == 0 {
1212
c.Error = invalidValueError(value)
1313
return c
1414
}
@@ -65,9 +65,12 @@ func (c Carbon) ParseByLayout(value, layout string, timezone ...string) Carbon {
6565
if c.Error != nil {
6666
return c
6767
}
68-
if value == "" {
68+
if len(value) == 0 {
6969
return c
7070
}
71+
if len(layout) == 0 {
72+
layout = defaultLayout
73+
}
7174
if layout == "timestamp" {
7275
timestamp, _ := strconv.ParseInt(value, 10, 64)
7376
return c.CreateFromTimestamp(timestamp)

parser_unit_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package carbon
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
@@ -223,6 +224,7 @@ func TestCarbon_Issue206(t *testing.T) {
223224

224225
for _, tt := range tests {
225226
t.Run(tt.name, func(t *testing.T) {
227+
fmt.Println("err", tt.carbon.Error)
226228
assert.Equalf(t, tt.want, tt.carbon.ToString(PRC), "Parse()")
227229
})
228230
}

0 commit comments

Comments
 (0)