From f3be3b1f3c2c6b15ffce320e3050f6bd92dacf9d Mon Sep 17 00:00:00 2001 From: Ian Philips Date: Wed, 25 May 2022 15:55:50 -0600 Subject: [PATCH] Maybe free market countdown will work now, beats me --- web/hooks/use-has-created-contract-today.ts | 35 ++++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/web/hooks/use-has-created-contract-today.ts b/web/hooks/use-has-created-contract-today.ts index f63f5ac1..8f05287c 100644 --- a/web/hooks/use-has-created-contract-today.ts +++ b/web/hooks/use-has-created-contract-today.ts @@ -1,17 +1,36 @@ import { listContracts } from 'web/lib/firebase/contracts' import { useEffect, useState } from 'react' import { User } from 'common/user' +import dayjs from 'dayjs' +import utc from 'dayjs/plugin/utc' +dayjs.extend(utc) let sessionCreatedContractToday = true -export function getUtcFreeMarketResetTime(yesterday: boolean) { - // Uses utc time like the server. - const utcFreeMarketResetTime = new Date() - utcFreeMarketResetTime.setUTCDate( - utcFreeMarketResetTime.getUTCDate() - (yesterday ? 1 : 0) - ) - const utcFreeMarketMS = utcFreeMarketResetTime.setUTCHours(16, 0, 0, 0) - return utcFreeMarketMS +export function getUtcFreeMarketResetTime(previous: boolean) { + const localTimeNow = new Date() + const utc4pmToday = dayjs() + .utc() + .set('hour', 16) + .set('minute', 0) + .set('second', 0) + .set('millisecond', 0) + + // if it's after 4pm UTC today + if (localTimeNow.getTime() > utc4pmToday.valueOf()) { + return previous + ? // Return it as it is + utc4pmToday.valueOf() + : // Or add 24 hours to get the next 4pm UTC time: + utc4pmToday.valueOf() + 24 * 60 * 60 * 1000 + } + + // 4pm UTC today is coming up + return previous + ? // Subtract 24 hours to get the previous 4pm UTC time: + utc4pmToday.valueOf() - 24 * 60 * 60 * 1000 + : // Return it as it is + utc4pmToday.valueOf() } export const useHasCreatedContractToday = (user: User | null | undefined) => {