Skip to content

Commit

Permalink
Fix 30-day month relative date calculation scenarios through new opti…
Browse files Browse the repository at this point in the history
…onal parameter
  • Loading branch information
kommunarr committed Apr 12, 2024
1 parent a4d9e6f commit 08b532f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/renderer/components/ft-list-video/ft-list-video.js
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,8 @@ export default defineComponent({
if (this.inHistory) {
this.uploadedTime = new Date(this.data.published).toLocaleDateString([this.currentLocale, 'en'])
} else {
this.uploadedTime = getRelativeTimeFromDate(new Date(this.data.published).toDateString())
// Use 30 days per month, just like calculatePublishedDate
this.uploadedTime = getRelativeTimeFromDate(new Date(this.data.published).toDateString(), false)
}
}

Expand Down
27 changes: 15 additions & 12 deletions src/renderer/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export function calculatePublishedDate(publishedText, isLive = false, isUpcoming
} else if (timeFrame.startsWith('week') || timeFrame === 'w') {
timeSpan = timeAmount * 604800000
} else if (timeFrame.startsWith('month') || timeFrame === 'mo') {
// 30 day month being used
timeSpan = timeAmount * 2592000000
} else if (timeFrame.startsWith('year') || timeFrame === 'y') {
timeSpan = timeAmount * 31556952000
Expand Down Expand Up @@ -720,53 +721,55 @@ export function getTodayDateStrLocalTimezone() {
return timeNowStr.split('T')[0]
}

export function getRelativeTimeFromDate(date, hideSeconds = false) {
export function getRelativeTimeFromDate(date, hideSeconds = false, useThirtyDayMonths = true) {
if (!date) {
return ''
}

const now = new Date()
const now = new Date().getTime()
// Convert from ms to second
// For easier code interpretation the value is made to be positive
// `comparisonDate` is sometimes a string
const comparisonDate = Date.parse(date)
let timeDiffFromNow = ((now - comparisonDate) / 1000)
let timeUnit = 'second'

if (timeDiffFromNow <= 60 && hideSeconds) {
if (timeDiffFromNow < 60 && hideSeconds) {
return i18n.t('Moments Ago')
}

if (timeDiffFromNow > 60) {
if (timeDiffFromNow >= 60) {
timeDiffFromNow /= 60
timeUnit = 'minute'
}

if (timeUnit === 'minute' && timeDiffFromNow > 60) {
if (timeUnit === 'minute' && timeDiffFromNow >= 60) {
timeDiffFromNow /= 60
timeUnit = 'hour'
}

if (timeUnit === 'hour' && timeDiffFromNow > 24) {
if (timeUnit === 'hour' && timeDiffFromNow >= 24) {
timeDiffFromNow /= 24
timeUnit = 'day'
}

// Diff month might have diff no. of days
// To ensure the display is fine we use 31
if (timeUnit === 'day' && timeDiffFromNow > 31) {
timeDiffFromNow /= 24
/* Different months might have a different number of days.
In some contexts, to ensure the display is fine, we use 31.
In other contexts, like when working with calculatePublishedDate, we use 30. */
const daysInMonth = useThirtyDayMonths ? 30 : 31
if (timeUnit === 'day' && timeDiffFromNow >= daysInMonth) {
timeDiffFromNow /= daysInMonth
timeUnit = 'month'
}

if (timeUnit === 'month' && timeDiffFromNow > 12) {
if (timeUnit === 'month' && timeDiffFromNow >= 12) {
timeDiffFromNow /= 12
timeUnit = 'year'
}

// Using `Math.ceil` so that -1.x days ago displayed as 1 day ago
// Notice that the value is turned to negative to be displayed as "ago"
return new Intl.RelativeTimeFormat(currentLocale()).format(Math.ceil(-timeDiffFromNow), timeUnit)
return new Intl.RelativeTimeFormat([currentLocale(), 'en']).format(Math.ceil(-timeDiffFromNow), timeUnit)
}

/**
Expand Down

0 comments on commit 08b532f

Please sign in to comment.