This commit is contained in:
2025-02-06 11:14:33 +08:00
commit 07847a2d9e
535 changed files with 65131 additions and 0 deletions

View File

@ -0,0 +1,119 @@
package timehelper
import "time"
//GetDateUnixDate 返回带毫秒的时间戳如果需要转化为时间类型time,
//不带毫秒的time.Unix(1663315884651/1000,0)
//带毫秒的time.Unix(1663315884651/1000, 1000000*(1663315884651%1000))
func GetDateUnixDate(date time.Time) int64 {
return date.UnixMilli() //Unix() //* 1000
}
//GetUnixDate 时间撮转化为time类型
func GetUnixDate(date int64) time.Time {
return time.Unix(date/1000, 1000000*(date%1000))
}
func ConvertTimeLocalSec(date int64) time.Time {
d1 := time.Unix(date/1000, 1000000*(date%1000))
d2 := time.Date(d1.Year(), d1.Month(), d1.Day(), d1.Hour(), d1.Minute(), d1.Second(), d1.Nanosecond(), time.Local)
return d2
}
//TimeSubDays 时间间隔天数
func TimeSubDays(t1, t2 time.Time) int {
if t1.Location().String() != t2.Location().String() {
return -1
}
hours := t1.Sub(t2).Hours()
if hours <= 0 {
return -1
}
// sub hours less than 24
if hours < 24 {
// may same day
t1y, t1m, t1d := t1.Date()
t2y, t2m, t2d := t2.Date()
isSameDay := t1y == t2y && t1m == t2m && t1d == t2d
if isSameDay {
return 0
}
return 1
}
// equal or more than 24
if (hours/24)-float64(int(hours/24)) == 0 { // just 24's times
return int(hours / 24)
}
// more than 24 hours
return int(hours/24) + 1
}
//ConvertTimeLocal 时间在数据库返回给结构体的时候,结构体的时间时区是非本地的,需要转化为本地的再转变为时间戳
func ConvertTimeLocal(d1 time.Time) time.Time {
d2 := time.Date(d1.Year(), d1.Month(), d1.Day(), d1.Hour(), d1.Minute(), d1.Second(), 0, time.Local)
return d2
}
//ConvertTimeDayLocal s
func ConvertTimeDayLocal(d1 time.Time) time.Time {
d2 := time.Date(d1.Year(), d1.Month(), d1.Day(), 0, 0, 0, 0, time.Local)
return d2
}
//转为时分秒
func ToTimeHour(ltime int64) string {
now := IntToTime(ltime)
return now.Format("15:04:05")
}
// func ParseInLocation(layout, value string, loc *Location) (Time, error) (layout已带时区时可直接用Parse)
//time.ParseInLocation("2006-01-02 15:04:05", "2017-05-11 14:06:06", time.Local)
//转为时间类型
func IntToTime(intime int64) time.Time {
return time.Unix(intime/1000, 0)
}
//GetPastDay 当前时间算起过去num天内的开始日期、结束日期
func GetPastDay(num int) (start, end time.Time) {
tn := time.Now()
//当前时间,天数为单位
nowday := time.Date(tn.Year(), tn.Month(), tn.Day(), 0, 0, 0, 0, time.Local)
//过去30天
oldTime := nowday.AddDate(0, 0, -num)
return oldTime, nowday
}
//ConvertTimeToString 格式时间返回前台yyyy-mm-dd hh:mm:ss
func ConvertTimeToString(t time.Time) string {
return t.Format("2006-01-02 15:04:05")
}
//ConvertStringToTime 格式时间返回前台yyyy-mm-dd hh:mm:ss
func ConvertStringToTime(t string) time.Time {
t2, _ := time.Parse("2006-01-02 15:04:05", t)
return t2
}
//ConvertTimeToStringMin 格式时间返回前台yyyy-mm-dd hh:mm:ss
func ConvertTimeToStringMin(t time.Time) string {
return t.Format("2006-01-02 15:04")
}
//ConvertTimeToStringDay 格式时间返回前台yyyy-mm-dd
func ConvertTimeToStringDay(t time.Time) string {
return t.Format("2006-01-02")
}
//ConvertTimeToString2 格式时间返回前台yyyy.mm.dd hh:mm 2020.06.02 17:50
func ConvertTimeToString2(t time.Time) string {
return t.Format("2006.01.02 15:04")
}
//ConvertTimeToStringYear 格式时间返回前台mm-dd yyyy
func ConvertTimeToStringYear(s time.Time) string {
return s.Format("01-02 2006")
}