Skip to content

Commit

Permalink
Fix timestamp formatting (#644)
Browse files Browse the repository at this point in the history
  • Loading branch information
neoxelox authored Nov 20, 2024
1 parent 6d8d8d5 commit aaaae30
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import {
TableRow,
Text,
} from '@latitude-data/web-ui'
import { relativeTime } from '$/app/_lib/formatUtils'
import { LogicTablePaginationFooterWithoutCount } from '$/components/TablePaginationFooter/TablePaginationFooterWithoutCount'
import { relativeTime } from '$/lib/relativeTime'
import { EvaluationResultByDocument } from '$/stores/evaluationResultsByDocumentContent'

export const ResultCellContent = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ import {
useCurrentCommit,
useCurrentProject,
} from '@latitude-data/web-ui'
import { formatCostInMillicents, relativeTime } from '$/app/_lib/formatUtils'
import { formatCostInMillicents } from '$/app/_lib/formatUtils'
import { getRunErrorFromErrorable } from '$/app/(private)/_lib/getRunErrorFromErrorable'
import { useCurrentDocument } from '$/app/providers/DocumentProvider'
import { LinkableTablePaginationFooter } from '$/components/TablePaginationFooter'
import { relativeTime } from '$/lib/relativeTime'
import { ROUTES } from '$/services/routes'
import useEvaluationResultsPagination from '$/stores/useEvaluationResultsCount'
import { useSearchParams } from 'next/navigation'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ import {
useCurrentCommit,
useCurrentProject,
} from '@latitude-data/web-ui'
import {
formatCostInMillicents,
formatDuration,
relativeTime,
} from '$/app/_lib/formatUtils'
import { formatCostInMillicents, formatDuration } from '$/app/_lib/formatUtils'
import { getRunErrorFromErrorable } from '$/app/(private)/_lib/getRunErrorFromErrorable'
import { useCurrentDocument } from '$/app/providers/DocumentProvider'
import { LinkableTablePaginationFooter } from '$/components/TablePaginationFooter'
import { relativeTime } from '$/lib/relativeTime'
import { ROUTES } from '$/services/routes'
import useDocumentLogsPagination from '$/stores/useDocumentLogsPagination'
import { useSearchParams } from 'next/navigation'
Expand Down
13 changes: 0 additions & 13 deletions apps/web/src/app/_lib/formatUtils.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
import { format, formatDistanceToNow, formatRelative } from 'date-fns'

const SECONDS = 1000 // ms
const MINUTES = 60 * SECONDS
const HOURS = MINUTES * 60
const DAYS = HOURS * 24
export function relativeTime(date: Date | null) {
if (date == null) return 'never'
if (!(date instanceof Date)) return '-' // NOTE: This is a dummy defense to avoid crashing on the frontend

const now = new Date()
const diff = now.getTime() - date.getTime()
if (diff < 1 * HOURS) return formatDistanceToNow(date, { addSuffix: true })
if (diff < 7 * DAYS) return formatRelative(date, new Date())
return format(date, 'PPpp')
}

export function formatDuration(duration?: number | null) {
if (!duration) return '-'
Expand Down
52 changes: 52 additions & 0 deletions apps/web/src/lib/relativeTime.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'

import { relativeTime } from './relativeTime'

describe('relativeTime', () => {
const SECONDS = 1000
const MINUTES = 60 * SECONDS
const HOURS = 60 * MINUTES
const DAYS = 24 * HOURS
const YEARS = 365 * DAYS
const NOW = new Date(2000, 6, 31, 12, 0, 0)

beforeEach(() => {
vi.useFakeTimers()
vi.setSystemTime(NOW)
})

it('returns formatted relative time', () => {
expect(relativeTime(new Date(NOW.getTime() - 3 * SECONDS))).toBe(
'Less than a minute ago',
)
expect(relativeTime(new Date(NOW.getTime() - 30 * SECONDS))).toBe(
'1 minute ago',
)
expect(relativeTime(new Date(NOW.getTime() - 30 * MINUTES))).toBe(
'30 minutes ago',
)
expect(relativeTime(new Date(NOW.getTime() - 59 * MINUTES))).toBe(
'About 1 hour ago',
)
expect(relativeTime(new Date(NOW.getTime() - 2 * HOURS))).toBe(
'Today at 10:00 AM',
)
expect(relativeTime(new Date(NOW.getTime() - 3 * DAYS))).toBe(
'Last Friday at 12:00 PM',
)
expect(
relativeTime(new Date(NOW.getTime() - (6 * DAYS + 12 * HOURS))),
).toBe('Jul 25, 2000, 12:00:00 AM')
expect(relativeTime(new Date(NOW.getTime() - 7 * DAYS))).toBe(
'Jul 24, 2000, 12:00:00 PM',
)
expect(relativeTime(new Date(NOW.getTime() - 1 * YEARS))).toBe(
'Aug 1, 1999, 12:00:00 PM',
)
})

it('returns "-" when no date is provided', () => {
expect(relativeTime(null)).toBe('-')
expect(relativeTime(undefined)).toBe('-')
})
})
28 changes: 19 additions & 9 deletions apps/web/src/lib/relativeTime.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import { format, formatDistanceToNow, formatRelative } from 'date-fns'
import {
differenceInDays,
differenceInHours,
format,
formatDistanceToNow,
formatRelative,
} from 'date-fns'

const HOURS = 1000 * 60 * 60
const DAYS = HOURS * 24
function capitalize(str: string) {
return str.replace(/^\w/, (c) => c.toUpperCase())
}

export function relativeTime(date?: Date | null) {
if (date == null) return '-'
export function relativeTime(date: Date | null | undefined) {
if (!date) return '-'
// NOTE: This is a dummy defense to avoid crashing on the frontend
if (!(date instanceof Date)) return '-'

const now = new Date()
const diff = now.getTime() - date.getTime()
const capitalize = (str: string) => str.replace(/^\w/, (c) => c.toUpperCase())

if (diff < 1 * HOURS) {
if (differenceInHours(now, date) < 1) {
return capitalize(formatDistanceToNow(date, { addSuffix: true }))
}
if (diff < 7 * DAYS) return capitalize(formatRelative(date, new Date()))

if (differenceInDays(now, date) < 6) {
return capitalize(formatRelative(date, now))
}

return capitalize(format(date, 'PPpp'))
}

0 comments on commit aaaae30

Please sign in to comment.