Skip to content

Commit

Permalink
Update relative time function #1163
Browse files Browse the repository at this point in the history
  • Loading branch information
flashburst committed Apr 2, 2024
1 parent b53309d commit bb0ff86
Showing 1 changed file with 32 additions and 81 deletions.
113 changes: 32 additions & 81 deletions src/utils/formatter/relative-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,120 +17,71 @@ const WEEK = 7 * DAY
const MONTH = 30 * DAY
const YEAR = 365 * DAY

/**
* @type {Array<{unit: Intl.RelativeTimeFormatUnit, divisor: number}>}
*/
const units = [
{
max: 30 * SECOND,
divisor: 1,
past1: 'just now',
pastN: 'just now',
future1: 'just now',
futureN: 'just now'
unit: 'year',
divisor: YEAR
},
{
max: MINUTE,
divisor: SECOND,
past1: 'a second ago',
pastN: '# seconds ago',
future1: 'in a second',
futureN: 'in # seconds'
unit: 'month',
divisor: MONTH
},
{
max: HOUR,
divisor: MINUTE,
past1: 'a minute ago',
pastN: '# minutes ago',
future1: 'in a minute',
futureN: 'in # minutes'
unit: 'week',
divisor: WEEK
},
{
max: DAY,
divisor: HOUR,
past1: 'an hour ago',
pastN: '# hours ago',
future1: 'in an hour',
futureN: 'in # hours'
unit: 'day',
divisor: DAY
},
{
max: WEEK,
divisor: DAY,
past1: 'yesterday',
pastN: '# days ago',
future1: 'tomorrow',
futureN: 'in # days'
unit: 'hour',
divisor: HOUR
},
{
max: 4 * WEEK,
divisor: WEEK,
past1: 'last week',
pastN: '# weeks ago',
future1: 'in a week',
futureN: 'in # weeks'
unit: 'minute',
divisor: MINUTE
},
{
max: YEAR,
divisor: MONTH,
past1: 'last month',
pastN: '# months ago',
future1: 'in a month',
futureN: 'in # months'
},
{
max: 100 * YEAR,
divisor: YEAR,
past1: 'last year',
pastN: '# years ago',
future1: 'in a year',
futureN: 'in # years'
},
{
max: 1000 * YEAR,
divisor: 100 * YEAR,
past1: 'last century',
pastN: '# centuries ago',
future1: 'in a century',
futureN: 'in # centuries'
},
{
max: Infinity,
divisor: 1000 * YEAR,
past1: 'last millennium',
pastN: '# millennia ago',
future1: 'in a millennium',
futureN: 'in # millennia'
unit: 'second',
divisor: SECOND
}
]

export const fromNow = (date) => {
export const fromNow = (date, locale = 'en') => {
if (!date) {
return ''
}

if (parseInt(date) === 0) {
if (typeof date === 'string' && parseInt(date) === 0) {
return 'Not available'
}

if (!(date instanceof Date)) {
if (!(date instanceof Date) && ['string', 'number'].includes(typeof date)) {
date = DateLib.fromUnix(date)
}

const diff =
Date.now() - (typeof date === 'object' ? date : new Date(date)).getTime()
const diff = Date.now() - (typeof date === 'object' ? date : new Date(date)).getTime()
const diffAbs = Math.abs(diff)

for (const unit of units) {
if (diffAbs > unit.max) {
continue
}
const matchedUnit = units.find(unit => { return (diffAbs / unit.divisor) >= 1 })

const isFuture = diff < 0
const x = Math.round(Math.abs(diff) / unit.divisor)
if (matchedUnit) {
const { divisor, unit } = matchedUnit
let value = Math.round(diff / divisor)
value = value > 0 ? -1 * value : Math.abs(value)

if (x <= 1) {
return isFuture ? unit.future1 : unit.past1
}
const rtf = new Intl.RelativeTimeFormat(locale, {
numeric: 'auto'
})

return (isFuture ? unit.futureN : unit.pastN).replace('#', x.toString())
return rtf.format(value, unit)
}

return 'just now'
}

export const getUtcFormatString = (timestamp, locale = 'en') => {
Expand Down

0 comments on commit bb0ff86

Please sign in to comment.