From 3b89bbd2937ef5873e8979c6914fa36f53b27e4f Mon Sep 17 00:00:00 2001 From: YanHui Date: Fri, 11 Nov 2022 14:38:45 +0800 Subject: [PATCH] =?UTF-8?q?fix(date-picker):=20=E9=BB=98=E8=AE=A4=E6=97=B6?= =?UTF-8?q?=E5=8C=BA=E4=BB=8Eutc=E4=BF=AE=E5=A4=8D=E4=B8=BA=E6=9C=BA?= =?UTF-8?q?=E5=99=A8=E6=97=B6=E5=8C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jest.config.js | 2 +- .../__tests__/index.test.tsx | 14 ++++++------- src/static-past-time-picker/utils.ts | 4 ++-- src/utils/timeHelper.ts | 21 +++++++++++++------ 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/jest.config.js b/jest.config.js index 71fc50a3c4..c2c328d060 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,5 +1,5 @@ // set default timeZone UTC -process.env.TZ = 'UTC'; +process.env.TZ = Intl.DateTimeFormat().resolvedOptions().timeZone; module.exports = { // if you're also using typescript preset: 'ts-jest', diff --git a/src/static-date-picker/__tests__/index.test.tsx b/src/static-date-picker/__tests__/index.test.tsx index 09ae92ee22..99f1b06de7 100644 --- a/src/static-date-picker/__tests__/index.test.tsx +++ b/src/static-date-picker/__tests__/index.test.tsx @@ -5,12 +5,12 @@ import StaticDatePicker from '../StaticDatePicker'; describe('Testing StaticDatePicker ', () => { it('without params', () => { render(); - expect(screen.getByText('12')).toBeTruthy(); + expect(screen.getByText('26')).toBeTruthy(); }); it('disabledDate is function', () => { render( current.getTime() > new Date().getTime()} />); - expect(screen.getByText('12')).toBeTruthy(); + expect(screen.getByText('26')).toBeTruthy(); }); it('onPanelChange has onPanelChange', () => { @@ -24,15 +24,15 @@ describe('Testing StaticDatePicker ', () => { fireEvent.click(container.querySelector('button[class="gio-picker-header-super-prev-btn"]')); - expect(screen.getByText('12')).toBeTruthy(); + expect(screen.getByText('26')).toBeTruthy(); fireEvent.click(container.querySelector('button[class="gio-picker-header-prev-btn"]')); - expect(screen.getByText('12')).toBeTruthy(); + expect(screen.getByText('26')).toBeTruthy(); - fireEvent.click(screen.getByText('12')); + fireEvent.click(screen.getByText('26')); - expect(screen.getByText('12')).toBeTruthy(); + expect(screen.getByText('26')).toBeTruthy(); }); it('onPanelChange not onPanelChange', () => { @@ -42,6 +42,6 @@ describe('Testing StaticDatePicker ', () => { fireEvent.click(container.querySelector('button[class="gio-picker-header-super-prev-btn"]')); - expect(screen.getByText('12')).toBeTruthy(); + expect(screen.getByText('26')).toBeTruthy(); }); }); diff --git a/src/static-past-time-picker/utils.ts b/src/static-past-time-picker/utils.ts index 87d451969e..24f7af9a3e 100644 --- a/src/static-past-time-picker/utils.ts +++ b/src/static-past-time-picker/utils.ts @@ -31,10 +31,10 @@ export const startOfTodayInTimezone = () => momentTZ .tz( `${momentTZ - .tz(new Date(), localStorage.getItem('timezone') || 'UTC') + .tz(new Date(), localStorage.getItem('timezone') || Intl.DateTimeFormat().resolvedOptions().timeZone) .format() .substring(0, 10)} 00:00:00`, - localStorage.getItem('timezone') || 'UTC' + localStorage.getItem('timezone') || Intl.DateTimeFormat().resolvedOptions().timeZone ) .format() ); diff --git a/src/utils/timeHelper.ts b/src/utils/timeHelper.ts index 0e06863c84..d595b6e3a2 100644 --- a/src/utils/timeHelper.ts +++ b/src/utils/timeHelper.ts @@ -5,7 +5,9 @@ import { format as dateFnsFormat, utcToZonedTime } from 'date-fns-tz'; // 时间日期转换时区 moment export const parseTimeZone = (data?: any, format?: string) => - momentTZ(data as string, format).tz(localStorage.getItem('timezone') || 'UTC'); + momentTZ(data as string, format).tz( + localStorage.getItem('timezone') || Intl.DateTimeFormat().resolvedOptions().timeZone + ); // 时间日期转换时区 date-fns export const parseFnsTimeZone = (date: number | Date | string, format: string) => { @@ -14,9 +16,13 @@ export const parseFnsTimeZone = (date: number | Date | string, format: string) = finalDate = new Date(date); } - return dateFnsFormat(utcToZonedTime(finalDate, localStorage.getItem('timezone') || 'UTC'), format, { - timeZone: localStorage.getItem('timezone') || 'UTC', - }); + return dateFnsFormat( + utcToZonedTime(finalDate, localStorage.getItem('timezone') || Intl.DateTimeFormat().resolvedOptions().timeZone), + format, + { + timeZone: localStorage.getItem('timezone') || Intl.DateTimeFormat().resolvedOptions().timeZone, + } + ); }; // 选择器时间字符串按时区转化 @@ -26,7 +32,10 @@ export const exportDateToZonedDate = (date: any, format?: string) => { if (!date) return date; return new Date( momentTZ - .tz(moment(date).format(format || 'yyyy-MM-DD HH:mm:ss'), localStorage.getItem('timezone') || 'UTC') + .tz( + moment(date).format(format || 'yyyy-MM-DD HH:mm:ss'), + localStorage.getItem('timezone') || Intl.DateTimeFormat().resolvedOptions().timeZone + ) .format() ); }; @@ -36,7 +45,7 @@ export const exportDateToZonedDate = (date: any, format?: string) => { export const exportZonedDateToDate = (date: any, format?: string) => { if (!date) return date; const arr = momentTZ - .tz(date, localStorage.getItem('timezone') || 'UTC') + .tz(date, localStorage.getItem('timezone') || Intl.DateTimeFormat().resolvedOptions().timeZone) .format() .split('T');