carbon/parser.go

89 lines
2.8 KiB
Go
Raw Normal View History

2021-02-18 14:32:31 +08:00
package carbon
import (
"strconv"
"strings"
2021-03-25 17:56:25 +08:00
"time"
2021-02-18 14:32:31 +08:00
)
2021-08-10 10:58:43 +08:00
// Parse parses a standard 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 {
2021-02-18 14:32:31 +08:00
layout := DateTimeFormat
if _, err := strconv.ParseInt(value, 10, 64); err == nil {
switch len(value) {
case 8:
layout = ShortDateFormat
case 14:
layout = ShortDateTimeFormat
}
} else {
switch {
case len(value) == 10 && strings.Count(value, "-") == 2:
layout = DateFormat
case len(value) == 25 && strings.Index(value, "T") == 10:
layout = RFC3339Format
case len(value) == 29 && strings.Index(value, "T") == 10 && strings.Index(value, ".") == 19:
layout = RFC3339MilliFormat
case len(value) == 32 && strings.Index(value, "T") == 10 && strings.Index(value, ".") == 19:
layout = RFC3339MicroFormat
case len(value) == 35 && strings.Index(value, "T") == 10 && strings.Index(value, ".") == 19:
layout = RFC3339NanoFormat
}
2021-02-18 14:32:31 +08:00
}
carbon := c.ParseByLayout(value, layout, timezone...)
if carbon.Error != nil {
carbon.Error = invalidValueError(value)
2021-07-23 11:04:56 +08:00
}
return carbon
2021-02-18 14:32:31 +08:00
}
2021-08-10 10:58:43 +08:00
// Parse parses a standard 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
}
2021-08-10 10:58:43 +08:00
// ParseByFormat parses a string as a Carbon instance by format.
2021-07-28 15:18:05 +08:00
// 通过格式化字符将字符串解析成 carbon 实例
2021-07-23 11:04:56 +08:00
func (c Carbon) ParseByFormat(value string, 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
}
2021-08-10 10:58:43 +08:00
// ParseByFormat parses a string as a Carbon instance by format.
2021-07-28 15:18:05 +08:00
// 通过布局字符将字符串解析成 carbon 实例
2021-07-23 11:04:56 +08:00
func ParseByFormat(value string, format string, timezone ...string) Carbon {
return NewCarbon().ParseByFormat(value, format, timezone...)
2021-02-18 14:32:31 +08:00
}
2021-08-10 10:58:43 +08:00
// ParseByLayout parses a string as a Carbon instance by layout.
2021-07-28 15:18:05 +08:00
// 通过布局字符将字符串解析成 carbon 实例
2021-07-23 11:04:56 +08:00
func (c Carbon) ParseByLayout(value string, layout string, timezone ...string) Carbon {
if len(timezone) > 0 {
2021-08-16 09:47:46 +08:00
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
2021-07-23 11:04:56 +08:00
}
if c.Error != nil {
return c
}
2021-07-19 09:54:47 +08:00
if value == "" || value == "0" || value == "0000-00-00 00:00:00" || value == "0000-00-00" || value == "00:00:00" {
return c
2021-02-18 14:32:31 +08:00
}
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
}
2021-08-10 10:58:43 +08:00
// ParseByLayout parses a string as a Carbon instance by layout.
2021-07-28 15:18:05 +08:00
// 将布局时间字符串解析成 Carbon 实例
2021-07-23 11:04:56 +08:00
func ParseByLayout(value string, layout string, timezone ...string) Carbon {
return NewCarbon().ParseByLayout(value, layout, timezone...)
2021-02-18 14:32:31 +08:00
}