Commit ea614646 authored by hujiebin's avatar hujiebin

下个月月底

parent bee9c6a1
......@@ -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)
......
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment