2021-02-18 14:32:31 +08:00
|
|
|
package carbon
|
|
|
|
|
|
|
|
import (
|
2023-12-26 22:12:48 +08:00
|
|
|
"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 {
|
2022-10-27 00:38:20 +08:00
|
|
|
if value == "" || value == "0" || value == "0000-00-00 00:00:00" || value == "0000-00-00" || value == "00:00:00" {
|
|
|
|
return c
|
|
|
|
}
|
2022-10-27 14:46:59 +08:00
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-10-27 14:46:59 +08:00
|
|
|
}
|
2022-11-04 10:56:50 +08:00
|
|
|
switch value {
|
|
|
|
case "now":
|
|
|
|
return c.Now(timezone...)
|
|
|
|
case "yesterday":
|
|
|
|
return c.Yesterday(timezone...)
|
|
|
|
case "tomorrow":
|
|
|
|
return c.Tomorrow(timezone...)
|
|
|
|
}
|
2022-11-05 12:24:25 +08:00
|
|
|
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.
|
2022-11-05 12:24:25 +08:00
|
|
|
// 通过格式模板将时间字符串解析成 Carbon 实例
|
2022-04-14 11:32:01 +08:00
|
|
|
func (c Carbon) ParseByFormat(value, format string, timezone ...string) Carbon {
|
2022-04-12 17:38:11 +08:00
|
|
|
carbon := c.ParseByLayout(value, format2layout(format), timezone...)
|
|
|
|
if carbon.Error != nil {
|
|
|
|
carbon.Error = invalidFormatError(value, format)
|
2021-07-23 11:04:56 +08:00
|
|
|
}
|
2022-04-12 17:38:11 +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.
|
2022-11-05 12:24:25 +08:00
|
|
|
// 通过格式模板将时间字符串解析成 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.
|
2022-11-05 12:24:25 +08:00
|
|
|
// 通过布局模板将时间字符串解析成 Carbon 实例
|
2022-04-14 11:32:01 +08:00
|
|
|
func (c Carbon) ParseByLayout(value, layout string, timezone ...string) Carbon {
|
2021-07-31 13:34:27 +08:00
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
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
|
|
|
}
|
2023-12-26 22:12:48 +08:00
|
|
|
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
|
|
|
}
|
2021-09-06 11:05:04 +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
|
|
|
}
|