Files
web_huangguo2_server/pub/common/timehelper.go
T
2026-09-29 10:47:38 +08:00

56 lines
1.3 KiB
Go

package common
import (
"path/filepath"
"strings"
"time"
)
func BindUrl(hostname string, path ...string) string {
if hostname == "" {
return strings.TrimLeft(filepath.Join(path...), "/")
}
if len(path) == 0 {
return hostname
}
su := strings.TrimLeft(filepath.Join(path...), "/")
pr := strings.TrimRight(hostname, "/")
return pr + "/" + su
}
// 将time.Time转换成int64
func Time2Stamp(t time.Time) int64 {
return t.UnixNano() / 1e9
}
/**时间对象->字符串*/
func Time2Str() string {
const shortForm = "2006-01-01 15:04:05"
t := time.Now()
temp := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), time.Local)
str := temp.Format(shortForm)
return str
}
/*时间戳->字符串*/
func Stamp2Str(stamp int64) string {
timeLayout := "2006-01-02 15:04:05"
str := time.Unix(stamp/1000, 0).Format(timeLayout)
return str
}
/*时间戳->时间对象*/
func Stamp2Time(stamp int64) time.Time {
stampStr := Stamp2Str(stamp)
timer := Str2Time(stampStr)
return timer
}
/**字符串->时间对象*/
func Str2Time(formatTimeStr string) time.Time {
timeLayout := "2006-01-02 15:04:05"
loc, _ := time.LoadLocation("Local")
theTime, _ := time.ParseInLocation(timeLayout, formatTimeStr, loc) //使用模板在对应时区转化为time.time类型
return theTime
}