From 311798b71aac19d5c5cdeaae621eb92e72880836 Mon Sep 17 00:00:00 2001 From: H Vs Date: Tue, 15 Oct 2024 11:20:00 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4GetSampleTimeFromLastUpdate?= =?UTF-8?q?=E7=B2=BE=E5=BA=A6=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HealthMonitor.WebApi/Worker.cs | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/HealthMonitor.WebApi/Worker.cs b/HealthMonitor.WebApi/Worker.cs index 492e64c..9e997f5 100644 --- a/HealthMonitor.WebApi/Worker.cs +++ b/HealthMonitor.WebApi/Worker.cs @@ -2633,20 +2633,32 @@ namespace HealthMonitor.WebApi //} } - private DateTime GetSampleTimeFromLastUpdate(DateTime lastUpdate,int interval) + //private DateTime GetSampleTimeFromLastUpdate(DateTime lastUpdate,int interval) + //{ + // DateTime nowInterval = lastUpdate; + // //if (nowInterval.Second > 0) + // //{ + // // nowInterval = nowInterval.AddMinutes(1); + // //} + // // 计算last_update到上一间隔的分钟数 + // int minutesToSubtract = nowInterval.Minute % interval; + + // // 计算上一间隔的时间 + // DateTime previousInterval = nowInterval.AddMinutes(-minutesToSubtract).AddSeconds(-nowInterval.Second).AddMilliseconds(-nowInterval.Millisecond); + + // return previousInterval; + //} + + public static DateTime GetSampleTimeFromLastUpdate(DateTime lastUpdate, int interval) { - DateTime nowInterval = lastUpdate; - //if (nowInterval.Second > 0) - //{ - // nowInterval = nowInterval.AddMinutes(1); - //} - // 计算last_update到上一间隔的分钟数 - int minutesToSubtract = nowInterval.Minute % interval; + // 获取当前的分钟 + int minute = lastUpdate.Minute; - // 计算上一间隔的时间 - DateTime previousInterval = nowInterval.AddMinutes(-minutesToSubtract).AddSeconds(-nowInterval.Second).AddMilliseconds(-nowInterval.Millisecond); + // 按 15 分钟为单位划分时间段 + int intervalStartMinute = (minute / interval) * interval; - return previousInterval; + // 返回当前时间刻度下的 DateTime,保留小时和新计算的分钟,秒和毫秒设为0 + return new DateTime(lastUpdate.Year, lastUpdate.Month, lastUpdate.Day, lastUpdate.Hour, intervalStartMinute, 0, 0); } } }