Files
exchange_go/pkg/utility/timehelper/timehelper.go

307 lines
7.8 KiB
Go
Raw Permalink Normal View History

2025-02-06 11:14:33 +08:00
package timehelper
import (
"fmt"
"go-admin/pkg/utility"
"strings"
"time"
)
const (
DATEFORMAT = "2006-01-02"
TIMEFORMAT = "2006-01-02 15:04:05"
MONTHFORMAT = "2006-01"
DATETIMEFORMAT = "2006-01-02 15:04"
TimeNil = "1900-01-01 00:00:00"
DefaultUnix = -62135596800 // 时间戳默认初始值
)
// GetDateUnixDate 返回带毫秒的时间戳如果需要转化为时间类型time,
// 不带毫秒的time.Unix(1663315884651/1000,0)
// 带毫秒的time.Unix(1663315884651/1000, 1000000*(1663315884651%1000))
func GetDateUnixDate(date time.Time) int64 {
return date.UnixMilli()
}
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 s
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
}
// ConvertTimeLocalLong 转本地时间戳输出
func ConvertTimeLocalLong(d1 time.Time) int64 {
d2 := time.Date(d1.Year(), d1.Month(), d1.Day(), d1.Hour(), d1.Minute(), d1.Second(), 0, time.Local)
return d2.Unix()
}
// 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
}
// // ToTimeHour 转为时分秒
// func ToTimeHour(ltime int64) string {
// now := IntToTime(ltime)
// return now.Format("15:04:05")
// }
// ParseTimeStrInLocal 从时间字符串解析时间,默认 当前时间
func ParseTimeStrInLocal(timeStr string, defaultT ...time.Time) time.Time {
t, err := time.ParseInLocation(TIMEFORMAT, timeStr, time.Local)
if err != nil {
if len(defaultT) > 0 {
return defaultT[0]
}
return time.Now()
}
return t
}
// IntToTime 时间戳转为时间类型
func IntToTime(intime int64) time.Time {
return time.Unix(intime/1000, 0)
}
func Int64ToTime(in int64) time.Time {
return time.Unix(in, 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")
}
// 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")
}
func Time2TimeStampMilli(s time.Time) string {
return utility.Int64ToString(s.UnixMilli())
}
// GetWeeHours 返回若干天后的凌晨时间戳 若day=0 则返回当天凌晨的时间
// var cstSh, _ = time.LoadLocation("Asia/Shanghai") //上海时区
func GetWeeHours(day int) time.Time {
t := time.Now() // 当前时间
OnHour := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
return OnHour.AddDate(0, 0, day)
// 另一种写法
// return uint(time.Date(t.Year(), t.Month(), t.Day()+day, 0, 0, 0, 0, t.Location()).Unix())
}
// GetMonths 返回若干月后的凌晨时间戳 若months=0 则返回当月凌晨的时间
func GetMonths(months int) time.Time {
t := time.Now() // 当前时间
OnHour := time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location())
return OnHour.AddDate(0, months, 0)
}
func GetTimeFromStrDate(date string) (year, month, day int) {
d, err := time.Parse(DATEFORMAT, date)
if err != nil {
fmt.Println("出生日期解析错误!")
return 0, 0, 0
}
year = d.Year()
month = int(d.Month())
day = d.Day()
return
}
func GetAge(year int) (age int) {
if year <= 0 {
age = -1
}
nowyear := time.Now().Year()
age = nowyear - year
return
}
// GetWeekDayByNum 根据输入的数字日期返回周XX,字符串隔开的 (0,1,2,3)
func GetWeekDayByNum(weeks string, lang string) []string {
var result []string
if len(weeks) == 0 {
return result
}
weeks = strings.TrimRight(weeks, ",")
arr := strings.Split(weeks, ",")
weekMap := map[string]string{}
switch lang {
case "zh-CN":
weekMap = map[string]string{
"0": "周日",
"1": "周一",
"2": "周二",
"3": "周三",
"4": "周四",
"5": "周五",
"6": "周六",
}
case "zh-HK":
weekMap = map[string]string{
"0": "周日",
"1": "週一",
"2": "週二",
"3": "週三",
"4": "週四",
"5": "週五",
"6": "週六",
}
case "jp":
weekMap = map[string]string{
"0": "日曜日",
"1": "月曜日",
"2": "火曜日",
"3": "水曜日",
"4": "木曜日",
"5": "金曜日",
"6": "土曜日",
}
case "kr":
weekMap = map[string]string{
"0": "일요일",
"1": "월요일",
"2": "화요일",
"3": "수요일",
"4": "목요일",
"5": "금요일",
"6": "토요일",
}
default:
weekMap = map[string]string{
"0": "Sunday",
"1": "Monday",
"2": "Tuesday",
"3": "Wednesday",
"4": "Thursday",
"5": "Friday",
"6": "Saturday",
}
}
for _, a := range arr {
if value, ok := weekMap[a]; ok {
result = append(result, value)
}
}
return result
}
// 时间差计算
func GetDateSub(begin time.Time, end time.Time) time.Duration {
begin1 := GetDateFormat(begin)
end1 := GetDateFormat(end)
return end1.Sub(begin1)
}
// GetDateFormat 格式化日期用来计算时间差
func GetDateFormat(date time.Time) time.Time {
return time.Date(date.Year(), date.Month(), date.Day(), date.Hour(), date.Minute(), date.Second(), 0, time.UTC)
}
// 本周一
func ThisWeek1() time.Time {
// w1 = today - (Weekday+6)%7
today := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.Local)
factor := time.Duration((today.Weekday()+6)%7) * 24 * time.Hour
return today.Add(-factor)
}
// 本月 1号
func ThisMonthD1() time.Time {
// d1 = today - day - 1
today := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.Local)
factor := time.Duration(today.Day()-1) * 24 * time.Hour
return today.Add(-factor)
}
// 近 xx 天
func Latest7Day(day int) time.Time {
today := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.Local)
return today.Add(-(time.Duration(day)) * 24 * time.Hour)
}
func LatestMonth(num int) time.Time {
today := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.Local)
return today.AddDate(0, -num, 0)
}
// 近 30 天
func Latest30Day() time.Time {
today := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.Local)
return today.Add(-30 * 24 * time.Hour)
}
func YMDUnix(t time.Time) int64 {
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()).Unix()
}