-
Notifications
You must be signed in to change notification settings - Fork 2
/
time.go
69 lines (57 loc) · 1.56 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
package util
import (
"fmt"
"time"
)
// GetTime 获取当前时间戳
func GetTime() int64 {
return time.Now().Unix()
}
// GetMicrotime 获取微秒时间
func GetMicrotime() string {
return fmt.Sprintf("%d", time.Now().UnixNano())
}
// GetMillsTime 获取当前微秒时间
// 名称拼写错误了,不知道以前有哪些地方调用,以后的项目中废弃
func GetMillsTime() int64 {
return time.Now().UnixNano() / 1000000
}
// GetMilliTime 获取当前毫秒时间
func GetMilliTime() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}
// GetTimestamp 获取当前格式化时间
func GetTimestamp() string {
return time.Now().Format("2006-01-02 15:04:05")
}
// FormatUnixTime 将时间戳格式化
func FormatUnixTime(unixTime int64) string {
return time.Unix(unixTime, 0).Format("2006-01-02 15:04:05")
}
// GetYMD 获取当前年月日
func GetYMD() string {
return time.Now().Format("20060102")
}
// GetChinaWeekDay 获取中国的星期几
func GetChinaWeekDay() int {
weekDay := time.Now().Weekday()
if weekDay == time.Sunday {
return 7
}
return int(weekDay)
}
// GetTodayStartTime 获取当天开始时间
func GetTodayStartTime() int64 {
timeStr := time.Now().Format("2006-01-02")
fmt.Println("timeStr:", timeStr)
t, _ := time.Parse("2006-01-02", timeStr)
return t.Unix()
}
// GetChinaWeekStartTime 获取中国的本周一开始时间
func GetChinaWeekStartTime() int64 {
// 获取今天是周几
weekday := GetChinaWeekDay()
// 获取当天开始
startTime := GetTodayStartTime()
return startTime - int64((weekday-1)*86400)
}