fix: not use local time

This commit is contained in:
Joel 2025-06-30 18:22:40 +08:00
parent 63a1a1077e
commit 0625d6a361
3 changed files with 46 additions and 4 deletions

View File

@ -8,7 +8,8 @@ import { useTranslation } from 'react-i18next'
import TimePicker from '@/app/components/base/date-and-time-picker/time-picker'
import OptionCard from '@/app/components/workflow/nodes/_base/components/option-card'
import PluginsPicker from './plugins-picker'
import { dayjsToTimeOfDay, timeOfDayToDayjs } from './utils'
import { convertLocalSecondsToUTCDaySeconds, convertUTCDaySecondsToLocalSeconds, dayjsToTimeOfDay, timeOfDayToDayjs } from './utils'
import { useAppContext } from '@/context/app-context'
const i18nPrefix = 'plugin.autoUpdate'
@ -22,6 +23,8 @@ const AutoUpdateSetting: FC<Props> = ({
onChange,
}) => {
const { t } = useTranslation()
const { userProfile: { timezone } } = useAppContext()
const {
strategy_setting,
upgrade_time_of_day,
@ -97,9 +100,10 @@ const AutoUpdateSetting: FC<Props> = ({
<div className='flex items-center justify-between'>
<Label label={t(`${i18nPrefix}.updateTime`)} />
<TimePicker
value={timeOfDayToDayjs(upgrade_time_of_day)}
onChange={v => handleChange('upgrade_time_of_day')(dayjsToTimeOfDay(v))}
onClear={() => handleChange('upgrade_time_of_day')(0)}
value={timeOfDayToDayjs(convertUTCDaySecondsToLocalSeconds(upgrade_time_of_day, timezone!))}
timezone={timezone}
onChange={v => handleChange('upgrade_time_of_day')(convertLocalSecondsToUTCDaySeconds(dayjsToTimeOfDay(v), timezone!))}
onClear={() => handleChange('upgrade_time_of_day')(convertLocalSecondsToUTCDaySeconds(0, timezone!))}
popupClassName='z-[99]'
title={t(`${i18nPrefix}.updateTime`)}
minuteFilter={minuteFilter}

View File

@ -0,0 +1,15 @@
// write unit test for convertLocalSecondsToUTCDaySeconds
import { convertLocalSecondsToUTCDaySeconds, convertUTCDaySecondsToLocalSeconds } from './utils'
describe('convertLocalSecondsToUTCDaySeconds', () => {
it('should convert local seconds to UTC day seconds correctly', () => {
const localTimezone = 'Asia/Shanghai'
const utcSeconds = convertLocalSecondsToUTCDaySeconds(0, localTimezone)
expect(utcSeconds).toBe((24 - 8) * 3600)
})
it('should convert local seconds to UTC day seconds for a specific time', () => {
const localTimezone = 'Asia/Shanghai'
expect(convertUTCDaySecondsToLocalSeconds(convertLocalSecondsToUTCDaySeconds(0, localTimezone), localTimezone)).toBe(0)
})
})

View File

@ -1,5 +1,10 @@
import type { Dayjs } from 'dayjs'
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
import timezone from 'dayjs/plugin/timezone'
dayjs.extend(utc)
dayjs.extend(timezone)
export const timeOfDayToDayjs = (timeOfDay: number): Dayjs => {
const hours = Math.floor(timeOfDay / 3600)
@ -8,7 +13,25 @@ export const timeOfDayToDayjs = (timeOfDay: number): Dayjs => {
return res
}
export const convertLocalSecondsToUTCDaySeconds = (secondsInDay: number, localTimezone: string): number => {
const localDayStart = dayjs().tz(localTimezone).startOf('day')
const localTargetTime = localDayStart.add(secondsInDay, 'second')
const utcTargetTime = localTargetTime.utc()
const utcDayStart = utcTargetTime.startOf('day')
const secondsFromUTCMidnight = utcTargetTime.diff(utcDayStart, 'second')
return secondsFromUTCMidnight
}
export const dayjsToTimeOfDay = (date?: Dayjs): number => {
if(!date) return 0
return date.hour() * 3600 + date.minute() * 60
}
export const convertUTCDaySecondsToLocalSeconds = (utcDaySeconds: number, localTimezone: string): number => {
const utcDayStart = dayjs().utc().startOf('day')
const utcTargetTime = utcDayStart.add(utcDaySeconds, 'second')
const localTargetTime = utcTargetTime.tz(localTimezone)
const localDayStart = localTargetTime.startOf('day')
const secondsInLocalDay = localTargetTime.diff(localDayStart, 'second')
return secondsInLocalDay
}