Skip to content

Commit f5a211e

Browse files
authored
Merge pull request #219 from duyet/fix/timezone
fix: local fromNow format timezone-aware
2 parents 10d74e6 + 2a2de06 commit f5a211e

File tree

6 files changed

+65
-29
lines changed

6 files changed

+65
-29
lines changed

app/api/timezone/route.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { NextResponse } from 'next/server'
2+
3+
import { fetchData } from '@/lib/clickhouse'
4+
5+
export async function GET() {
6+
try {
7+
const resp = await fetchData<{ tz: string }[]>({
8+
query: 'SELECT timezone() as tz',
9+
})
10+
return NextResponse.json({
11+
tz: resp[0].tz,
12+
})
13+
} catch {
14+
return NextResponse.json({}, { status: 500 })
15+
}
16+
}

app/api/version/route.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

components/data-table/cell.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
ColumnFormat,
66
ColumnFormatOptions,
77
} from '@/components/data-table/column-defs'
8-
import dayjs from '@/lib/dayjs'
98
import { formatReadableQuantity } from '@/lib/format-readable'
109

1110
import { ActionFormat } from './cells/action-format'
@@ -15,7 +14,9 @@ import { BadgeFormat } from './cells/badge-format'
1514
import { BooleanFormat } from './cells/boolean-format'
1615
import { CodeToggleFormat } from './cells/code-toggle-format'
1716
import { ColoredBadgeFormat } from './cells/colored-badge-format'
17+
import { DurationFormat } from './cells/duration-format'
1818
import { LinkFormat } from './cells/link-format'
19+
import { RelatedTimeFormat } from './cells/related-time-format'
1920

2021
export const formatCell = <TData extends RowData, TValue>(
2122
table: Table<TData>,
@@ -52,14 +53,10 @@ export const formatCell = <TData extends RowData, TValue>(
5253
return <CodeToggleFormat row={row} value={value} />
5354

5455
case ColumnFormat.RelatedTime:
55-
let fromNow = dayjs(value as string).fromNow()
56-
return <span title={value as string}>{fromNow}</span>
56+
return <RelatedTimeFormat value={value} />
5757

5858
case ColumnFormat.Duration:
59-
let humanized = dayjs
60-
.duration({ seconds: parseFloat(value as string) })
61-
.humanize()
62-
return <span title={value as string}>{humanized}</span>
59+
return <DurationFormat value={value} />
6360

6461
case ColumnFormat.Boolean:
6562
return <BooleanFormat value={value} />
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import dayjs from '@/lib/dayjs'
2+
3+
interface DurationFormatProps {
4+
value: any
5+
}
6+
7+
export async function DurationFormat({ value }: DurationFormatProps) {
8+
let humanized = dayjs
9+
.duration({ seconds: parseFloat(value as string) })
10+
.humanize()
11+
12+
return <span title={value as string}>{humanized}</span>
13+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import dayjs from '@/lib/dayjs'
2+
3+
interface RelatedTimeFormatProps {
4+
value: any
5+
}
6+
7+
let tz: string = ""
8+
9+
export async function RelatedTimeFormat({ value }: RelatedTimeFormatProps) {
10+
let fromNow
11+
try {
12+
if (!tz) {
13+
// Getting timezone from server
14+
tz = await fetch('/api/timezone')
15+
.then((res) => res.json())
16+
.then((data) => data.tz)
17+
console.log('Server timezone:', tz)
18+
}
19+
20+
let parsed = dayjs.tz(value as string, tz)
21+
fromNow = parsed.fromNow()
22+
} catch (e) {
23+
console.error('Error parsing time:', e)
24+
fromNow = dayjs(value as string).fromNow()
25+
}
26+
27+
return <span title={value as string}>{fromNow}</span>
28+
}

lib/dayjs.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import dayjs from 'dayjs'
22
import duration from 'dayjs/plugin/duration'
33
import relativeTime from 'dayjs/plugin/relativeTime'
4+
import timezone from 'dayjs/plugin/timezone'
5+
import utc from 'dayjs/plugin/utc'
46

57
import 'dayjs/locale/en'
68

79
dayjs.extend(duration)
10+
dayjs.extend(utc)
11+
dayjs.extend(timezone)
812
dayjs.extend(relativeTime)
913

1014
export default dayjs

0 commit comments

Comments
 (0)