forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
executable file
·106 lines (99 loc) · 2.99 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package carbon
import (
"strconv"
"strings"
"time"
)
// Parse parse a standard string as a Carbon instance
// 将标准格式时间字符串解析成 Carbon 实例
func (c Carbon) Parse(value string, timezone ...string) Carbon {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
}
if c.Error != nil {
return c
}
layout := DateTimeFormat
if value == "" || value == "0" || value == "0000-00-00 00:00:00" || value == "0000-00-00" || value == "00:00:00" {
return c
}
if len(value) == 10 && strings.Count(value, "-") == 2 {
layout = DateFormat
}
if strings.Index(value, "T") == 10 {
layout = RFC3339Format
}
if _, err := strconv.ParseInt(value, 10, 64); err == nil {
switch len(value) {
case 8:
layout = ShortDateFormat
case 14:
layout = ShortDateTimeFormat
}
}
if c.ParseByLayout(value, layout).Error != nil {
c.Error = invalidValueError(value)
return c
}
return c.ParseByLayout(value, layout)
}
// Parse parse a standard string as a Carbon instance
// 将标准时间字符串解析成 Carbon 实例
func Parse(value string, timezone ...string) Carbon {
return NewCarbon().Parse(value, timezone...)
}
// ParseByFormat parse a string as a Carbon instance by format
// 通过格式化字符将字符串解析成 carbon 实例
func (c Carbon) ParseByFormat(value string, format string, timezone ...string) Carbon {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
}
if c.Error != nil {
return c
}
if value == "" || value == "0" || value == "0000-00-00 00:00:00" || value == "0000-00-00" || value == "00:00:00" {
return c
}
layout := format2layout(format)
if c.ParseByLayout(value, layout).Error != nil {
c.Error = invalidFormatError(value, format)
return c
}
return c.ParseByLayout(value, layout)
}
// ParseByFormat parse a string as a Carbon instance by format
// 通过布局字符将字符串解析成 carbon 实例
func ParseByFormat(value string, format string, timezone ...string) Carbon {
return NewCarbon().ParseByFormat(value, format, timezone...)
}
// ParseByLayout parse a string as a Carbon instance by layout
// 通过布局字符将字符串解析成 carbon 实例
func (c Carbon) ParseByLayout(value string, layout string, timezone ...string) Carbon {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
}
if c.Error != nil {
return c
}
if value == "" || value == "0" || value == "0000-00-00 00:00:00" || value == "0000-00-00" || value == "00:00:00" {
return c
}
tt, err := time.ParseInLocation(layout, value, c.Loc)
if err != nil {
c.Error = invalidLayoutError(value, layout)
return c
}
c.Time = tt
return c
}
// ParseByLayout parse a string as a Carbon instance by layout
// 将布局时间字符串解析成 Carbon 实例
func ParseByLayout(value string, layout string, timezone ...string) Carbon {
return NewCarbon().ParseByLayout(value, layout, timezone...)
}