Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
3.1 kB · 117 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118import { Platform } from "react-native";
let timeOffset = 0;let hasWarned = false;let OriginalDate: DateConstructor = Date;let isInitialized = false;
const CLOCK_DRIFT_THRESHOLD_MS = 60 * 60 * 1000; // 1 hour
export function getTimeOffset(): number { return timeOffset;}
export function setTimeOffset(offset: number): void { timeOffset = offset;}
export function checkClockDrift(serverTime: string): { hasDrift: boolean; driftMs: number; driftSeconds: number;} { const serverDate = new OriginalDate(serverTime); const clientDate = new OriginalDate(); const drift = Math.abs(serverDate.getTime() - clientDate.getTime());
if (drift > CLOCK_DRIFT_THRESHOLD_MS) { const driftSeconds = Math.round(drift / 1000); if (!hasWarned) { hasWarned = true; console.warn( `clock drift detected: ${driftSeconds}s difference from server time. ` + `this may cause issues with time-sensitive operations. ` + `please sync your system clock.`, ); } return { hasDrift: true, driftMs: drift, driftSeconds }; } else { return { hasDrift: false, driftMs: drift, driftSeconds: Math.round(drift / 1000), }; }}
export function syncTimeWithServer( serverTime: string, networkLatencyMs: number,): void { const serverDate = new OriginalDate(serverTime); const clientDate = new OriginalDate(); const offset = serverDate.getTime() - clientDate.getTime() - networkLatencyMs;
setTimeOffset(offset);}
export function getSyncedDate(): Date { if (timeOffset !== 0) { return new Date(OriginalDate.now() + timeOffset); } return new OriginalDate();}
export function getSystemDate(): Date { return new OriginalDate();}
export function getSystemTime(): number { return OriginalDate.now();}
export function initializeTimeSync(): void { if (isInitialized) { return; } isInitialized = true;
if (Platform.OS !== "web") { return; }
// store original Date OriginalDate = Date; const OriginalDatePrototype = OriginalDate.prototype;
// create patched Date constructor function PatchedDate(this: any, ...args: any[]): any { // If called as a function (no `new`), forward to original Date to get the string form if (!(this instanceof PatchedDate)) { return OriginalDate.apply(undefined, args as any); }
// If called as a constructor, construct a Date with synced time when no args provided if (args.length === 0) { const syncedTime = OriginalDate.now() + timeOffset; return Reflect.construct(OriginalDate, [syncedTime], PatchedDate); }
// Otherwise construct with the provided arguments return Reflect.construct(OriginalDate, args, PatchedDate); }
// copy static methods PatchedDate.now = function (): number { return OriginalDate.now() + timeOffset; };
PatchedDate.parse = OriginalDate.parse; PatchedDate.UTC = OriginalDate.UTC;
// copy prototype PatchedDate.prototype = OriginalDatePrototype;
// replace global Date (globalThis as any).Date = PatchedDate;}