Goal: ensure daily aggregates anchored to a business timezone (e.g., America/Los_Angeles) match aggregates computed by converting UTC event_time -> business timezone, including across DST changeovers.Validation strategy (steps):1. Canonical approach: compute business_date by converting UTC event timestamp to the business timezone and truncating to date; aggregate on that. This is the ground truth.2. Alternative approach: use UTC-based windowing logic (e.g., fixed offsets) or precomputed business_date column — validate it matches canonical approach for all rows, especially around DST transitions.3. Automated SQL checks + targeted test cases around DST boundaries, leap seconds, and ambiguous/skipped local times.SQL checks (example for Postgres / Redshift with timezone support):sql
-- 1) Recompute canonical business_date
WITH canonical AS (
SELECT id, event_time_utc,
(event_time_utc AT TIME ZONE 'UTC' AT TIME ZONE 'America/Los_Angeles')::date AS canonical_business_date
FROM events
),
stated AS (
SELECT id, business_date AS stored_business_date
FROM events
)
SELECT s.id, e.event_time_utc, canonical_business_date, stored_business_date
FROM canonical e
JOIN stated s USING (id)
WHERE canonical_business_date <> stored_business_date
LIMIT 100;
sql
-- 2) Aggregate comparison for a date range (including DST days)
WITH agg_canon AS (
SELECT canonical_business_date AS day, COUNT(*) AS cnt
FROM (
SELECT (event_time_utc AT TIME ZONE 'UTC' AT TIME ZONE 'America/Los_Angeles')::date AS canonical_business_date
FROM events
WHERE event_time_utc BETWEEN '2023-03-10' AND '2023-03-15'
) t
GROUP BY 1
),
agg_stored AS (
SELECT business_date AS day, COUNT(*) AS cnt FROM events
WHERE event_time_utc BETWEEN '2023-03-10' AND '2023-03-15'
GROUP BY 1
)
SELECT coalesce(a.day,b.day) AS day, a.cnt AS canon_cnt, b.cnt AS stored_cnt
FROM agg_canon a FULL OUTER JOIN agg_stored b USING(day)
WHERE coalesce(a.cnt,0) <> coalesce(b.cnt,0);
Test cases to include:- DST spring forward day (e.g., second Sunday in March for US): generate events at UTC times that map to the missing local hour (e.g., 02:00 local skipped) — verify mapping and counts.- DST fall back day: generate events in the repeated local hour; ensure conversions include correct offset (use timezone-aware functions) and counts reflect both occurrences.- Boundary seconds: events at 00:00:00 UTC and 23:59:59 UTC around business_date edges.- Historical TZ rule changes: test with older dates if business needs historical accuracy.- Nulls/invalid timestamps: ensure they are flagged.Additional checks:- Row-level parity: every event has canonical_business_date = stored_business_date.- Aggregate parity across sliding windows (7/30-day) to catch subtle off-by-one shifts.- Monitoring: daily job that runs SQL checks for past N days and alerts on mismatches.Why this works:- Using DB timezone-aware conversion handles DST rules reliably.- Comparing recomputed canonical values to stored/precomputed values surfaces bugs from naive fixed-offset logic.- Targeted DST test cases ensure edge behaviors (skipped/repeated hours) are validated.