-
Notifications
You must be signed in to change notification settings - Fork 529
/
DateTimeCorrected.tsx
37 lines (33 loc) · 1.36 KB
/
DateTimeCorrected.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { tsToLocaleDate, tsToLocaleDateTime } from "@/lib/utils";
import { txToast } from "./TxToaster";
const clockSkewTolerance = 5 * 60; //5 minutes
type Props = {
tsFetch: number;
tsObject: number;
serverTime: number;
className?: string;
isDateOnly?: boolean;
};
export default function DateTimeCorrected({ tsFetch, tsObject, serverTime, className, isDateOnly }: Props) {
const serverClockDrift = serverTime - tsFetch; //be positive if server is ahead
const howFarInThePast = serverTime - tsObject;
const localTime = tsFetch - howFarInThePast;
const clockDriftBtnHandler = () => {
txToast.warning(`This means that the server clock is ${Math.abs(serverClockDrift)} seconds ${serverClockDrift > 0 ? 'ahead' : 'behind'} your computer time. Make sure both your computer and the server have their clocks synchronized.`);
}
const displayTime = isDateOnly
? tsToLocaleDate(localTime, 'short')
: tsToLocaleDateTime(localTime, 'medium', 'short')
return <span
className={className}
title={tsToLocaleDateTime(localTime, 'long', 'long')}
>
{displayTime}
{Math.abs(serverClockDrift) > clockSkewTolerance && (
<button
className="ml-1 text-warning-inline"
onClick={clockDriftBtnHandler}
>(clock drift)</button>
)}
</span>
}