-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstl.datetime.go
196 lines (166 loc) · 5.53 KB
/
stl.datetime.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package stl
import (
"strings"
"time"
)
// XPDateTimeImpl 日期时间工具对象
type XPDateTimeImpl struct {
}
const (
yyyy = "2006"
yy = "06"
mmmm = "January"
mmm = "Jan"
mm = "01"
dddd = "Monday"
ddd = "Mon"
dd = "02"
HHT = "03"
HH = "15"
MM = "04"
SS = "05"
ss = "05"
III = "000"
iii = "000"
tt = "PM"
Z = "MST"
ZZZ = "MST"
)
func convertFormat(format string) string {
var goFormat = format
if strings.Contains(goFormat, "YYYY") {
goFormat = strings.Replace(goFormat, "YYYY", yyyy, -1)
} else if strings.Contains(goFormat, "yyyy") {
goFormat = strings.Replace(goFormat, "yyyy", yyyy, -1)
} else if strings.Contains(goFormat, "YY") {
goFormat = strings.Replace(goFormat, "YY", yy, -1)
} else if strings.Contains(goFormat, "yy") {
goFormat = strings.Replace(goFormat, "yy", yy, -1)
}
if strings.Contains(goFormat, "MMMM") {
goFormat = strings.Replace(goFormat, "MMMM", mmmm, -1)
} else if strings.Contains(goFormat, "mmmm") {
goFormat = strings.Replace(goFormat, "mmmm", mmmm, -1)
} else if strings.Contains(goFormat, "MMM") {
goFormat = strings.Replace(goFormat, "MMM", mmm, -1)
} else if strings.Contains(goFormat, "mmm") {
goFormat = strings.Replace(goFormat, "mmm", mmm, -1)
} else if strings.Contains(goFormat, "mm") {
goFormat = strings.Replace(goFormat, "mm", mm, -1)
}
if strings.Contains(goFormat, "dddd") {
goFormat = strings.Replace(goFormat, "dddd", dddd, -1)
} else if strings.Contains(goFormat, "ddd") {
goFormat = strings.Replace(goFormat, "ddd", ddd, -1)
} else if strings.Contains(goFormat, "dd") {
goFormat = strings.Replace(goFormat, "dd", dd, -1)
}
if strings.Contains(goFormat, "tt") {
if strings.Contains(goFormat, "HH") {
goFormat = strings.Replace(goFormat, "HH", HHT, -1)
} else if strings.Contains(goFormat, "hh") {
goFormat = strings.Replace(goFormat, "hh", HHT, -1)
}
goFormat = strings.Replace(goFormat, "tt", tt, -1)
} else {
if strings.Contains(goFormat, "HH") {
goFormat = strings.Replace(goFormat, "HH", HH, -1)
} else if strings.Contains(goFormat, "hh") {
goFormat = strings.Replace(goFormat, "hh", HH, -1)
}
goFormat = strings.Replace(goFormat, "tt", "", -1)
}
if strings.Contains(goFormat, "MM") {
goFormat = strings.Replace(goFormat, "MM", MM, -1)
}
if strings.Contains(goFormat, "SS") {
goFormat = strings.Replace(goFormat, "SS", SS, -1)
} else if strings.Contains(goFormat, "ss") {
goFormat = strings.Replace(goFormat, "ss", ss, -1)
}
if strings.Contains(goFormat, "III") {
goFormat = strings.Replace(goFormat, "III", III, -1)
} else if strings.Contains(goFormat, "iii") {
goFormat = strings.Replace(goFormat, "iii", iii, -1)
}
if strings.Contains(goFormat, "ZZZ") {
goFormat = strings.Replace(goFormat, "ZZZ", ZZZ, -1)
} else if strings.Contains(goFormat, "zzz") {
goFormat = strings.Replace(goFormat, "zzz", ZZZ, -1)
} else if strings.Contains(goFormat, "Z") {
goFormat = strings.Replace(goFormat, "Z", Z, -1)
} else if strings.Contains(goFormat, "z") {
goFormat = strings.Replace(goFormat, "z", Z, -1)
}
if strings.Contains(goFormat, "tt") {
goFormat = strings.Replace(goFormat, "tt", tt, -1)
}
return goFormat
}
// StringToTime 按照默认的 yyyy-mm-dd HH:MM:SS 格式将字符串转换为时间
func (instance *XPDateTimeImpl) StringToTime(str string) time.Time {
t, _ := time.ParseInLocation("2006-01-02 15:04:05", str, time.Local)
return t
}
// TimeToString 按照默认的 yyyy-mm-dd HH:MM:SS 格式将时间转换为字符串
func (instance *XPDateTimeImpl) TimeToString(t time.Time) string {
return t.Format("2006-01-02 15:04:05")
}
// StringToTimeFmt 将字符串转换为时间, 需自定义格式
func (instance *XPDateTimeImpl) StringToTimeFmt(str, fmt string) time.Time {
t, _ := time.ParseInLocation(convertFormat(fmt), str, time.Local)
return t
}
// TimeToStringFmt 将时间转换为字符串, 需自定义格式
func (instance *XPDateTimeImpl) TimeToStringFmt(t time.Time, fmt string) string {
return t.Format(convertFormat(fmt))
}
// Timestamp 获取时间戳(到秒共10位)
func (instance *XPDateTimeImpl) Timestamp() int64 {
return time.Now().Unix()
}
// TimestampMillisecond 获取时间戳(到毫秒共13位)
func (instance *XPDateTimeImpl) TimestampMillisecond() int64 {
return time.Now().UnixNano() / 1000000
}
// IsSameDay 是否是同一天
func (instance *XPDateTimeImpl) IsSameDay(t1, t2 time.Time) bool {
y1, m1, d1 := t1.Date()
y2, m2, d2 := t2.Date()
return y1 == y2 && m1 == m2 && d1 == d2
}
// IsSameWeek 判断两个时间是否在同一周
func (instance *XPDateTimeImpl) IsSameWeek(t1, t2 time.Time) bool {
y1, w1 := t1.ISOWeek()
y2, w2 := t2.ISOWeek()
return y1 == y2 && w1 == w2
}
// IsSameMonth 是否同一个月
func (instance *XPDateTimeImpl) IsSameMonth(t1, t2 time.Time) bool {
y1, m1, _ := t1.Date()
y2, m2, _ := t2.Date()
return y1 == y2 && m1 == m2
}
// IsSameYear 是否同一年
func (instance *XPDateTimeImpl) IsSameYear(t1, t2 time.Time) bool {
y1, _, _ := t1.Date()
y2, _, _ := t2.Date()
return y1 == y2
}
// IsToday 是否是今天
func (instance *XPDateTimeImpl) IsToday(t time.Time) bool {
return instance.IsSameDay(t, time.Now())
}
// IsLeapYear 是否为闰年
func (instance *XPDateTimeImpl) IsLeapYear(year int) bool {
return year%4 == 0 && year%100 != 0 || year%400 == 0
}
// ThisMonday 获取本周一
func (instance *XPDateTimeImpl) ThisMonday() time.Time {
now := time.Now()
offset := int(time.Monday - now.Weekday())
if offset > 0 {
offset = -6
}
return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
}