dify/web/hooks/use-timestamp.ts
盐粒 Yanli 52c106b532
feat(agent-v2): sync nightly updates to main (2026-06-25) (#37915)
Co-authored-by: Joel <iamjoel007@gmail.com>
Co-authored-by: yyh <yuanyouhuilyz@gmail.com>
Co-authored-by: Yansong Zhang <916125788@qq.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-06-26 13:13:35 +00:00

40 lines
1.2 KiB
TypeScript

'use client'
import { useQuery } from '@tanstack/react-query'
import dayjs from 'dayjs'
import timezone from 'dayjs/plugin/timezone'
import utc from 'dayjs/plugin/utc'
import { useCallback } from 'react'
import { userProfileQueryOptions } from '@/features/account-profile/client'
dayjs.extend(utc)
dayjs.extend(timezone)
type UseTimestampOptions = {
timezone?: string
}
const getBrowserTimezone = () => {
return Intl.DateTimeFormat().resolvedOptions().timeZone
}
const useTimestamp = ({ timezone: timezoneOverride }: UseTimestampOptions = {}) => {
const { data: accountTimezone } = useQuery({
...userProfileQueryOptions(),
select: data => data.profile.timezone ?? undefined,
enabled: timezoneOverride === undefined,
})
const resolvedTimezone = timezoneOverride ?? accountTimezone ?? getBrowserTimezone()
const formatTime = useCallback((value: number, format: string) => {
return dayjs.unix(value).tz(resolvedTimezone).format(format)
}, [resolvedTimezone])
const formatDate = useCallback((value: string, format: string) => {
return dayjs(value).tz(resolvedTimezone).format(format)
}, [resolvedTimezone])
return { formatTime, formatDate }
}
export default useTimestamp