diff --git a/script/test_game_charge.go b/script/test_game_charge.go index ac21cb04c5eebaab13e196b0b74266484e801ae8..df2b15f0b09a6dbb6f506ef06700f9cbc5c89e3a 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 6de7826623457313dfdd234e35cdb6e71c54dd20..6dcf6c42a6f55f1d68eb2d858efdce15264ba9f0 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