gitee.com/h79/goutils@v1.22.10/common/timer/timer.go (about) 1 package timer 2 3 import "time" 4 5 const FormatYMD = "2006-01-02 15:04:05" 6 const FormatWMD = "Mon Jan 2 15:04:05 2006" 7 8 // CurrentMS 豪秒 9 func CurrentMS() int64 { 10 return time.Now().Local().UnixMilli() 11 } 12 13 // CurrentS 秒 14 func CurrentS() int64 { 15 return time.Now().Local().Unix() 16 } 17 18 func CurrentTime() string { 19 return time.Now().Local().Format(FormatYMD) 20 } 21 22 func MillToFormat(msec int64) string { 23 t := time.UnixMilli(msec) 24 return t.Local().Format(time.RFC3339) 25 } 26 27 func UnixToFormat(second int64) string { 28 return TimeFormat(time.Unix(second, 0)) 29 } 30 31 func TimeFormat(t time.Time) string { 32 return t.Local().Format(FormatYMD) 33 } 34 35 // TimeFormatNoLocal 36 // 时间转换成日期+时间字符串 (time.Time to "2006-01-02 15:04:05") 37 func TimeFormatNoLocal(t time.Time) string { 38 return t.Format(FormatYMD) 39 } 40 41 // GetTimeUnix 42 // 获取时间戳 return:1464059756 43 func GetTimeUnix(t time.Time) int64 { 44 return t.Local().Unix() 45 } 46 47 // TodayLast 48 // 今天最后时间 23:59:59 49 func TodayLast() time.Time { 50 now := time.Now() 51 return time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, now.Location()) 52 } 53 54 func TodayAt(d time.Duration) time.Time { 55 return TodayLast().Add(d) 56 } 57 58 // Get24Time 59 // 获取当日晚上24点(次日0点)的时间 60 func Get24Time(t time.Time) time.Time { 61 date := TimeToDate(t.Add(time.Hour * 24)) 62 return DateStringToTime(date) 63 } 64 65 // Get24TimeUnix 66 // 获取当日晚上24点(次日0点)的时间戳 67 func Get24TimeUnix(t time.Time) int64 { 68 t24 := Get24Time(t) 69 return t24.Local().Unix() 70 } 71 72 // GetToday24Time 73 // 获取今天晚上24点(次日0点)的时间 74 func GetToday24Time() time.Time { 75 return Get24Time(time.Now()) 76 } 77 78 // GetToday24TimeUnix 79 // 获取今天晚上24点(次日0点)的时间戳 80 func GetToday24TimeUnix() int64 { 81 return Get24TimeUnix(time.Now()) 82 } 83 84 // TimeToDate 85 // 时间转换成日期字符串 (time.Time to "2006-01-02") 86 func TimeToDate(t time.Time) string { 87 return t.Format("2006-01-02") 88 } 89 90 // GetNowDateString 91 // 获取当前时间的日期字符串("2006-01-02") 92 func GetNowDateString() string { 93 return TimeToDate(time.Now()) 94 } 95 96 // GetNowString 97 // 获取当前的时期+时间字符串 98 func GetNowString() string { 99 return TimeFormat(time.Now()) 100 } 101 102 // DateStringToTime 103 // 日期字符串转换成时间 ("2006-01-02" to time.Time) 104 func DateStringToTime(d string) time.Time { 105 t, _ := time.ParseInLocation("2006-01-02", d, time.Local) 106 return t 107 } 108 109 // DateTimeStringToTime 110 // 日期+时间字符串转换成时间 ("2006-01-02 15:04:05" to time.Time) 111 func DateTimeStringToTime(dt string) time.Time { 112 t, _ := time.ParseInLocation(FormatYMD, dt, time.Local) 113 return t 114 } 115 116 // TodayStringToTime 117 // 时间字符串转换成时间 ("15:04:05" to time.Time) 118 func TodayStringToTime(dt string) time.Time { 119 now := time.Now() 120 nowDate := TimeToDate(now) 121 return DateTimeStringToTime(nowDate + " " + dt) 122 } 123 124 // IsWeekend 125 // 是否是周末 126 func IsWeekend(t time.Time) bool { 127 wd := t.Weekday() 128 if wd == time.Sunday || wd == time.Saturday { 129 return true 130 } 131 return false 132 } 133 134 // NowNano returns the current Unix time in nanoseconds. 135 func NowNano() int64 { 136 return time.Now().UnixNano() 137 } 138 139 func Seconds(seconds int) time.Duration { 140 return time.Duration(seconds) * time.Second 141 } 142 143 func Minutes(minutes int) time.Duration { 144 return time.Duration(minutes) * time.Minute 145 } 146 147 func Hours(hours int) time.Duration { 148 return time.Duration(hours) * time.Hour 149 }