Timer Behavior Specification - LastMoment / Timer420
1. Current Prototype Behavior Analysis
In the initial prototype (LastMoment.c), timer behavior is managed through wall-clock epoch timestamps:
- Time Source:
time(NULL)(standard C library wall-clock time, 1-second resolution). - Target Deadline:
g.deadlineEpoch = time(NULL) + g.remainingSeconds. - Tick Interval:
WM_TIMERfires every 250ms to refresh the display, calculatingremaining = deadlineEpoch - time(NULL). - Pause Behavior: Freezes remaining time by computing
remainingSeconds = deadlineEpoch - time(NULL). - Resume Behavior: Recomputes
deadlineEpoch = time(NULL) + remainingSeconds. - Completion: When remaining time reaches 0,
g.runningis set toFALSE, display color turns red, and text shows “TIME’S UP”.
2. Identified Deficiencies in Current Behavior
- Wall-Clock Vulnerability:
time(NULL)is affected by manual clock adjustments, NTP time synchronizations, and Daylight Saving Time transitions. - Pause/Resume Truncation Drift:
time(NULL)operates with 1-second granularity. Pausing and resuming repeatedly causes cumulative time loss due to integer division truncation. - Global Escape Hotkey Bug:
RegisterHotKey(hwnd, HOTKEY_ESC, 0, VK_ESCAPE)traps the Escape key globally across all open desktop windows. - No Cycle Repetition: The prototype cannot automatically restart a new cycle or track completed cycles.
- No System Sleep/Resume Handling: If the computer enters sleep mode for 30 minutes during a 2-hour timer,
time(NULL)jumps upon wake-up, but no event is raised for missed timer expiration while sleeping. - No Sub-Second High Resolution: Display update occurs 4 times per second, but calculations are locked to whole seconds.
3. Target Architecture Timer Specification
3.1 Time Source & High-Resolution Precision
The new Timer Core uses a Monotonic Clock (QueryPerformanceCounter on Windows, wrapped behind a generic Clock abstraction).
- Monotonic time guarantees strictly increasing ticks unaffected by system wall-clock updates.
- Internal precision is tracked in milliseconds (or nanoseconds), avoiding rounding drift.
3.2 Timer State Machine
stateDiagram-v2
[*] --> IDLE
IDLE --> RUNNING : Start / Resume
RUNNING --> PAUSED : Pause
PAUSED --> RUNNING : Resume
RUNNING --> COMPLETED : Expiration (Remaining == 0)
COMPLETED --> RUNNING : Repeat Cycle Enabled
COMPLETED --> IDLE : Reset / Stop
PAUSED --> IDLE : Reset / Stop
RUNNING --> IDLE : Stop
- IDLE: Initial or reset state. Duration configured, remaining time equals total duration.
- RUNNING: Countdown active. Ticks evaluate elapsed monotonic time.
- PAUSED: Countdown suspended. Monotonic target offset is frozen.
- COMPLETED: Time reached 0.
TimerCompletedevent emitted. If auto-repeat is active, transitions toRUNNINGfor next cycle.
3.3 Repeating Timer Mechanism
- Supports single-shot or multi-cycle repeating mode (finite count or infinite
∞). - Tracks:
configured_duration_mselapsed_duration_msremaining_duration_mscurrent_cyclecompleted_cyclesmax_cycles(0 = infinite)
3.4 System Sleep / Resume Behavior
- The platform layer listens to
WM_POWERBROADCAST(PBT_APMRESUMESUSPEND/PBT_APMRESUMEAUTOMATIC). - Upon system resume:
- The Monotonic Clock evaluates actual real-world time elapsed during sleep.
- If target duration expired while computer was sleeping, the timer immediately triggers
COMPLETEDevent and dispatches catch-up notification upon resume. - If duration has not expired, remaining time is adjusted accurately without drift.