timing
This commit is contained in:
@@ -193,3 +193,43 @@ export function syncLagMs(bill: { billedat?: string; receivedat?: string }): num
|
||||
const lag = received - billed;
|
||||
return lag >= 60_000 ? lag : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* A lifecycle stamp that cannot have happened before the order did.
|
||||
*
|
||||
* The same class of fault as `billedAtMs`, in a second place. `getorders`
|
||||
* returns `deliverydate` — which is `orders.deliverytime` under an alias — and
|
||||
* on live tenant 1147 it is 5h29m48s behind `orderdate` on ALL FIFTY rows read:
|
||||
*
|
||||
* orderdate 2026-09-05T14:48:42+05:30
|
||||
* deliverydate 2026-09-05T09:18:54+05:30
|
||||
*
|
||||
* A delivery cannot be stamped before the order it belongs to. The gap is one
|
||||
* IST offset less a few seconds of processing, which is what a UTC instant
|
||||
* looks like when it is written into a column whose other writer uses server
|
||||
* local time and then serialised with a +05:30 tag. Fiesta's own default for
|
||||
* `orderdate` is `time.Now()` in the server's zone, so `orderdate` is the sound
|
||||
* reference and the one to measure against.
|
||||
*
|
||||
* Corrected only when the correction actually resolves the impossibility — a
|
||||
* stamp still earlier than the order afterwards is something else, and guessing
|
||||
* further would be inventing data. So this stops correcting by itself the day
|
||||
* the caller sends a real instant.
|
||||
*/
|
||||
export function stampAfterMs(value: string | undefined, notBefore: string | undefined): number | null {
|
||||
if (!value) return null;
|
||||
const at = new Date(value).getTime();
|
||||
if (Number.isNaN(at)) return null;
|
||||
|
||||
if (!notBefore) return at;
|
||||
const floor = new Date(notBefore).getTime();
|
||||
if (Number.isNaN(floor)) return at;
|
||||
if (at >= floor - CLOCK_TOLERANCE_MS) return at;
|
||||
|
||||
// `getTimezoneOffset` is (UTC − local) in minutes: −330 for IST. SUBTRACTING
|
||||
// it moves a UTC instant forward to the wall-clock reading it stood for,
|
||||
// which is the direction this fault runs — the opposite of `billedAtMs`,
|
||||
// where local digits were labelled `Z`.
|
||||
const corrected = at - new Date(at).getTimezoneOffset() * 60_000;
|
||||
return corrected >= floor - CLOCK_TOLERANCE_MS ? corrected : at;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user