namespace AttendanceHub.Sync.Domain; /// /// Punch-time clock convention (single source of truth for window maths). /// /// ZKTeco devices report attendance as a LOCAL WALL CLOCK (y/m/d/h/m/s) with no offset. /// The canonical identity persists those exact components with a ZERO offset — a punch taken at /// 12:00:16 device-local is stored as 2026-10-03T12:00:16Z. Host UTC is therefore NOT /// comparable with punch_time (site = UTC+03:30, so device-local labels run 3.5h "ahead" of UTC). /// /// Rules: /// /// — "now" expressed in the device-label convention /// (host local wall clock, zero offset). Use it for any comparison against punch_time. /// — device-label value -> a real instant. /// Use it whenever an SDK window API needs a real time (ReadTimeGLogData receives /// ToLocalTime() strings, i.e. it wants instants, not labels). /// — real instant -> device-label value. /// /// Assumption: the device clock shows the same wall clock as this host (both are site time). /// MEASURED (2026-10-11, host time at raw COM callback minus the device's own y/m/d/h/m/s): /// 172.20.30.220 = 95-96s behind host 172.20.30.221 = 106-108s behind host /// 172.26.30.17 = 107s behind host phase1-test-01 = 113s behind host /// The skew is per-device (18s spread) and NOT the "~84s" this comment used to claim. All /// consumers below treat the device label as host wall clock, so every window comparison carries /// that per-device error. It currently lands in the SAFE direction (a device that is behind makes /// its punches look older, so the watermark window is wider than needed and nothing is skipped), /// and DeviceWorker's 10-minute OverlapMinutes dwarfs the 18s spread. It is still a correctness /// gap for anything that compares punch_time ACROSS devices or against a threshold to the second. /// Do not "correct" it with one global constant - measure per device, or sync the device clocks. /// public static class DeviceClock { /// Current time expressed in the device-label convention (zero offset). public static DateTimeOffset Now() { var wall = DateTime.Now; // host local wall clock == device local wall clock (same site time) return new DateTimeOffset(wall.Year, wall.Month, wall.Day, wall.Hour, wall.Minute, wall.Second, TimeSpan.Zero); } /// Device-label value -> real instant (so SDK local-time formatting resolves correctly). public static DateTimeOffset ToInstant(DateTimeOffset deviceLocalLabelled) { var wall = DateTime.SpecifyKind(deviceLocalLabelled.UtcDateTime, DateTimeKind.Local); return new DateTimeOffset(wall); } /// Real instant -> device-label value (zero offset). public static DateTimeOffset ToDeviceLabelled(DateTimeOffset instant) { var local = instant.LocalDateTime; return new DateTimeOffset(local.Year, local.Month, local.Day, local.Hour, local.Minute, local.Second, TimeSpan.Zero); } /// Compose a device-label timestamp from raw device components (never throws for sane ranges). public static bool TryComposeDeviceLabelled(int year, int month, int day, int hour, int minute, int second, out DateTimeOffset labelled) { labelled = default; if (year < 1970 || year > 2200 || month is < 1 or > 12 || day is < 1 or > 31 || hour is < 0 or > 23 || minute is < 0 or > 59 || second is < 0 or > 60) return false; try { labelled = new DateTimeOffset(year, month, day, hour, minute, Math.Min(second, 59), TimeSpan.Zero); return true; } catch (ArgumentOutOfRangeException) { return false; } } }