From ea614646b044b3c84dc423a778985ac3a66c9c28 Mon Sep 17 00:00:00 2001 From: hujiebin Date: Tue, 7 Mar 2023 17:04:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8B=E4=B8=AA=E6=9C=88=E6=9C=88=E5=BA=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script/test_game_charge.go | 2 +- utils/time.go | 28 +++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/script/test_game_charge.go b/script/test_game_charge.go index ac21cb0..df2b15f 100644 --- a/script/test_game_charge.go +++ b/script/test_game_charge.go @@ -17,7 +17,7 @@ type Msg struct { func main() { var rpcLogs []RpcLog if err := mysql.ProdReadOnlyDB.Table("rpc_log_202303 ").Where("`type` = 146"). - Where("created_time >= ? and created_time < ?", "2023-03-02", "2023-03-03").Find(&rpcLogs).Error; err != nil { + Where("created_time >= ? and created_time < ?", "2023-03-06", "2023-03-07").Find(&rpcLogs).Error; err != nil { panic(err) } var data = make(map[int]int) diff --git a/utils/time.go b/utils/time.go index 6de7826..6dcf6c4 100644 --- a/utils/time.go +++ b/utils/time.go @@ -1,6 +1,9 @@ package utils -import "time" +import ( + "github.com/jinzhu/now" + "time" +) const DEFAULT_LANG = "en" const DATETIME_FORMAT = "2006-01-02 15:04:05" @@ -27,3 +30,26 @@ func GetLastDayOfWeek(t time.Time, n time.Weekday) time.Time { func GetMonday(t time.Time) time.Time { return GetLastDayOfWeek(t, time.Monday) } + +// 增加年/月 +// 因为golang原生的Time.AddDate增加月份的时候有bug +func AddDate(t time.Time, years int, months int) time.Time { + year, month, day := t.Date() + hour, min, sec := t.Clock() + + // firstDayOfMonthAfterAddDate: years 年,months 月后的 那个月份的1号 + firstDayOfMonthAfterAddDate := time.Date(year+years, month+time.Month(months), 1, + hour, min, sec, t.Nanosecond(), t.Location()) + // firstDayOfMonthAfterAddDate 月份的最后一天 + lastDay := now.New(firstDayOfMonthAfterAddDate).EndOfMonth().Day() + + // 如果 t 的天 > lastDay,则设置为lastDay + // 如:t 为 2020-03-31 12:00:00 +0800,增加1个月,为4月31号 + // 但是4月没有31号,则设置为4月最后一天lastDay(30号) + if day > lastDay { + day = lastDay + } + + return time.Date(year+years, month+time.Month(months), day, + hour, min, sec, t.Nanosecond(), t.Location()) +} \ No newline at end of file -- 2.22.0