From 2127978093ec53a07d704857dee83c3df3137038 Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Mon, 26 Jun 2023 19:07:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=9B=B4=E5=A4=9A?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E9=97=B4=E5=A4=84=E7=90=86=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 包括 delta 时间计算、创建特定时间函数等 --- utils/times/helper.go | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/utils/times/helper.go b/utils/times/helper.go index 1b521b2..04d1926 100644 --- a/utils/times/helper.go +++ b/utils/times/helper.go @@ -1,6 +1,19 @@ package times -import "time" +import ( + "time" +) + +const ( + Nanosecond = time.Nanosecond + Microsecond = time.Microsecond + Millisecond = time.Millisecond + Second = time.Second + Minute = time.Minute + Hour = time.Hour + Day = time.Hour * 24 + Week = Day * 7 +) // GetMonthDays 获取一个时间当月共有多少天 func GetMonthDays(t time.Time) int { @@ -64,3 +77,24 @@ func GetMondayZero(t time.Time) time.Time { weekDay := WeekDay(t) return t.AddDate(0, 0, 1-weekDay) } + +// Date 返回一个特定日期的时间 +func Date(year int, month time.Month, day int) time.Time { + return time.Date(year, month, day, 0, 0, 0, 0, time.Local) +} + +// DateWithHMS 返回一个精确到秒的时间 +func DateWithHMS(year int, month time.Month, day, hour, min, sec int) time.Time { + return time.Date(year, month, day, hour, min, sec, 0, time.Local) +} + +// GetDeltaDay 获取两个时间需要加减的天数 +func GetDeltaDay(t1, t2 time.Time) int { + return int(GetToday(t1).Sub(GetToday(t2)) / time.Hour * 24) +} + +// GetDeltaWeek 获取两个时间需要加减的周数 +func GetDeltaWeek(t1, t2 time.Time) int { + t1, t2 = GetMondayZero(t1), GetMondayZero(t2) + return GetDeltaDay(t1, t2) / 7 +}