Skip to content

Commit

Permalink
Merge pull request #6017 from nextcloud-libraries/fix/relative-time
Browse files Browse the repository at this point in the history
fix: Adjust time intervals for relative time
  • Loading branch information
susnux authored Aug 29, 2024
2 parents 115c2b8 + c4a6618 commit 47e15cd
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/composables/useFormatDateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,31 +74,31 @@ export function useFormatDateTime(

const diff = date.value.getTime() - currentTime.value
const seconds = diff / 1000
if (Math.abs(seconds) <= 90) {
if (Math.abs(seconds) < 59.5) {
if (wrappedOptions.value.ignoreSeconds) {
return FEW_SECONDS_AGO[wrappedOptions.value.relativeTime]
} else {
return formatter.format(Math.round(seconds), 'second')
}
}
const minutes = seconds / 60
if (Math.abs(minutes) <= 90) {
if (Math.abs(minutes) <= 59) {
return formatter.format(Math.round(minutes), 'minute')
}
const hours = minutes / 60
if (Math.abs(hours) <= 24) {
if (Math.abs(hours) < 23.5) {
return formatter.format(Math.round(hours), 'hour')
}
const days = hours / 24
if (Math.abs(days) <= 6) {
if (Math.abs(days) < 6.5) {
return formatter.format(Math.round(days), 'day')
}
const weeks = days / 7
if (Math.abs(weeks) <= 4) {
if (Math.abs(days) < 27.5) {
const weeks = days / 7
return formatter.format(Math.round(weeks), 'week')
}
const months = days / 30
if (Math.abs(months) <= 12) {
if (Math.abs(months) < 11.5) {
return formatter.format(Math.round(months), 'month')
}
return formatter.format(Math.round(days / 365), 'year')
Expand Down
71 changes: 71 additions & 0 deletions tests/unit/composables/useFormatDateTime.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,75 @@ describe('useFormatDateTime composable', () => {
await nextTick()
expect(ctx.formattedTime.value).toBe('a few seconds ago')
})

describe('relative time intervals', () => {
const time = ref(Date.now())
const ctx = useFormatDateTime(time, { ignoreSeconds: false, relativeTime: 'long' })

test('t < 60s -> seconds', async () => {
time.value = Date.now() - (50 * 1000)
await nextTick()
expect(ctx.formattedTime.value).toMatch(/\d sec/)
})

test('t >= 60s -> minutes', async () => {
time.value = Date.now() - (60 * 1000)
await nextTick()
expect(ctx.formattedTime.value).toMatch(/minute/)
})

test('t >= 60min -> hour', async () => {
time.value = Date.now() - (60 * 60 * 1000)
await nextTick()
expect(ctx.formattedTime.value).toMatch(/hour/)
})

test('t <= 23h -> hour', async () => {
time.value = Date.now() - (23 * 60 * 60 * 1000)
await nextTick()
expect(ctx.formattedTime.value).toMatch(/hour/)
})

test('t >= 24h -> days', async () => {
time.value = Date.now() - (24 * 60 * 60 * 1000)
await nextTick()
expect(ctx.formattedTime.value).toMatch(/day/)
})

test('t <= 6days -> days', async () => {
time.value = Date.now() - (6 * 24 * 60 * 60 * 1000)
await nextTick()
expect(ctx.formattedTime.value).toMatch(/day/)
})

test('t >= 7 days -> weeks', async () => {
time.value = Date.now() - (7 * 24 * 60 * 60 * 1000)
await nextTick()
expect(ctx.formattedTime.value).toMatch(/week/)
})

test('t < 28 days -> weeks', async () => {
time.value = Date.now() - (21 * 24 * 60 * 60 * 1000)
await nextTick()
expect(ctx.formattedTime.value).toMatch(/week/)
})

test('t >= 28 days -> months', async () => {
time.value = Date.now() - (28 * 24 * 60 * 60 * 1000)
await nextTick()
expect(ctx.formattedTime.value).toMatch(/month/)
})

test('t <= 11 month -> month', async () => {
time.value = Date.now() - (335 * 24 * 60 * 60 * 1000)
await nextTick()
expect(ctx.formattedTime.value).toMatch(/month/)
})

test('t >= 12 month -> years', async () => {
time.value = Date.now() - (365 * 24 * 60 * 60 * 1000)
await nextTick()
expect(ctx.formattedTime.value).toMatch(/year/)
})
})
})

0 comments on commit 47e15cd

Please sign in to comment.