-
Notifications
You must be signed in to change notification settings - Fork 1
/
time.go
197 lines (175 loc) · 5.06 KB
/
time.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
197
package function
import "time"
var (
TimeLayoutYMDHIS string = "20060102150405"
TimeLayout string = "2006-01-02 15:04:05"
TimeLayoutYMD string = "2006-01-02"
TimeLayoutHMS string = "15:04:05"
)
// NowDateTime 当前时间戳
func NowDateTime() time.Time {
return time.Now()
}
// NowDateTimeFmtStr 当前时间格式
func NowDateTimeFmtStr() string {
timestamp := time.Now().Unix()
tm := time.Unix(timestamp, 0)
return tm.Format(TimeLayout)
}
// NowDateTimeStr 当前时间字符串
func NowDateTimeStr() string {
timestamp := time.Now().Unix()
tm := time.Unix(timestamp, 0)
return tm.Format("20060102150405")
}
// TodayDateTimeStart 今日00:00:00时间
func TodayDateTimeStart() (timestamp int64, Time time.Time, timeStr string) {
currentTime := time.Now()
Time = time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), 0, 0, 0, 0, currentTime.Location())
timestamp = Time.Unix()
timeStr = Time.Format(TimeLayout)
return
}
// TodayDateTimeEnd 今日23:59:59时间
func TodayDateTimeEnd() (timestamp int64, Time time.Time, timeStr string) {
currentTime := time.Now()
Time = time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), 23, 59, 59, 0, currentTime.Location())
timestamp = Time.Unix()
timeStr = Time.Format(TimeLayout)
return
}
//TimeStrToTime 将时间字符串转为时间戳和time.Time(例如:2021-08-08 08:08:08)
func TimeStrToTime(str string) (timestamp int64, Time time.Time) {
loc, _ := time.LoadLocation("Local")
Time, _ = time.ParseInLocation(TimeLayout, str, loc)
timestamp = Time.Unix()
return
}
// AddSecondToTime 在当前时间戳的基础上加上秒数,重新生成时间日期
func AddSecondToTime(second int64) (timestamp int64, Time time.Time, timeStr string) {
timestamp = time.Now().Unix() + second
Time = time.Unix(timestamp, 0)
timeStr = Time.Format(TimeLayout)
return
}
// TimestampToTime 时间戳转日期格式
func TimestampToTime(timestamp int64) (Time time.Time, timeStr string) {
Time = time.Unix(timestamp, 0)
timeStr = Time.Format(TimeLayout)
return
}
// NowTime 当前时间
func NowTime() (timestamp int64, Time time.Time, timeStr string) {
timestamp = time.Now().Unix()
Time = time.Unix(timestamp, 0)
timeStr = Time.Format(TimeLayout)
return
}
// TimeSplit 时间格式拆分
func TimeSplit(t time.Time) (ymd, hms string) {
ymd = t.Format(TimeLayoutYMD)
hms = t.Format(TimeLayoutHMS)
return
}
// NowTimeSplit 当前时间格式拆分
func NowTimeSplit() (ymd, hms string) {
var Time time.Time
Time = time.Unix(time.Now().Unix(), 0)
ymd = Time.Format(TimeLayoutYMD)
hms = Time.Format(TimeLayoutHMS)
return
}
// BeforeDayTime N天前的时间
func BeforeDayTime(day int) (timestamp int64, Time time.Time, timeStr string) {
currentTime := time.Now()
Time = currentTime.AddDate(0, 0, -day)
timeStr = Time.Format(TimeLayout)
timestamp = Time.Unix()
return
}
// AfterDayTime N天后的时间
func AfterDayTime(day int) (timestamp int64, Time time.Time, timeStr string) {
currentTime := time.Now()
Time = currentTime.AddDate(0, 0, day)
timeStr = Time.Format(TimeLayout)
timestamp = Time.Unix()
return
}
// BeforeMonthTime N月前的时间
func BeforeMonthTime(month int) (timestamp int64, Time time.Time, timeStr string) {
currentTime := time.Now()
Time = currentTime.AddDate(0, -month, 0)
timeStr = Time.Format(TimeLayout)
timestamp = Time.Unix()
return
}
// AfterMonthTime N月后的时间
func AfterMonthTime(month int) (timestamp int64, Time time.Time, timeStr string) {
currentTime := time.Now()
Time = currentTime.AddDate(0, month, 0)
timeStr = Time.Format(TimeLayout)
timestamp = Time.Unix()
return
}
// DiyTimeFmtStr Diy时间格式
func DiyTimeFmtStr(format string, timestamps ...int64) string {
var timestamp int64
if len(timestamps) == 0 {
timestamp = time.Now().Unix()
} else {
timestamp = timestamps[0]
}
return time.Unix(timestamp, 0).Format(format)
}
// SubMonths 两个时间相隔多少个月 [t1 - t2]
func SubMonths(t1, t2 time.Time) (month int) {
y1 := t1.Year()
y2 := t2.Year()
m1 := int(t1.Month())
m2 := int(t2.Month())
chaY := y1 - y2
chaM := 12 - m2 + m1
month = chaM + ((chaY - 1) * 12)
return
}
// SubMonth 两个时间相隔多少个月 [t1 - t2]
func SubMonth(t1, t2 time.Time) (month int) {
y1 := t1.Year()
y2 := t2.Year()
m1 := int(t1.Month())
m2 := int(t2.Month())
d1 := t1.Day()
d2 := t2.Day()
yearInterval := y1 - y2
// 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
if m1 < m2 || m1 == m2 && d1 < d2 {
yearInterval--
}
// 获取月数差值
monthInterval := (m1 + 12) - m2
if d1 < d2 {
monthInterval--
}
monthInterval %= 12
month = yearInterval*12 + monthInterval
return
}
// SubDays 两个时间相隔多少天 [t1 - t2]
func SubDays(t1, t2 time.Time) (day int) {
swap := false
if t1.Unix() < t2.Unix() {
t_ := t1
t1 = t2
t2 = t_
swap = true
}
day = int(t1.Sub(t2).Hours() / 24)
// 计算在被24整除外的时间是否存在跨自然日
if int(t1.Sub(t2).Milliseconds())%86400000 > int(86400000-t2.Unix()%86400000) {
day += 1
}
if swap {
day = -day
}
return
}