@@ -151,4 +151,57 @@ describe('getSecondsUntilMidnightInTimezone', () => {
151151
152152 expect ( getSecondsUntilMidnightInTimezone ( 'Etc/GMT-14' ) ) . toBe ( 10 * 3600 ) ;
153153 } ) ;
154+
155+ it ( 'should calculate seconds until UTC midnight correctly at a calendar boundary' , ( ) => {
156+ // Arrange: Set time to Dec 31, 2023, 23:59:50 UTC (10 seconds before New Year)
157+ const boundaryTime = new Date ( Date . UTC ( 2023 , 11 , 31 , 23 , 59 , 50 ) ) ;
158+ vi . setSystemTime ( boundaryTime ) ;
159+
160+ // Act
161+ const seconds = getSecondsUntilUTCMidnight ( ) ;
162+
163+ // Assert: Exactly 10 seconds should remain
164+ expect ( seconds ) . toBe ( 10 ) ;
165+ } ) ;
166+
167+ it ( 'should handle extreme positive timezone offset boundary (+14:00)' , ( ) => {
168+ // Arrange: Pacific/Kiritimati is UTC+14.
169+ // When UTC is Dec 31, 09:59:50, Kiritimati is Dec 31, 23:59:50 (10 seconds to midnight)
170+ const boundaryTime = new Date ( Date . UTC ( 2023 , 11 , 31 , 9 , 59 , 50 ) ) ;
171+ vi . setSystemTime ( boundaryTime ) ;
172+
173+ // Act
174+ const seconds = getSecondsUntilMidnightInTimezone ( 'Pacific/Kiritimati' ) ;
175+
176+ // Assert
177+ expect ( seconds ) . toBe ( 10 ) ;
178+ } ) ;
179+
180+ it ( 'should handle extreme negative timezone offset boundary (-11:00)' , ( ) => {
181+ // Arrange: Pacific/Midway is UTC-11.
182+ // When UTC is Jan 1, 10:59:50, Midway is Dec 31, 23:59:50 (10 seconds to midnight)
183+ const boundaryTime = new Date ( Date . UTC ( 2024 , 0 , 1 , 10 , 59 , 50 ) ) ;
184+ vi . setSystemTime ( boundaryTime ) ;
185+
186+ // Act
187+ const seconds = getSecondsUntilMidnightInTimezone ( 'Pacific/Midway' ) ;
188+
189+ // Assert
190+ expect ( seconds ) . toBe ( 10 ) ;
191+ } ) ;
192+
193+ it ( 'should handle exact midnight boundary gracefully (0 seconds remaining)' , ( ) => {
194+ // Arrange: Exactly midnight in UTC
195+ const midnightTime = new Date ( Date . UTC ( 2024 , 0 , 1 , 0 , 0 , 0 ) ) ;
196+ vi . setSystemTime ( midnightTime ) ;
197+
198+ // Act
199+ const secondsUTC = getSecondsUntilUTCMidnight ( ) ;
200+ const secondsLondon = getSecondsUntilMidnightInTimezone ( 'Europe/London' ) ;
201+
202+ // Assert: Both should recognize it's 24 hours (86400 seconds) until the NEXT midnight
203+ // Since the logic does `86400 - (hour * 3600 + ...)`, at 00:00:00 it returns 86400.
204+ expect ( secondsUTC ) . toBe ( 86400 ) ;
205+ expect ( secondsLondon ) . toBe ( 86400 ) ;
206+ } ) ;
154207} ) ;
0 commit comments