Coverage for clock_correction.py: 100.00%

35 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2026-11-30 14:44 +0000

1"""Re-time events after a clock correction (SB-1449). 

2 

3Pure: no database. When a manager undoes halftime or the second half, every 

4event logged since the undone moment carries a minute read off the wrong 

5clock. Match #10106 went to the second half 14' in, so a goal at ~41' was 

6stamped 71'. This works out what each of those events should have read on 

7the corrected clock. 

8 

9Each event is placed by its `created_at`, on the same clock the Android app 

10shows (whole minutes elapsed, the second half counting on from 

11`half_duration`, stoppage beyond each half as `extra_time`). An event synced 

12late from an offline phone carries its sync time, not its tap time, so it 

13can land a few minutes late — still far closer than the wrong clock's minute. 

14""" 

15 

16from collections.abc import Iterable 

17from datetime import datetime 

18 

19 

20def _parse(ts: str | datetime | None) -> datetime | None: 

21 if ts is None or isinstance(ts, datetime): 

22 return ts 

23 return datetime.fromisoformat(ts.replace("Z", "+00:00")) 

24 

25 

26def minute_at( 

27 at: datetime, 

28 kickoff_time: str | datetime, 

29 halftime_start: str | datetime | None, 

30 second_half_start: str | datetime | None, 

31 half_duration: int, 

32) -> tuple[int, int]: 

33 """The (match_minute, extra_time) the clock read at `at`. 

34 

35 Mirrors the Android LiveClock: at half time the clock reads 

36 `half_duration`; a minute past either half's length is stoppage. 

37 """ 

38 kickoff = _parse(kickoff_time) 

39 halftime = _parse(halftime_start) 

40 second_half = _parse(second_half_start) 

41 

42 if second_half is not None and at >= second_half: 

43 minute = int((at - second_half).total_seconds() // 60) + half_duration 

44 cap = 2 * half_duration 

45 elif halftime is not None and at >= halftime: 

46 return half_duration, 0 

47 else: 

48 minute = int((at - kickoff).total_seconds() // 60) 

49 cap = half_duration 

50 

51 minute = max(minute, 0) 

52 if minute > cap: 

53 return cap, minute - cap 

54 return minute, 0 

55 

56 

57def retime_events( 

58 events: Iterable[dict], 

59 since: str | datetime, 

60 kickoff_time: str | datetime, 

61 halftime_start: str | datetime | None, 

62 second_half_start: str | datetime | None, 

63 half_duration: int, 

64) -> list[tuple[int, int, int]]: 

65 """Corrected minutes for events logged since a clock correction's cut-off. 

66 

67 Args: 

68 events: match events (deleted ones already filtered out); only those 

69 with a `match_minute` are timed — status changes and chat aren't. 

70 since: the clock timestamp the correction cleared; events before it 

71 were stamped on a clock that was right at the time. 

72 kickoff_time, halftime_start, second_half_start, half_duration: the 

73 clock as it stands after the correction. 

74 

75 Returns: 

76 (event_id, match_minute, extra_time) for each event whose minute 

77 changes, oldest first. 

78 """ 

79 cutoff = _parse(since) 

80 changes: list[tuple[datetime, int, int, int]] = [] 

81 for event in events: 

82 if event.get("match_minute") is None or not event.get("created_at"): 

83 continue 

84 created = _parse(event["created_at"]) 

85 if created < cutoff: 

86 continue 

87 minute, extra = minute_at(created, kickoff_time, halftime_start, second_half_start, half_duration) 

88 if (event["match_minute"], event.get("extra_time") or 0) != (minute, extra): 

89 changes.append((created, event["id"], minute, extra)) 

90 changes.sort() 

91 return [(event_id, minute, extra) for _, event_id, minute, extra in changes]