Skip to content

Commit

Permalink
Optimize ParseByLayout method when the timezone is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
gouguoyin committed Oct 11, 2024
1 parent 00a5f8a commit 5f97cf2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 5 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions parser_unit_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package carbon

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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()")
})
}
Expand Down

0 comments on commit 5f97cf2

Please sign in to comment.