refactor: 优化时间解析方法

- 用 range 代替索引访问 layouts 数组,提高代码可读性
- 移除冗余的 ParseWithLayouts 和 ParseWithFormats 函数
This commit is contained in:
gouguoyin
2025-07-06 15:33:40 +08:00
parent 4da0ef6f1e
commit 5db3b2009a
2 changed files with 9 additions and 68 deletions

View File

@@ -35,10 +35,10 @@ func Parse(value string, timezone ...string) *Carbon {
return Tomorrow().SetLocation(loc)
}
c := NewCarbon().SetLocation(loc)
for i := range defaultLayouts {
if tt, err = time.ParseInLocation(defaultLayouts[i], value, loc); err == nil {
for _, layout := range defaultLayouts {
if tt, err = time.ParseInLocation(layout, value, loc); err == nil {
c.time = tt
c.currentLayout = defaultLayouts[i]
c.currentLayout = layout
return c
}
}
@@ -48,8 +48,7 @@ func Parse(value string, timezone ...string) *Carbon {
// ParseByLayout parses a time string as a Carbon instance by a confirmed layout.
//
// Note: it will not support parsing timestamp string in the future,
// use "CreateFromTimestamp" or "CreateFromTimestampXXX" instead
// Note: it doesn't support parsing timestamp string.
func ParseByLayout(value, layout string, timezone ...string) *Carbon {
if value == "" {
return &Carbon{isEmpty: true}
@@ -58,7 +57,6 @@ func ParseByLayout(value, layout string, timezone ...string) *Carbon {
return &Carbon{Error: ErrEmptyLayout()}
}
var (
ts int64
tz string
tt StdTime
loc *Location
@@ -73,31 +71,6 @@ func ParseByLayout(value, layout string, timezone ...string) *Carbon {
return &Carbon{Error: err}
}
// timestamp layouts
switch layout {
case TimestampLayout:
if ts, err = parseTimestamp(value); err != nil {
return &Carbon{Error: err}
}
return CreateFromTimestamp(ts).SetLocation(loc)
case TimestampMilliLayout:
if ts, err = parseTimestamp(value); err != nil {
return &Carbon{Error: err}
}
return CreateFromTimestampMilli(ts).SetLocation(loc)
case TimestampMicroLayout:
if ts, err = parseTimestamp(value); err != nil {
return &Carbon{Error: err}
}
return CreateFromTimestampMicro(ts).SetLocation(loc)
case TimestampNanoLayout:
if ts, err = parseTimestamp(value); err != nil {
return &Carbon{Error: err}
}
return CreateFromTimestampNano(ts).SetLocation(loc)
}
// other layouts
if tt, err = time.ParseInLocation(layout, value, loc); err != nil {
return &Carbon{Error: fmt.Errorf("%w: %w", ErrMismatchedLayout(value, layout), err)}
}
@@ -151,10 +124,10 @@ func ParseByLayouts(value string, layouts []string, timezone ...string) *Carbon
return &Carbon{Error: err}
}
c := NewCarbon().SetLocation(loc)
for i := range layouts {
if tt, err = time.ParseInLocation(layouts[i], value, loc); err == nil {
for _, layout := range layouts {
if tt, err = time.ParseInLocation(layout, value, loc); err == nil {
c.time = tt
c.currentLayout = layouts[i]
c.currentLayout = layout
return c
}
}
@@ -185,22 +158,8 @@ func ParseByFormats(value string, formats []string, timezone ...string) *Carbon
return &Carbon{Error: err}
}
var layouts []string
for i := range formats {
layouts = append(layouts, format2layout(formats[i]))
for _, v := range formats {
layouts = append(layouts, format2layout(v))
}
return ParseByLayouts(value, layouts, tz)
}
// ParseWithLayouts parses a time string as a Carbon instance by multiple fuzzy layouts.
//
// Deprecated: it will be removed in the future, use "ParseByLayouts" instead.
func ParseWithLayouts(value string, layouts []string, timezone ...string) *Carbon {
return ParseByLayouts(value, layouts, timezone...)
}
// ParseWithFormats parses a time string as a Carbon instance by multiple fuzzy formats.
//
// Deprecated: it will be removed in the future, use "ParseByFormats" instead.
func ParseWithFormats(value string, formats []string, timezone ...string) *Carbon {
return ParseByFormats(value, formats, timezone...)
}

View File

@@ -48,21 +48,12 @@ func ExampleParseByLayout() {
fmt.Println(carbon.ParseByLayout("It is 2020-08-05 13:14:15", "It is 2006-01-02 15:04:05").ToString())
fmt.Println(carbon.ParseByLayout("今天是 2020年08月05日13时14分15秒", "今天是 2006年01月02日15时04分05秒").ToString())
fmt.Println(carbon.ParseByLayout("1699677240", carbon.TimestampLayout).ToString())
fmt.Println(carbon.ParseByLayout("1699677240666", carbon.TimestampMilliLayout).ToString())
fmt.Println(carbon.ParseByLayout("1699677240666666", carbon.TimestampMicroLayout).ToString())
fmt.Println(carbon.ParseByLayout("1699677240666666666", carbon.TimestampNanoLayout).ToString())
// Output:
// 2020-08-05 00:00:00 +0000 UTC
// 2020-08-05 13:14:15 +0800 CST
// 2020-08-05 13:14:15 +0000 UTC
// 2020-08-05 13:14:15 +0000 UTC
// 2020-08-05 13:14:15 +0000 UTC
// 2023-11-11 04:34:00 +0000 UTC
// 2023-11-11 04:34:00.666 +0000 UTC
// 2023-11-11 04:34:00.666666 +0000 UTC
// 2023-11-11 04:34:00.666666666 +0000 UTC
}
func ExampleParseByFormat() {
@@ -73,21 +64,12 @@ func ExampleParseByFormat() {
fmt.Println(carbon.ParseByFormat("It is 2020-08-05 13:14:15", "\\I\\t \\i\\s Y-m-d H:i:s").ToString())
fmt.Println(carbon.ParseByFormat("今天是 2020年08月05日13时14分15秒", "今天是 Y年m月d日H时i分s秒").ToString())
fmt.Println(carbon.ParseByFormat("1699677240", carbon.TimestampFormat).ToString())
fmt.Println(carbon.ParseByFormat("1699677240666", carbon.TimestampMilliFormat).ToString())
fmt.Println(carbon.ParseByFormat("1699677240666666", carbon.TimestampMicroFormat).ToString())
fmt.Println(carbon.ParseByFormat("1699677240666666666", carbon.TimestampNanoFormat).ToString())
// Output:
// 2020-08-05 00:00:00 +0000 UTC
// 2020-08-05 13:14:15 +0800 CST
// 2020-08-05 13:14:15 +0000 UTC
// 2020-08-05 13:14:15 +0000 UTC
// 2020-08-05 13:14:15 +0000 UTC
// 2023-11-11 04:34:00 +0000 UTC
// 2023-11-11 04:34:00.666 +0000 UTC
// 2023-11-11 04:34:00.666666 +0000 UTC
// 2023-11-11 04:34:00.666666666 +0000 UTC
}
func ExampleParseByLayouts() {