Skip to content

armgit / plexo

Public Imported from GitHub
0

Speed up downloads by combining multiple network connections in parallel

Start and stop schedules at the DST jump when their time is skipped (#148)

Arpit Khandelwal7a06ad1632e9
diff --git a/e2e/downloadSchedule.spec.ts b/e2e/downloadSchedule.spec.ts
index 6f2ba3e..20ec6ed 100644
--- a/e2e/downloadSchedule.spec.ts
+++ b/e2e/downloadSchedule.spec.ts
@@ -1,6 +1,7 @@
import { expect, test } from '@playwright/test'
import {
DEFAULT_DOWNLOAD_SCHEDULE,
+ scheduleMessage,
scheduleWindow,
validSchedule
} from '../src/shared/downloadSchedule'
@@ -74,3 +75,30 @@ test('local wall-clock windows survive daylight saving changes', () => {
else process.env.TZ = previous
}
})
+
+test('a window starting or ending in the hour clocks skip uses the moment they jump', () => {
+ const previous = process.env.TZ
+ process.env.TZ = 'America/New_York'
+ try {
+ // 2026-03-08: 2:00 EST becomes 3:00 EDT, so 2:30 never happens that day.
+ const march8 = (hour: number, minute = 0): number =>
+ new Date(2026, 2, 8, hour, minute).getTime()
+ const early = { ...schedule, startMinute: 2 * 60 + 30, endMinute: 7 * 60 }
+ expect(scheduleWindow(early, march8(1, 59)).nextStart).toBe(march8(3))
+ const threeAm = new Date(march8(3)).toLocaleTimeString([], {
+ hour: 'numeric',
+ minute: '2-digit'
+ })
+ expect(scheduleMessage(early, march8(1, 59))).toBe(`Waiting until ${threeAm} today`)
+ expect(scheduleWindow(early, march8(3)).allowed).toBe(true)
+ expect(scheduleWindow(early, march8(3, 15)).allowed).toBe(true)
+
+ const overnight = { ...schedule, startMinute: 22 * 60, endMinute: 2 * 60 + 30 }
+ const lateSaturday = new Date(2026, 2, 7, 23).getTime()
+ expect(scheduleWindow(overnight, lateSaturday).endsAt).toBe(march8(3))
+ expect(scheduleWindow(overnight, march8(3, 15)).allowed).toBe(false)
+ } finally {
+ if (previous === undefined) delete process.env.TZ
+ else process.env.TZ = previous
+ }
+})
diff --git a/src/shared/downloadSchedule.ts b/src/shared/downloadSchedule.ts
index fad6dff..a2b1c35 100644
--- a/src/shared/downloadSchedule.ts
+++ b/src/shared/downloadSchedule.ts
@@ -41,6 +41,26 @@ export interface ScheduleWindow {
expired?: boolean
}
+const MINUTE_MS = 60_000
+
+/** `date` at `minute` past local midnight. On the day clocks spring forward, a time in the skipped
+ * hour doesn't exist, and Date moves it on by the size of the gap (2:30 becomes 3:30); the clock
+ * passes 2:30 the moment it jumps to 3:00, so that's the time used instead. */
+function atLocalMinute(date: Date, minute: number): Date {
+ const at = new Date(date)
+ at.setHours(Math.floor(minute / 60), minute % 60, 0, 0)
+ const wallMinute = (time: Date): number => time.getHours() * 60 + time.getMinutes()
+ if (wallMinute(at) === minute) return at
+ // Back up to the first minute after the jump: the one before it reads earlier than `minute`, or
+ // is the day before (where clocks jump at midnight).
+ for (;;) {
+ const before = new Date(at.getTime() - MINUTE_MS)
+ if (before.getDate() !== date.getDate() || wallMinute(before) <= minute) break
+ at.setTime(before.getTime())
+ }
+ return at
+}
+
/** Calendar arithmetic, rather than 24-hour offsets, keeps local times across DST changes.
* Overnight windows belong to the day they start; the end date includes that whole window. */
export function scheduleWindow(
@@ -55,10 +75,10 @@ export function scheduleWindow(
const start = new Date(today.getFullYear(), today.getMonth(), today.getDate() + offset)
if (!schedule.days.includes(start.getDay())) continue
if (schedule.endDate && localDate(start) > schedule.endDate) continue
- start.setHours(Math.floor(schedule.startMinute / 60), schedule.startMinute % 60, 0, 0)
- const end = new Date(start)
- if (schedule.endMinute < schedule.startMinute) end.setDate(end.getDate() + 1)
- end.setHours(Math.floor(schedule.endMinute / 60), schedule.endMinute % 60, 0, 0)
+ const endDay = new Date(start)
+ if (schedule.endMinute < schedule.startMinute) endDay.setDate(endDay.getDate() + 1)
+ const end = atLocalMinute(endDay, schedule.endMinute)
+ start.setTime(atLocalMinute(start, schedule.startMinute).getTime())
if (now >= start.getTime() && now < end.getTime()) {
return { allowed: true, endsAt: end.getTime() }
}
@@ -87,7 +107,8 @@ export function scheduleMessage(
localDate(next) === localDate(new Date(now))
? 'today'
: next.toLocaleDateString([], { weekday: 'short' })
- return `Waiting until ${scheduleTime(schedule.startMinute)} ${day}`
+ const time = next.toLocaleTimeString([], { hour: 'numeric', minute: '2-digit' })
+ return `Waiting until ${time} ${day}`
}
return window.expired ? 'Schedule ended · downloads waiting' : 'Schedule needs attention'
}