nix-playgraound/notes/048-haskell-time-windows.md

60 lines
1.6 KiB
Markdown

# Haskell Time Windows
This note covers `45-haskell-time-windows/`, which uses `Data.Time` to parse UTC timestamps and classify maintenance windows.
---
## 1. Why This Exists Next to the QuickCheck Window Example
`26-haskell-quickcheck/` works with abstract intervals and normalization rules.
This example teaches a different concern:
- parsing real UTC timestamps,
- comparing them against concrete windows, and
- rendering the resulting status in terms people would actually read.
The shared domain is intentional, but the concept is different.
---
## 2. What the Status Function Returns
`statusAt` sorts the windows by start time, then returns one of three cases:
- `Active`, with the remaining time in the current window,
- `Waiting`, with the delay until the next window starts, or
- `NoMoreWindows`.
That is the useful pattern to remember. Time logic often becomes easier to explain once the raw comparison result is turned into a small sum type.
---
## 3. Why the Test Checks Exact Delays
The test does not stop at string formatting. It also checks exact `NominalDiffTime` values:
- `1800` seconds for an active window with 30 minutes left, and
- `2700` seconds for a future window starting in 45 minutes.
That matters because time code can render the right words while still comparing the wrong timestamps underneath.
---
## 4. Commands to Try
```bash
cd 45-haskell-time-windows
nix develop
cabal run
cabal run -- 2026-05-05T11:45:00Z
cabal test
nix build
./result/bin/mini-time-windows 2026-05-05T11:45:00Z
nix run . -- 2026-05-05T11:45:00Z
nix flake check
```