耗电同比

parent 1be0dee2
......@@ -18,6 +18,7 @@ import {
getMonthStart,
getLastMonthStart,
getLastMonthEnd,
getLastMonthSameTimeEnd,
getHoursAgo,
getDaysAgoStart,
getMonthsAgoStart,
......@@ -397,15 +398,20 @@ export async function getRunAnalysis() {
const todayStart = getTodayStart();
const todayEnd = getTodayEnd();
const yesterdayStart = getYesterdayStart();
const yesterdayEnd = getYesterdayEnd();
// const yesterdayEnd = getYesterdayEnd();
const monthStart = getMonthStart();
const lastMonthStart = getLastMonthStart();
const lastMonthEnd = getLastMonthEnd();
// const lastMonthEnd = getLastMonthEnd();
// ---- energyManagement 对称口径专用(不动 dateUtils 中原有边界函数,别处仍共用) ----
// 昨日同刻:昨天 0 点 → 昨天当前同一时刻(秒级对称,与今日「0点→现在」等长)
const yesterdaySameTimeEnd = getHoursAgo(24);
// 上月同期:上月 1 号 → 对标日(短月/本月月底自动兜底到上月最后一天)
const lastMonthSameTimeEnd = getLastMonthSameTimeEnd();
const jrnh = await getElectricitySumInRange(todayStart, todayEnd);
const zrnh = await getElectricitySumInRange(yesterdayStart, yesterdayEnd);
const zrnh = await getElectricitySumInRange(yesterdayStart, yesterdaySameTimeEnd);
const bynh = await getElectricitySumInRange(monthStart, todayEnd);
const synh = await getElectricitySumInRange(lastMonthStart, lastMonthEnd);
const synh = await getElectricitySumInRange(lastMonthStart, lastMonthSameTimeEnd);
const zrtb = calcCompareRate(jrnh, zrnh);
const sytb = calcCompareRate(bynh, synh);
......
......@@ -71,6 +71,39 @@ export function getLastMonthEnd(): string {
return formatTime(now);
}
/**
* 获取上月同期结束时间(用于「本月至今 vs 上月同期」的对称对比)
* 对标规则:
* - 普通日(今天不是本月最后一天):对标上月同号日,截止到与当前一致的时分秒(秒级对称);
* - 短月截断:上月没有今天这个号数(如 3/31 → 2 月),取上月最后一天 23:59:59;
* - 本月月底:今天已是本月最后一天,直接对标上月最后一天 23:59:59(整月对比)。
* @param now 基准时刻,默认当前时间
*/
export function getLastMonthSameTimeEnd(now: Date = new Date()): string {
const y = now.getFullYear();
const m = now.getMonth(); // 0-11,本月
const day = now.getDate();
// 本月最后一天号数(判断“今天是否本月最后一天”)
const daysThisMonth = new Date(y, m + 1, 0).getDate();
// 上月最后一天 23:59:59(月底对标 / 短月兜底)
const lastMonthLastDay = new Date(y, m, 0);
lastMonthLastDay.setHours(23, 59, 59, 999);
const fallback = formatTime(lastMonthLastDay);
// 今天已是本月最后一天 → 对标上月整月
if (day === daysThisMonth) return fallback;
// 短月:上月没有今天这个号数(如 3/31 → 2 月)→ 对标上月整月
const daysLastMonth = lastMonthLastDay.getDate();
if (day > daysLastMonth) return fallback;
// 普通日:上月同号日,截止到与当前一致的时分秒
const sameDay = new Date(y, m - 1, day, now.getHours(), now.getMinutes(), now.getSeconds(), 0);
return formatTime(sameDay);
}
/** 获取 N 小时前的时间字符串 */
export function getHoursAgo(n: number): string {
const now = new Date();
......
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