|
| 1 | +import { TZDate } from "@date-fns/tz"; |
| 2 | +import { |
| 3 | + startOfDay, |
| 4 | + addDays, |
| 5 | + addMinutes, |
| 6 | + differenceInMilliseconds, |
| 7 | +} from "date-fns"; |
| 8 | +import type { HomeAssistant } from "../../types"; |
| 9 | +import { TimeZone } from "../../data/translation"; |
| 10 | +import { parseTimeString } from "../datetime/check_time"; |
| 11 | + |
| 12 | +/** |
| 13 | + * Calculate milliseconds until next time boundary for a time condition |
| 14 | + * @param hass Home Assistant object |
| 15 | + * @param after Optional time string (HH:MM) for after condition |
| 16 | + * @param before Optional time string (HH:MM) for before condition |
| 17 | + * @param weekdays Optional array of weekdays to match |
| 18 | + * @returns Milliseconds until next boundary, or undefined if no boundaries |
| 19 | + */ |
| 20 | +export function calculateNextTimeUpdate( |
| 21 | + hass: HomeAssistant, |
| 22 | + after?: string, |
| 23 | + before?: string, |
| 24 | + weekdays?: string[] |
| 25 | +): number | undefined { |
| 26 | + const timezone = |
| 27 | + hass.locale.time_zone === TimeZone.server |
| 28 | + ? hass.config.time_zone |
| 29 | + : Intl.DateTimeFormat().resolvedOptions().timeZone; |
| 30 | + |
| 31 | + const now = new TZDate(new Date(), timezone); |
| 32 | + const updates: Date[] = []; |
| 33 | + |
| 34 | + // Calculate next occurrence of after time |
| 35 | + if (after) { |
| 36 | + let afterDate = parseTimeString(after, timezone); |
| 37 | + if (afterDate <= now) { |
| 38 | + // If time has passed today, schedule for tomorrow |
| 39 | + afterDate = addDays(afterDate, 1); |
| 40 | + } |
| 41 | + updates.push(afterDate); |
| 42 | + } |
| 43 | + |
| 44 | + // Calculate next occurrence of before time |
| 45 | + if (before) { |
| 46 | + let beforeDate = parseTimeString(before, timezone); |
| 47 | + if (beforeDate <= now) { |
| 48 | + // If time has passed today, schedule for tomorrow |
| 49 | + beforeDate = addDays(beforeDate, 1); |
| 50 | + } |
| 51 | + updates.push(beforeDate); |
| 52 | + } |
| 53 | + |
| 54 | + // If weekdays are specified, check for midnight (weekday transition) |
| 55 | + if (weekdays && weekdays.length > 0 && weekdays.length < 7) { |
| 56 | + // Calculate next midnight using startOfDay + addDays |
| 57 | + const tomorrow = addDays(now, 1); |
| 58 | + const midnight = startOfDay(tomorrow); |
| 59 | + updates.push(midnight); |
| 60 | + } |
| 61 | + |
| 62 | + if (updates.length === 0) { |
| 63 | + return undefined; |
| 64 | + } |
| 65 | + |
| 66 | + // Find the soonest update time |
| 67 | + const nextUpdate = updates.reduce((soonest, current) => |
| 68 | + current < soonest ? current : soonest |
| 69 | + ); |
| 70 | + |
| 71 | + // Add 1 minute buffer to ensure we're past the boundary |
| 72 | + const updateWithBuffer = addMinutes(nextUpdate, 1); |
| 73 | + |
| 74 | + // Calculate difference in milliseconds |
| 75 | + return differenceInMilliseconds(updateWithBuffer, now); |
| 76 | +} |
0 commit comments