From 987a0fb25afa49b835a41b3eace2017650388318 Mon Sep 17 00:00:00 2001 From: Rosy-zhudan Date: Wed, 9 Oct 2019 11:17:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=97=B6=E9=97=B4=E8=8C=83?= =?UTF-8?q?=E5=9B=B4=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/utils.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/utils/utils.go b/utils/utils.go index cba4891f..31e755b5 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -97,6 +97,27 @@ func GetCurDate() time.Time { return time.Date(year, month, day, 0, 0, 0, 0, now.Location()) } +func GetDate(dateTime time.Time) time.Time { + year, month, day := dateTime.Date() + return time.Date(year, month, day, 0, 0, 0, 0, dateTime.Location()) +} + +func GetTimeRange(baseTime time.Time, dayNum int, includeBaseTime bool) (beginTime, endTime time.Time) { + if dayNum <= 0 { + dayNum = 1 + } + baseTime = GetDate(baseTime) + if includeBaseTime { + dayNum = dayNum - 1 + endTime = baseTime.Add(time.Hour*24 - time.Second) + } else { + endTime = baseTime.Add(-time.Second) + } + beginTime = baseTime.Add(-time.Hour * 24 * time.Duration(dayNum)) + + return beginTime, endTime +} + // timestamp is in second func GetCurTimestamp() int64 { return time.Now().Unix()