From c0efe8d3a30851374b45fdb7ff8dbff9a7489c26 Mon Sep 17 00:00:00 2001 From: jakub961241 <144362244+jakub961241@users.noreply.github.com> Date: Sun, 22 Mar 2026 01:37:33 +0100 Subject: [PATCH] fix: monitor time-based search timezone offset (#11891) When searching monitor data by time range, the results were off by the server's timezone offset (e.g., 8 hours for UTC+8). The previous code used time.In(loc) which only changes the display timezone but doesn't adjust the actual moment in time for the DB query. The fix: when the frontend sends time in UTC but the server is in a different timezone, reconstruct the time.Time with the local timezone so the DB query matches the correct time range. This treats the frontend's time values as "wall clock time" in the server's timezone. Applied to both LoadMonitorData and LoadGPUMonitorData. Fixes #11891 --- agent/app/service/monitor.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/agent/app/service/monitor.go b/agent/app/service/monitor.go index a619c6c5de3e..aaa79eb46773 100644 --- a/agent/app/service/monitor.go +++ b/agent/app/service/monitor.go @@ -58,8 +58,14 @@ func NewIMonitorService() IMonitorService { func (m *MonitorService) LoadMonitorData(req dto.MonitorSearch) ([]dto.MonitorData, error) { loc, _ := time.LoadLocation(common.LoadTimeZoneByCmd()) - req.StartTime = req.StartTime.In(loc) - req.EndTime = req.EndTime.In(loc) + if req.StartTime.Location() == time.UTC && loc != time.UTC { + req.StartTime = time.Date(req.StartTime.Year(), req.StartTime.Month(), req.StartTime.Day(), + req.StartTime.Hour(), req.StartTime.Minute(), req.StartTime.Second(), req.StartTime.Nanosecond(), loc) + } + if req.EndTime.Location() == time.UTC && loc != time.UTC { + req.EndTime = time.Date(req.EndTime.Year(), req.EndTime.Month(), req.EndTime.Day(), + req.EndTime.Hour(), req.EndTime.Minute(), req.EndTime.Second(), req.EndTime.Nanosecond(), loc) + } var data []dto.MonitorData if req.Param == "all" || req.Param == "cpu" || req.Param == "memory" || req.Param == "load" { @@ -182,8 +188,14 @@ func (m *MonitorService) LoadGPUOptions() dto.MonitorGPUOptions { func (m *MonitorService) LoadGPUMonitorData(req dto.MonitorGPUSearch) (dto.MonitorGPUData, error) { loc, _ := time.LoadLocation(common.LoadTimeZoneByCmd()) - req.StartTime = req.StartTime.In(loc) - req.EndTime = req.EndTime.In(loc) + if req.StartTime.Location() == time.UTC && loc != time.UTC { + req.StartTime = time.Date(req.StartTime.Year(), req.StartTime.Month(), req.StartTime.Day(), + req.StartTime.Hour(), req.StartTime.Minute(), req.StartTime.Second(), req.StartTime.Nanosecond(), loc) + } + if req.EndTime.Location() == time.UTC && loc != time.UTC { + req.EndTime = time.Date(req.EndTime.Year(), req.EndTime.Month(), req.EndTime.Day(), + req.EndTime.Hour(), req.EndTime.Minute(), req.EndTime.Second(), req.EndTime.Nanosecond(), loc) + } var data dto.MonitorGPUData gpuList, err := monitorRepo.GetGPU(repo.WithByCreatedAt(req.StartTime, req.EndTime), monitorRepo.WithByProductName(req.ProductName)) if err != nil {