158 lines
4.0 KiB
Go
158 lines
4.0 KiB
Go
/*
|
||
* @FileName: getTime.go
|
||
* @Author: JJXu
|
||
* @CreateTime: 2022/3/1 下午6:35
|
||
* @Description:
|
||
*/
|
||
|
||
package stime
|
||
|
||
import (
|
||
"fmt"
|
||
"time"
|
||
)
|
||
|
||
func StrNowDate() string {
|
||
return TimeToString(time.Now(), Format_Normal_YMD)
|
||
}
|
||
|
||
func StrNowYearMonth() string {
|
||
return TimeToString(time.Now(), Format_Normal_YM)
|
||
}
|
||
|
||
// ThisMorming 今天凌晨
|
||
func ThisMorming(format string) (strTime string) {
|
||
thisTime := time.Now()
|
||
year := thisTime.Year()
|
||
month := MonthStrMap[thisTime.Month().String()]
|
||
day := fmt.Sprintf("%02d", thisTime.Day())
|
||
strTime = fmt.Sprintf("%v-%v-%v 00:00:00", year, month, day)
|
||
if format != Format_Normal_YMDhms {
|
||
t1, _ := time.ParseInLocation(Format_Normal_YMDhms, strTime, Loc.Shanghai())
|
||
strTime = t1.Format(format)
|
||
}
|
||
return strTime
|
||
}
|
||
|
||
// ThisMorningUnix 获取当日凌晨的时间戳
|
||
func ThisMorningToUnix() int64 {
|
||
thist := time.Now()
|
||
zero_tm := time.Date(thist.Year(), thist.Month(), thist.Day(), 0, 0, 0, 0, thist.Location()).Unix()
|
||
return zero_tm
|
||
}
|
||
|
||
// TomorrowMorning 第二天凌晨
|
||
func TomorrowMorning(baseTime time.Time) *time.Time {
|
||
year := baseTime.Year()
|
||
month := MonthStrMap[baseTime.Month().String()]
|
||
day := fmt.Sprintf("%02d", baseTime.Day()+1)
|
||
strTime := fmt.Sprintf("%v-%v-%v 00:00:00", year, month, day)
|
||
res, _ := StringToTime(strTime)
|
||
return res
|
||
}
|
||
|
||
// ThisTimeUnix 获取当前时间的时间戳
|
||
func CurrentimeToUnix() int64 {
|
||
return time.Now().Unix()
|
||
}
|
||
|
||
// Currentime 获取当前时间
|
||
func Currentime(format string) (strTime string) {
|
||
strTime = time.Now().Format(format)
|
||
return
|
||
}
|
||
|
||
// HoursAgo 若干小时之前的时间
|
||
func HoursAgo(hours time.Duration, format string) (lastTimeStr string) {
|
||
lastStamp := time.Now().Unix() - int64((time.Hour * hours).Seconds())
|
||
lastTime := time.Unix(lastStamp, 0).In(Loc.Shanghai())
|
||
lastTimeStr = lastTime.Format(format)
|
||
return
|
||
}
|
||
|
||
// TimeToString 时间转字符串
|
||
func TimeToString(t time.Time, format string) string {
|
||
return t.Format(format)
|
||
}
|
||
|
||
// 计算指定月份的天数
|
||
func YearMonthToDayNumber(year int, month int) int {
|
||
// 有31天的月份
|
||
day31 := map[int]bool{
|
||
1: true,
|
||
3: true,
|
||
5: true,
|
||
7: true,
|
||
8: true,
|
||
10: true,
|
||
12: true,
|
||
}
|
||
if day31[month] == true {
|
||
return 31
|
||
}
|
||
// 有30天的月份
|
||
day30 := map[int]bool{
|
||
4: true,
|
||
6: true,
|
||
9: true,
|
||
11: true,
|
||
}
|
||
if day30[month] == true {
|
||
return 30
|
||
}
|
||
// 计算是平年还是闰年
|
||
if (year%4 == 0 && year%100 != 0) || year%400 == 0 {
|
||
// 得出2月的天数
|
||
return 29
|
||
}
|
||
// 得出2月的天数
|
||
return 28
|
||
}
|
||
|
||
// 求时间差(返回一个数字,该数字单位由传过来的unit决定。若unit为60,则单位是分钟。)
|
||
func GetDiffTime(start_time string, end_time string, unit int64) int64 {
|
||
// 转成时间戳
|
||
if len(start_time) == 10 {
|
||
start_time = fmt.Sprintf("%v 00:00:00", start_time)
|
||
}
|
||
if len(end_time) == 10 {
|
||
end_time = fmt.Sprintf("%v 00:00:00", end_time)
|
||
}
|
||
startUnix, _ := time.ParseInLocation("2006-01-02 15:04:05", start_time, Loc.Shanghai())
|
||
endUnix, _ := time.ParseInLocation("2006-01-02 15:04:05", end_time, Loc.Shanghai())
|
||
startTime := startUnix.Unix()
|
||
endTime := endUnix.Unix()
|
||
// 求相差天数
|
||
date := (endTime - startTime) / unit
|
||
return date
|
||
}
|
||
|
||
func TimeSince(pastTime string, format string) string {
|
||
|
||
t, err := time.Parse(format, pastTime)
|
||
if err != nil {
|
||
return ""
|
||
}
|
||
|
||
diff := time.Since(t)
|
||
if diff.Minutes() < 15 {
|
||
return "刚刚"
|
||
} else if diff.Minutes() >= 15 && diff.Minutes() < 30 {
|
||
return "15分钟前"
|
||
} else if diff.Minutes() >= 30 && diff.Minutes() < 60 {
|
||
return "半小时前"
|
||
} else if diff.Hours() >= 1 && diff.Hours() < 24 {
|
||
return fmt.Sprintf("%d小时前", int(diff.Hours()))
|
||
} else if diff.Hours() >= 24 && diff.Hours() < 24*15 {
|
||
return fmt.Sprintf("%d天前", int(diff.Hours()/24))
|
||
} else if diff.Hours() < 24*30 {
|
||
return "半月前"
|
||
} else if diff.Hours() < 24*30*6 { //小于半年
|
||
return fmt.Sprintf("%d月前", int(diff.Hours()/24/30))
|
||
} else if diff.Hours() < 24*365 { //小于1年
|
||
return "半年前"
|
||
} else {
|
||
return t.Format("2006-01-02 15:04:05")
|
||
}
|
||
}
|