-
Notifications
You must be signed in to change notification settings - Fork 1
/
time.go
110 lines (95 loc) · 2.94 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
package function
import "time"
var (
TimeLayout string = "2006-01-02 15:04:05"
TimeLayoutYMD string = "2006-01-02"
TimeLayoutHMS string = "15:04:05"
)
func NowDataTime() time.Time {
return time.Now()
}
func NowDataTimeFmtStr() string {
timestamp := time.Now().Unix()
tm := time.Unix(timestamp, 0)
return tm.Format(TimeLayout)
}
func NowDataTimeStr() string {
timestamp := time.Now().Unix()
tm := time.Unix(timestamp, 0)
return tm.Format("20060102150405")
}
// TodayDataTimeStart 今日凌晨时间
func TodayDataTimeStart() (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
}
// TodayDataTimeEnd 今日23:59:59时间
func TodayDataTimeEnd() (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
}
// 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)
}