Skip to content

Commit 8fc305e

Browse files
committed
Add tests for DST transitions
1 parent fd657fb commit 8fc305e

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

test/common/datetime/check_time.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,49 @@ describe("checkTimeInRange", () => {
161161
expect(checkTimeInRange(mockHass)).toBe(true);
162162
});
163163
});
164+
165+
describe("DST transitions", () => {
166+
it("should handle spring forward transition (losing an hour)", () => {
167+
// March 10, 2024 at 1:30 AM PST - before spring forward
168+
// At 2:00 AM, clocks jump to 3:00 AM PDT
169+
vi.setSystemTime(new Date(2024, 2, 10, 1, 30, 0));
170+
171+
// Should be within range that crosses the transition
172+
expect(checkTimeInRange(mockHass, "01:00", "04:00")).toBe(true);
173+
});
174+
175+
it("should handle spring forward transition after the jump", () => {
176+
// March 10, 2024 at 3:30 AM PDT - after spring forward
177+
vi.setSystemTime(new Date(2024, 2, 10, 3, 30, 0));
178+
179+
// Should still be within range
180+
expect(checkTimeInRange(mockHass, "01:00", "04:00")).toBe(true);
181+
});
182+
183+
it("should handle fall back transition (gaining an hour)", () => {
184+
// November 3, 2024 at 1:30 AM PDT - before fall back
185+
// At 2:00 AM PDT, clocks fall back to 1:00 AM PST
186+
vi.setSystemTime(new Date(2024, 10, 3, 1, 30, 0));
187+
188+
// Should be within range that crosses the transition
189+
expect(checkTimeInRange(mockHass, "01:00", "03:00")).toBe(true);
190+
});
191+
192+
it("should handle midnight crossing during DST transition", () => {
193+
// March 10, 2024 at 1:00 AM - during spring forward night
194+
vi.setSystemTime(new Date(2024, 2, 10, 1, 0, 0));
195+
196+
// Range that crosses midnight and DST transition
197+
expect(checkTimeInRange(mockHass, "22:00", "04:00")).toBe(true);
198+
});
199+
200+
it("should correctly compare times on DST transition day", () => {
201+
// November 3, 2024 at 10:00 AM - after fall back completed
202+
vi.setSystemTime(new Date(2024, 10, 3, 10, 0, 0));
203+
204+
// Normal business hours should work correctly
205+
expect(checkTimeInRange(mockHass, "08:00", "17:00")).toBe(true);
206+
expect(checkTimeInRange(mockHass, "12:00", "17:00")).toBe(false);
207+
});
208+
});
164209
});

0 commit comments

Comments
 (0)