历史代码:大坑
指标系统的代码使用的时间标准time,.Local
而PC上是Asia/Shanghai,而linux环境为utc
而es存贮的是utc时间,不能简单的用+8,-8
而是计算查询用北京时间,保存按utc\
还有一个问题用的是utc天执行的定时任务
解决办法:测试
func (self *TestGeneralserviceSuite) Test009_MigrationServiceDayAllReport() { var err1 = gtime.SetTimeZone("UTC") if err1 != nil { panic(err1) } golog.Info(time.Now()) var err = report_day.FindBeanReportDayDetail().DeleteDayEs("2024-12-01", "2024-12-15") if err != nil { golog.Error(err) return } var start, _ = gtime.StrToTimeLayout("2024-12-01", time.DateOnly) var end, _ = gtime.StrToTimeLayout("2024-12-16", time.DateOnly) err = self.MigrationServiceDayAllReport(start.Time, end.Time) golog.Info("TestGeneralserviceSuite [Test009_MigrationServiceDayAllReport]", err) }
代码优化
func (self *ReportDayBase) ToShanghai(start, end time.Time) (time.Time, time.Time) { var startShanghai = time.Date(start.Year(), start.Month(), start.Day(), 0, 0, 0, 0, reportkey.ZoneShanghai) var endShanghai = time.Date(end.Year(), end.Month(), end.Day(), 23, 59, 59, 999999, reportkey.ZoneShanghai) return startShanghai, endShanghai }
计算
func (self *ReportDaySupply) MigrationAvabLineDayReport(start, end time.Time) ([]*esentity.ServiceReportDayEs, error) { golog.Info("ReportDayMatch [MigrationAvabLineDayReport]开始统计所有供盘数:", end) var startShanghai, endShanghai = self.ToShanghai(start, end) //yesterday start = startShanghai.AddDate(0, 0, -1) end = endShanghai.AddDate(0, 0, -1) golog.Info("ReportDaySupply [MigrationAvabLineDayReport] start, end = ", start, end) var makeAdd, err = self.makeAdd(start, end) if err != nil { return nil, err } //去重 golog.Info("ReportDaySupply [MigrationAvabLineDayReport]统计所有供盘数结束:") //shopMemberIds = utils.UniqueInt(shopMemberIds) var mapRet = self.mergeList(makeAdd, []*esentity.ServiceReportDayEs{}) return self.map2List(mapRet, start), nil }
保存
func (self *ReportDaySupply) map2List(mapRet report_dto.MapRet, start time.Time) []*esentity.ServiceReportDayEs { list := make([]*esentity.ServiceReportDayEs, 0) var startId = start start = start.UTC() //Add(-8 * time.Hour) for _, data := range mapRet { data.Id = self.ToIdByMemberId(startId, data.ShopMemberId) data.CreatedAt = start.UnixNano() / 1e6 data.CreatedDate = start list = append(list, data) } return list }标签:end,err,大坑,self,二十四,var,start,time,Go From: https://blog.csdn.net/leijmdas/article/details/144471045