Skip to content
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(date-picker): 默认时区从utc修复为机器时区 #2112

Merged
merged 1 commit into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 7 additions & 7 deletions src/static-date-picker/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import StaticDatePicker from '../StaticDatePicker';
describe('Testing StaticDatePicker ', () => {
it('without params', () => {
render(<StaticDatePicker />);
expect(screen.getByText('12')).toBeTruthy();
expect(screen.getByText('26')).toBeTruthy();
});

it('disabledDate is function', () => {
render(<StaticDatePicker disabledDate={(current: Date) => current.getTime() > new Date().getTime()} />);
expect(screen.getByText('12')).toBeTruthy();
expect(screen.getByText('26')).toBeTruthy();
});

it('onPanelChange has onPanelChange', () => {
Expand All @@ -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', () => {
Expand All @@ -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();
});
});
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