Skip to content

Commit

Permalink
fix(date-picker): 默认时区从utc修复为机器时区
Browse files Browse the repository at this point in the history
  • Loading branch information
YanHui committed Nov 11, 2022
1 parent 951ec03 commit a97f366
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
2 changes: 1 addition & 1 deletion src/static-date-picker/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ 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"]'));

Expand Down
4 changes: 2 additions & 2 deletions src/static-past-time-picker/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
Expand Down
21 changes: 15 additions & 6 deletions src/utils/timeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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,
}
);
};

// 选择器时间字符串按时区转化
Expand All @@ -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()
);
};
Expand All @@ -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');

Expand Down

0 comments on commit a97f366

Please sign in to comment.