Skip to content

Commit

Permalink
fix(date-picker): 默认时区从utc修复为机器时区 (#2112)
Browse files Browse the repository at this point in the history
Co-authored-by: YanHui <yanhui@growingio.com>
  • Loading branch information
hiker90 and YanHui committed Nov 11, 2022
1 parent 951ec03 commit b2d07e8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 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
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

1 comment on commit b2d07e8

@vercel
Copy link

@vercel vercel bot commented on b2d07e8 Nov 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

gio-design – ./

gio-design-growingio.vercel.app
gio-design-git-master-growingio.vercel.app
gio-design.vercel.app

Please sign in to comment.