carbon/parser.go

105 lines
2.9 KiB
Go
Raw Permalink Normal View History

2021-02-18 14:32:31 +08:00
package carbon
import (
"strconv"
2021-03-25 17:56:25 +08:00
"time"
2021-02-18 14:32:31 +08:00
)
2022-10-27 00:38:20 +08:00
// Parse parses a standard time string as a Carbon instance.
2021-07-28 15:18:05 +08:00
// 将标准格式时间字符串解析成 Carbon 实例
2021-07-23 11:04:56 +08:00
func (c Carbon) Parse(value string, timezone ...string) Carbon {
if len(value) == 0 {
2024-04-09 20:15:08 +08:00
c.Error = invalidValueError(value)
2022-10-27 00:38:20 +08:00
return c
}
2022-10-27 14:46:59 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
2022-10-27 14:46:59 +08:00
}
switch value {
case "now":
return c.Now(timezone...)
case "yesterday":
return c.Yesterday(timezone...)
case "tomorrow":
return c.Tomorrow(timezone...)
}
for _, layout := range layouts {
2022-10-27 14:46:59 +08:00
t, err := time.ParseInLocation(layout, value, c.loc)
2022-10-27 00:38:20 +08:00
if err == nil {
2022-10-27 14:46:59 +08:00
c.time = t
2022-10-27 00:38:20 +08:00
return c
}
}
c.Error = invalidValueError(value)
return c
2021-02-18 14:32:31 +08:00
}
2022-10-27 00:38:20 +08:00
// Parse parses a standard time string as a Carbon instance.
2021-07-28 15:18:05 +08:00
// 将标准时间字符串解析成 Carbon 实例
2021-07-23 11:04:56 +08:00
func Parse(value string, timezone ...string) Carbon {
return NewCarbon().Parse(value, timezone...)
2021-02-18 14:32:31 +08:00
}
2022-10-27 00:38:20 +08:00
// ParseByFormat parses a time string as a Carbon instance by format.
// 通过格式模板将时间字符串解析成 Carbon 实例
2022-04-14 11:32:01 +08:00
func (c Carbon) ParseByFormat(value, format string, timezone ...string) Carbon {
carbon := c.ParseByLayout(value, format2layout(format), timezone...)
if carbon.Error != nil {
carbon.Error = invalidFormatError(value, format)
2021-07-23 11:04:56 +08:00
}
return carbon
2021-02-18 14:32:31 +08:00
}
2022-10-27 00:38:20 +08:00
// ParseByFormat parses a time string as a Carbon instance by format.
// 通过格式模板将时间字符串解析成 Carbon 实例
2022-04-14 11:32:01 +08:00
func ParseByFormat(value, format string, timezone ...string) Carbon {
2021-07-23 11:04:56 +08:00
return NewCarbon().ParseByFormat(value, format, timezone...)
2021-02-18 14:32:31 +08:00
}
2022-10-27 00:38:20 +08:00
// ParseByLayout parses a time string as a Carbon instance by layout.
// 通过布局模板将时间字符串解析成 Carbon 实例
2022-04-14 11:32:01 +08:00
func (c Carbon) ParseByLayout(value, layout string, timezone ...string) Carbon {
2024-11-29 11:24:35 +08:00
if len(value) == 0 {
c.Error = invalidValueError(value)
return c
}
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
2021-07-23 11:04:56 +08:00
}
if c.Error != nil {
return c
}
if len(layout) == 0 {
layout = defaultLayout
}
if layout == "timestamp" {
timestamp, _ := strconv.ParseInt(value, 10, 64)
return c.CreateFromTimestamp(timestamp)
}
if layout == "timestampMilli" {
timestamp, _ := strconv.ParseInt(value, 10, 64)
return c.CreateFromTimestampMilli(timestamp)
}
if layout == "timestampMicro" {
timestamp, _ := strconv.ParseInt(value, 10, 64)
return c.CreateFromTimestampMicro(timestamp)
}
if layout == "timestampNano" {
timestamp, _ := strconv.ParseInt(value, 10, 64)
return c.CreateFromTimestampNano(timestamp)
}
2021-08-16 09:47:46 +08:00
tt, err := time.ParseInLocation(layout, value, c.loc)
2021-03-25 17:56:25 +08:00
if err != nil {
2021-07-23 11:04:56 +08:00
c.Error = invalidLayoutError(value, layout)
2021-07-19 09:54:47 +08:00
return c
2021-03-25 17:56:25 +08:00
}
c.time = tt
2021-02-18 14:32:31 +08:00
return c
}
2022-10-27 00:38:20 +08:00
// ParseByLayout parses a time string as a Carbon instance by layout.
// 通过布局模板将时间字符串解析成 Carbon 实例
2022-04-14 11:32:01 +08:00
func ParseByLayout(value, layout string, timezone ...string) Carbon {
2021-07-23 11:04:56 +08:00
return NewCarbon().ParseByLayout(value, layout, timezone...)
2021-02-18 14:32:31 +08:00
}