From 60a78f8eefd8893e151e34cf976f22981d91ec85 Mon Sep 17 00:00:00 2001 From: 王家文 Date: Thu, 23 May 2024 17:59:04 +0800 Subject: [PATCH] feat✨:时间公共库 --- util/ztime/same.go | 30 ++++++++++++++++++++++++++++++ util/ztime/time.go | 105 +++++++++------------------------------------------------------------------------------------------------ 2 files changed, 39 insertions(+), 96 deletions(-) create mode 100644 util/ztime/same.go diff --git a/util/ztime/same.go b/util/ztime/same.go new file mode 100644 index 0000000..8002e19 --- /dev/null +++ b/util/ztime/same.go @@ -0,0 +1,30 @@ +package ztime + +import "time" + +// SameMonth +// @Description: 判断同一月 +// @param dt1 +// @param dt2 +// @return bool +func SameMonth(dt1, dt2 time.Time) bool { + return dt1.Year() == dt2.Year() && dt1.Month() == dt2.Month() +} + +// SameDay +// @Description: 判断同一天 +// @param dt1 +// @param dt2 +// @return bool +func SameDay(dt1, dt2 time.Time) bool { + return dt1.Year() == dt2.Year() && dt1.Month() == dt2.Month() && dt1.Day() == dt2.Day() +} + +// SameHour +// @Description: 判断同一小时 +// @param dt1 +// @param dt2 +// @return bool +func SameHour(dt1, dt2 time.Time) bool { + return dt1.Year() == dt2.Year() && dt1.Month() == dt2.Month() && dt1.Day() == dt2.Day() && dt1.Hour() == dt2.Hour() +} diff --git a/util/ztime/time.go b/util/ztime/time.go index 6049d62..499675e 100644 --- a/util/ztime/time.go +++ b/util/ztime/time.go @@ -18,112 +18,25 @@ func UnixMilli() int64 { return time.Now().UnixMilli() } -// TimeToDateString -// @Description: 时间转日期字符串 -// @param dt -// @return string -func TimeToDateString(dt time.Time) string { - return dt.Format(DtFormatDate) -} - -// TimeToTimeString -// @Description: 时间转时间字符串 -// @param dt -// @return string -func TimeToTimeString(dt time.Time) string { - return dt.Format(DtFormatTime) -} - -// TimeToMonthString -// @Description: 时间转月份字符串 -// @param dt -// @return string -func TimeToMonthString(dt time.Time) string { - return dt.Format(DtFormatMonth) -} - -// TimeToMonth -// @Description: 时间转月份字符串 -// @param dt -// @return string -func TimeToMonth(dt time.Time) int { - return dt.Year()*100 + int(dt.Month()) -} - -// DateStringToTimeE -// @Description: 日期字符串转时间 -// @param dt -// @return time.Time -// @return error -func DateStringToTimeE(dt string) (time.Time, error) { - return time.Parse(DtFormatDate, dt) -} - -// DateStringToTime -// @Description: 时间字符串转时间 -// @param dt -// @return time.Time -// @return error -func DateStringToTime(dt string) time.Time { - if t, err := time.Parse(DtFormatDate, dt); err == nil { +// ToTime 标准字符串转UTC时间 "2006-01-02 15:04:05" +func ToTime(dt string) time.Time { + if t, err := time.Parse(time.DateTime, dt); err == nil { return t } else { return time.Now() } } -// TimeStringToTimeE -// @Description: 时间字符串转时间 -// @param dt -// @return time.Time -// @return error -func TimeStringToTimeE(dt string) (time.Time, error) { - return time.Parse(DtFormatTime, dt) -} - -// TimeStringToTime -// @Description: 时间字符串转时间 -// @param dt -// @return time.Time -// @return error -func TimeStringToTime(dt string) time.Time { - if t, err := time.Parse(DtFormatTime, dt); err == nil { +// ToTimeLocal 标准字符串转本地时间 "2006-01-02 15:04:05" +func ToTimeLocal(dt string) time.Time { + if t, err := time.ParseInLocation(time.DateTime, dt, time.Local); err == nil { return t } else { return time.Now() } } -// TimeTextInt64 把时间转换成数字字符串 比如 20240510113130 -func TimeTextInt64(dt time.Time) int64 { - date := int64(dt.Year())*1_0000 + int64(dt.Month())*100 + int64(dt.Day()) - t := int64(dt.Hour())*1_0000 + int64(dt.Minute())*100 + int64(dt.Second()) - return date*1_000000 + t -} - -// SameMonth -// @Description: 判断同一月 -// @param dt1 -// @param dt2 -// @return bool -func SameMonth(dt1, dt2 time.Time) bool { - return dt1.Year() == dt2.Year() && dt1.Month() == dt2.Month() -} - -// SameDay -// @Description: 判断同一天 -// @param dt1 -// @param dt2 -// @return bool -func SameDay(dt1, dt2 time.Time) bool { - return dt1.Year() == dt2.Year() && dt1.Month() == dt2.Month() && dt1.Day() == dt2.Day() -} - -// SameHour -// @Description: 判断同一小时 -// @param dt1 -// @param dt2 -// @return bool -func SameHour(dt1, dt2 time.Time) bool { - return dt1.Year() == dt2.Year() && dt1.Month() == dt2.Month() && dt1.Day() == dt2.Day() && dt1.Hour() == dt2.Hour() +// ToString 时间转标准字符串 "2006-01-02 15:04:05" +func ToString(dt time.Time) string { + return dt.Format(time.DateTime) } -- libgit2 0.21.0