Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Adjust time intervals for relative time #6017

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/)
})
})
})
Loading