This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
461059f
commit bc90d90
Showing
3 changed files
with
38 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import moment from 'moment'; | ||
|
||
export const epochToGMTFormat = (time: number) => moment.unix(time).utc().format('YYYY-MM-DD HH:mm:ss [GMT]'); | ||
export const epochToLocalFormat = (time: number) => moment.unix(time).utc().local().format('YYYY-MM-DD HH:mm:ss Z'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { useEffect, useState } from 'react'; | ||
import useQuery from 'Api/hooks/useQuery'; | ||
|
||
const useServerTime = () => { | ||
const currentDate = Date.now() / 1000; | ||
const [serverTime, setServerTime] = useState(currentDate); | ||
const { data } = useQuery('time', { options: { refetchInterval: 30000, refetchOnWindowFocus: false } }); | ||
|
||
useEffect(() => { | ||
let timeInterval: ReturnType<typeof setInterval>; | ||
|
||
if (data) { | ||
setServerTime(data.time ?? currentDate); | ||
|
||
timeInterval = setInterval(() => { | ||
setServerTime(prev => prev + 1); | ||
}, 1000); | ||
} | ||
|
||
return () => clearInterval(timeInterval); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [data]); | ||
|
||
return serverTime; | ||
}; | ||
|
||
export default useServerTime; |