Skip to content

Commit

Permalink
fix: format date using Date.toLocaleString (#2459)
Browse files Browse the repository at this point in the history
* fix: format date using Date.toLocaleString

Fixes #2458

* fix: date typings
  • Loading branch information
zzmp authored Nov 17, 2021
1 parent 34a5885 commit b2697f0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 32 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"@types/lingui__core": "^2.7.1",
"@types/lingui__macro": "^2.7.4",
"@types/lingui__react": "^2.8.3",
"@types/luxon": "^1.24.4",
"@types/ms.macro": "^2.0.0",
"@types/multicodec": "^1.0.0",
"@types/node": "^13.13.5",
Expand Down Expand Up @@ -94,7 +93,6 @@
"graphql-request": "^3.4.0",
"inter-ui": "^3.13.1",
"jest-styled-components": "^7.0.5",
"luxon": "^1.25.0",
"microbundle": "^0.13.3",
"ms.macro": "^2.0.0",
"multicodec": "^3.0.1",
Expand Down
51 changes: 31 additions & 20 deletions src/pages/Vote/VotePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { BigNumber } from '@ethersproject/bignumber'
// eslint-disable-next-line no-restricted-imports
import { t, Trans } from '@lingui/macro'
import { CurrencyAmount, Token } from '@uniswap/sdk-core'
import { useActiveLocale } from 'hooks/useActiveLocale'
import useCurrentBlockTimestamp from 'hooks/useCurrentBlockTimestamp'
import JSBI from 'jsbi'
import { DateTime } from 'luxon/src/luxon'
import { useState } from 'react'
import { ArrowLeft } from 'react-feather'
import ReactMarkdown from 'react-markdown'
Expand Down Expand Up @@ -127,14 +127,17 @@ function getDateFromBlock(
currentBlock: number | undefined,
averageBlockTimeInSeconds: number | undefined,
currentTimestamp: BigNumber | undefined
): DateTime | undefined {
return targetBlock && currentBlock && averageBlockTimeInSeconds && currentTimestamp
? DateTime.fromSeconds(
currentTimestamp
.add(BigNumber.from(averageBlockTimeInSeconds).mul(BigNumber.from(targetBlock - currentBlock)))
.toNumber()
)
: undefined
): Date | undefined {
if (targetBlock && currentBlock && averageBlockTimeInSeconds && currentTimestamp) {
const date = new Date()
date.setTime(
currentTimestamp
.add(BigNumber.from(averageBlockTimeInSeconds).mul(BigNumber.from(targetBlock - currentBlock)))
.toNumber() * 1000
)
return date
}
return undefined
}

export default function VotePage({
Expand All @@ -161,19 +164,28 @@ export default function VotePage({
// get and format date from data
const currentTimestamp = useCurrentBlockTimestamp()
const currentBlock = useBlockNumber()
const startDate: DateTime | undefined = getDateFromBlock(
const startDate = getDateFromBlock(
proposalData?.startBlock,
currentBlock,
(chainId && AVERAGE_BLOCK_TIME_IN_SECS[chainId]) ?? DEFAULT_AVERAGE_BLOCK_TIME_IN_SECS,
currentTimestamp
)
const endDate: DateTime | undefined = getDateFromBlock(
const endDate = getDateFromBlock(
proposalData?.endBlock,
currentBlock,
(chainId && AVERAGE_BLOCK_TIME_IN_SECS[chainId]) ?? DEFAULT_AVERAGE_BLOCK_TIME_IN_SECS,
currentTimestamp
)
const now: DateTime = DateTime.local()
const now = new Date()
const locale = useActiveLocale()
const dateFormat: Intl.DateTimeFormatOptions = {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
timeZoneName: 'short',
}

// get total votes and format percentages for UI
const totalVotes: number | undefined = proposalData ? proposalData.forCount + proposalData.againstCount : undefined
Expand Down Expand Up @@ -241,19 +253,18 @@ export default function VotePage({
<RowBetween>
<TYPE.main>
{startDate && startDate > now ? (
<Trans>
Voting starts approximately {startDate && startDate.toLocaleString(DateTime.DATETIME_FULL)}
</Trans>
<Trans>Voting starts approximately {startDate.toLocaleString(locale, dateFormat)}</Trans>
) : null}
</TYPE.main>
</RowBetween>
<RowBetween>
<TYPE.main>
{endDate && endDate < now ? (
<Trans>Voting ended {endDate && endDate.toLocaleString(DateTime.DATETIME_FULL)}</Trans>
) : (
<Trans>Voting ends approximately {endDate && endDate.toLocaleString(DateTime.DATETIME_FULL)}</Trans>
)}
{endDate &&
(endDate < now ? (
<Trans>Voting ended {endDate.toLocaleString(locale, dateFormat)}</Trans>
) : (
<Trans>Voting ends approximately {endDate.toLocaleString(locale, dateFormat)}</Trans>
))}
</TYPE.main>
</RowBetween>
{proposalData && proposalData.status === ProposalState.ACTIVE && !showVotingButtons && (
Expand Down
10 changes: 0 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4123,11 +4123,6 @@
resolved "https://registry.yarnpkg.com/@types/long/-/long-4.0.1.tgz#459c65fa1867dafe6a8f322c4c51695663cc55e9"
integrity sha512-5tXH6Bx/kNGd3MgffdmP4dy2Z+G4eaXw0SE81Tq3BNadtnMR5/ySMzX4SLEzHJzSmPNn4HIdpQsBvXMUykr58w==

"@types/luxon@^1.24.4":
version "1.27.1"
resolved "https://registry.yarnpkg.com/@types/luxon/-/luxon-1.27.1.tgz#aceeb2d5be8fccf541237e184e37ecff5faa9096"
integrity sha512-cPiXpOvPFDr2edMnOXlz3UBDApwUfR+cpizvxCy0n3vp9bz/qe8BWzHPIEFcy+ogUOyjKuCISgyq77ELZPmkkg==

"@types/minimatch@*":
version "3.0.5"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.5.tgz#1001cc5e6a3704b83c236027e77f2f58ea010f40"
Expand Down Expand Up @@ -14123,11 +14118,6 @@ ltgt@~2.2.0:
resolved "https://registry.npmjs.org/ltgt/-/ltgt-2.2.1.tgz"
integrity sha1-81ypHEk/e3PaDgdJUwTxezH4fuU=

luxon@^1.25.0:
version "1.27.0"
resolved "https://registry.npmjs.org/luxon/-/luxon-1.27.0.tgz"
integrity sha512-VKsFsPggTA0DvnxtJdiExAucKdAnwbCCNlMM5ENvHlxubqWd0xhZcdb4XgZ7QFNhaRhilXCFxHuoObP5BNA4PA==

lz-string@^1.4.4:
version "1.4.4"
resolved "https://registry.yarnpkg.com/lz-string/-/lz-string-1.4.4.tgz#c0d8eaf36059f705796e1e344811cf4c498d3a26"
Expand Down

0 comments on commit b2697f0

Please sign in to comment.