58 lines
2.5 KiB
Python
58 lines
2.5 KiB
Python
import datetime
|
|
|
|
def solve():
|
|
# Scenario: User in Stockholm (UTC+2)
|
|
# Local Wakeup Reference: 04:00
|
|
# Actual Wakeup Time (UTC): 03:17
|
|
|
|
offset_minutes = -120 # Stockholm UTC+2 -> getTimezoneOffset() is -120
|
|
|
|
wakeup_ref_local = "04:00"
|
|
wakeup_ref_mins_local = 4 * 60
|
|
|
|
# Let's assume the backend calculates: wakeup_minutes = (UTC_wakeup - UTC_ref)
|
|
# where it treats the provided ref as if it were in UTC.
|
|
|
|
# Case 1: BUGGY (Frontend sends Local ref "04:00" to backend)
|
|
# Backend treats 04:00 as UTC.
|
|
utc_wakeup = 3 * 60 + 17 # 03:17 UTC
|
|
wakeup_minutes_from_backend = utc_wakeup - wakeup_ref_mins_local # 197 - 240 = -43
|
|
print(f"BUGGY: Backend returns wakeup_minutes (assuming ref 04:00 is UTC): {wakeup_minutes_from_backend}")
|
|
|
|
# Frontend (current) tries to fix it with adjustForTimezone:
|
|
received_wakeup_minutes = wakeup_minutes_from_backend
|
|
# offsetFromRef = getLocalOffsetFromReference(received, "04:00")
|
|
# localMins = adjustForTimezone(received) = received - offset_minutes
|
|
# offsetFromRef = localMins - ref_mins_local
|
|
|
|
def adjustForTimezone(utc_min):
|
|
return utc_min - offset_minutes
|
|
|
|
def getLocalOffsetFromReference(utc_min, ref_mins):
|
|
local_m = adjustForTimezone(utc_min)
|
|
return local_m - ref_mins
|
|
|
|
offset_from_ref = getLocalOffsetFromReference(received_wakeup_minutes, wakeup_ref_mins_local)
|
|
print(f"BUGGY: Frontend calculates offsetFromRef: {offset_from_ref}")
|
|
# formatMinutesAfter(offset, ref) -> ref + offset
|
|
print(f"BUGGY: Displayed time (ref + offset): {wakeup_ref_mins_local + offset_from_ref}")
|
|
|
|
# Case 2: CORRECT (Frontend sends UTC ref "02:00" to backend)
|
|
# Stockholm +2 means 04:00 Local is 02:00 UTC.
|
|
utc_wakeup_ref = wakeup_ref_mins_local + offset_minutes # 240 - 120 = 120
|
|
wakeup_minutes_from_backend_correct = utc_wakeup - utc_wakeup_ref # 197 - 120 = 77
|
|
print(f"\nCORRECT: Backend returns wakeup_minutes (assuming ref 02:00 is UTC): {wakeup_minutes_from_backend_correct}")
|
|
|
|
# Frontend receives 77.
|
|
# If it's a delta, the component should use it directly as offsetFromRef.
|
|
# Or if we want to keep existing logic, we have to realize 77 is ALREADY the delta.
|
|
# But the component thinks it's "minutes from midnight".
|
|
# This is a mismatch.
|
|
|
|
print("---")
|
|
print(f"Target Local Wakeup: 05:17 ({3 * 60 + 17 + abs(offset_minutes)})")
|
|
print(f"Target Local Ref: 04:00 ({wakeup_ref_mins_local})")
|
|
print(f"Target Local Delta: {317 - 240} = 77")
|
|
|
|
solve()
|