-
Notifications
You must be signed in to change notification settings - Fork 14
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: local fromNow format timezone-aware #219
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { NextResponse } from 'next/server' | ||
|
||
import { fetchData } from '@/lib/clickhouse' | ||
|
||
export async function GET() { | ||
try { | ||
const resp = await fetchData<{ tz: string }[]>({ | ||
query: 'SELECT timezone() as tz', | ||
}) | ||
return NextResponse.json({ | ||
tz: resp[0].tz, | ||
}) | ||
} catch { | ||
return NextResponse.json({}, { status: 500 }) | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import dayjs from '@/lib/dayjs' | ||
|
||
interface DurationFormatProps { | ||
value: any | ||
} | ||
|
||
export async function DurationFormat({ value }: DurationFormatProps) { | ||
let humanized = dayjs | ||
.duration({ seconds: parseFloat(value as string) }) | ||
.humanize() | ||
|
||
return <span title={value as string}>{humanized}</span> | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,28 @@ | ||||||||||||||||||||||||
import dayjs from '@/lib/dayjs' | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Missing test for RelatedTimeFormat component The new RelatedTimeFormat component handles timezone-aware formatting. It's crucial to ensure this component behaves as expected under various conditions, including edge cases like invalid date formats or null values. Please add unit tests to verify the functionality and error handling.
Suggested change
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
interface RelatedTimeFormatProps { | ||||||||||||||||||||||||
value: any | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
let tz: string = "" | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
export async function RelatedTimeFormat({ value }: RelatedTimeFormatProps) { | ||||||||||||||||||||||||
let fromNow | ||||||||||||||||||||||||
try { | ||||||||||||||||||||||||
if (!tz) { | ||||||||||||||||||||||||
// Getting timezone from server | ||||||||||||||||||||||||
tz = await fetch('/api/timezone') | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (code_refinement): Consider error handling for the fetch request to improve reliability. Adding error handling for the fetch request can prevent the application from breaking if the API call fails.
Suggested change
|
||||||||||||||||||||||||
.then((res) => res.json()) | ||||||||||||||||||||||||
.then((data) => data.tz) | ||||||||||||||||||||||||
console.log('Server timezone:', tz) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
let parsed = dayjs.tz(value as string, tz) | ||||||||||||||||||||||||
fromNow = parsed.fromNow() | ||||||||||||||||||||||||
} catch (e) { | ||||||||||||||||||||||||
console.error('Error parsing time:', e) | ||||||||||||||||||||||||
fromNow = dayjs(value as string).fromNow() | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): Fallback logic in catch block may not consider timezone. The fallback logic uses the default timezone instead of the server timezone, which might not be intended. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will not using timezone here |
||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
return <span title={value as string}>{fromNow}</span> | ||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,10 +1,14 @@ | ||||||||
import dayjs from 'dayjs' | ||||||||
import duration from 'dayjs/plugin/duration' | ||||||||
import relativeTime from 'dayjs/plugin/relativeTime' | ||||||||
import timezone from 'dayjs/plugin/timezone' | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Consider adding tests for timezone and UTC configurations in dayjs With the addition of timezone and UTC plugins to the dayjs configuration, it's important to verify that these settings are correctly applied across the application. This could involve checking the correct timezone adjustments and UTC handling in date/time calculations.
Suggested change
|
||||||||
import utc from 'dayjs/plugin/utc' | ||||||||
|
||||||||
import 'dayjs/locale/en' | ||||||||
|
||||||||
dayjs.extend(duration) | ||||||||
dayjs.extend(utc) | ||||||||
dayjs.extend(timezone) | ||||||||
dayjs.extend(relativeTime) | ||||||||
|
||||||||
export default dayjs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (testing): Missing test for DurationFormat component
The DurationFormat component has been introduced to handle human-readable duration formatting. Testing this component is essential to ensure it accurately converts various duration inputs into the correct format. Please add tests to cover typical, boundary, and erroneous input scenarios.