From 36fe3ea710ee718c9610c3e84cfde877b7c00188 Mon Sep 17 00:00:00 2001 From: Rithvik Vibhu Date: Wed, 7 Jun 2023 17:38:13 +0530 Subject: [PATCH] ui: switch to locale-aware date time formatting --- .../Transactions/Transaction/index.js | 26 ++++++----- app/pages/Auction/BidHistory.js | 9 ++-- app/utils/timeConverter.js | 44 ++++++------------- 3 files changed, 33 insertions(+), 46 deletions(-) diff --git a/app/components/Transactions/Transaction/index.js b/app/components/Transactions/Transaction/index.js index 5b3677afc..46232a84a 100644 --- a/app/components/Transactions/Transaction/index.js +++ b/app/components/Transactions/Transaction/index.js @@ -3,14 +3,14 @@ import { connect } from 'react-redux'; import PropTypes from 'prop-types'; import classnames from 'classnames'; import { withRouter } from 'react-router'; -import createAMPMTimeStamp from '../../../utils/timeConverter'; +import { dateTimeFormatters } from '../../../utils/timeConverter'; import '../index.scss'; import { displayBalance } from '../../../utils/balances'; import ellipsify from '../../../utils/ellipsify'; import { formatName } from '../../../utils/nameHelpers'; import Tooltipable from '../../Tooltipable'; import { shell } from 'electron'; -import {I18nContext} from "../../../utils/i18n"; +import { I18nContext } from "../../../utils/i18n"; const RECEIVE = 'RECEIVE'; const SEND = 'SEND'; @@ -87,13 +87,15 @@ class Transaction extends Component { }); renderTimestamp = tx => { - const {year, month, day, time} = createAMPMTimeStamp(tx.date); + const date = new Date(tx.date); + const formattedDate = dateTimeFormatters.date.format(date); + const formattedTime = dateTimeFormatters.time.format(date); return (
- - {month}/{day}/{year} + + {formattedDate}
@@ -176,21 +178,21 @@ class Transaction extends Component { { [RECEIVE, COINBASE, REDEEM, REVEAL, REGISTER].includes(tx.type) ? '+' - : [UPDATE, RENEW, OPEN, FINALIZE, CLAIM].includes(tx.type) + : [UPDATE, RENEW, OPEN, FINALIZE, CLAIM].includes(tx.type) ? '' : [SEND, BID].includes(tx.type) ? '-' : '' } - { (tx.type === FINALIZE && tx.value > 0) ? '+': '' } - { (tx.type === TRANSFER && tx.value > 0) ? '+': '' } + {(tx.type === FINALIZE && tx.value > 0) ? '+' : ''} + {(tx.type === TRANSFER && tx.value > 0) ? '+' : ''} {displayBalance(tx.value)} HNS ); render() { - const {transaction} = this.props; + const { transaction } = this.props; return (
@@ -202,15 +204,15 @@ class Transaction extends Component { } formatDomains(tx) { - const {t} = this.context; - const {id, domains} = tx; + const { t } = this.context; + const { id, domains } = tx; if (!domains?.length) { return `(${this.context.t('unknown')})`; } const expanded = this.state.isExpanded[id] - const domainsToDisplay = expanded ? domains : domains.slice(0,1); + const domainsToDisplay = expanded ? domains : domains.slice(0, 1); return (
{ order.push(bid.from); - const {month, day, year} = createAMPMTimeStamp(bid.date); + const formattedDate = bid.date ? + dateTimeFormatters.date.format(new Date(bid.date)) + : '(pending)'; + map[bid.from] = { - date: bid.date ? `${month}/${day}/${year}` : '(pending)', + date: formattedDate, bid: bid.bid.value, mask: bid.bid.lockup, own: bid.bid.own, diff --git a/app/utils/timeConverter.js b/app/utils/timeConverter.js index 9ca62bc97..2c16b1f29 100644 --- a/app/utils/timeConverter.js +++ b/app/utils/timeConverter.js @@ -1,33 +1,4 @@ -const createAMPMTimeStamp = timestamp => { - const date = new Date(timestamp); - const year = date - .getFullYear() - .toString() - .slice(2); - const month = pad(date.getMonth() + 1); - const day = pad(date.getDate()); - let h = date.getHours(); - const m = date.getMinutes(); - const ampm = h >= 12 ? 'pm' : 'am'; - h = h % 12; - h = h ? h : 12; // the hour '0' should be '12' - const mm = m < 10 ? '0'+m : m; - const time = h + ':' + mm + ' ' + ampm; - return { - year, - month, - day, - time - }; -}; - -function pad(num) { - if (num < 10) { - return `0${num}` - } - - return num.toString(); -} +import { app } from '@electron/remote'; export function hoursToNow(hoursUntil) { if (!hoursUntil) { @@ -46,4 +17,15 @@ export function hoursToNow(hoursUntil) { return `~${days}d ${hours}h ${mins}m` } -export default createAMPMTimeStamp; +// TODO: remove before merge +console.warn('locale:', { + locale: app.getLocale(), + systemLocale: app.getSystemLocale(), + preferredSystemLanguages: app.getPreferredSystemLanguages(), +}); + +const locale = app.getLocale(); +export const dateTimeFormatters = { + date: new Intl.DateTimeFormat(locale, { dateStyle: 'short' }), + time: new Intl.DateTimeFormat(locale, { timeStyle: 'short' }), +}