Skip to content

Commit

Permalink
[@mantine/dates] Calendar: Fix double timezone shift (#5916)
Browse files Browse the repository at this point in the history
* Fix double timezone shift in Calendar

* fix error with double timezone shift in Calendar

* fix tests inside pr about fixing timezone in Month
  • Loading branch information
1g0rrr committed Mar 26, 2024
1 parent ec1adbf commit a7829e9
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/@mantine/dates/src/components/Month/Month.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ export const Month = factory<MonthFactory>((_props, ref) => {
const dates = getMonthDays({
month,
firstDayOfWeek: ctx.getFirstDayOfWeek(firstDayOfWeek),
timezone: ctx.timezone || undefined,
consistentWeeks: ctx.consistentWeeks,
});

Expand Down Expand Up @@ -243,6 +242,7 @@ export const Month = factory<MonthFactory>((_props, ref) => {
}}
onClick={(event) => {
dayProps?.onClick?.(event);

__onDayClick?.(event, date);
}}
onMouseDown={(event) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const defaultDates = getMonthDays({
month: new Date(2010, 5, 1),
firstDayOfWeek: 1,
consistentWeeks: false,
timezone: undefined,
});
const defaultMinDate = new Date(2000, 0);
const defaultMaxDate = new Date(2100, 0);
Expand Down Expand Up @@ -40,7 +39,6 @@ describe('@mantine/dates/get-date-in-tab-order', () => {
month: new Date(),
firstDayOfWeek: 1,
consistentWeeks: false,
timezone: undefined,
}),
defaultMinDate,
defaultMaxDate,
Expand All @@ -61,7 +59,6 @@ describe('@mantine/dates/get-date-in-tab-order', () => {
month: new Date(2010, 1, 1),
firstDayOfWeek: 1,
consistentWeeks: false,
timezone: undefined,
}),
defaultMinDate,
defaultMaxDate,
Expand Down Expand Up @@ -91,7 +88,6 @@ describe('@mantine/dates/get-date-in-tab-order', () => {
month: new Date(2010, 1, 1),
firstDayOfWeek: 1,
consistentWeeks: false,
timezone: undefined,
}),
defaultMinDate,
defaultMaxDate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ describe('@mantine/dates/get-month-days', () => {
month: new Date(2021, 1, 2),
firstDayOfWeek: 1,
consistentWeeks: false,
timezone: undefined,
});
expect(monthDays).toHaveLength(4);

Expand All @@ -24,7 +23,6 @@ describe('@mantine/dates/get-month-days', () => {
const monthDays = getMonthDays({
month: new Date(2021, 1, 2),
firstDayOfWeek: 0,
timezone: undefined,
consistentWeeks: false,
});
expect(monthDays).toHaveLength(5);
Expand All @@ -42,7 +40,6 @@ describe('@mantine/dates/get-month-days', () => {
month: new Date(2021, 3, 2),
firstDayOfWeek: 1,
consistentWeeks: false,
timezone: undefined,
});

expect(monthDays).toHaveLength(5);
Expand All @@ -58,7 +55,6 @@ describe('@mantine/dates/get-month-days', () => {
month: new Date(2021, 3, 2),
firstDayOfWeek: 0,
consistentWeeks: false,
timezone: undefined,
});

expect(monthDays).toHaveLength(5);
Expand All @@ -74,7 +70,6 @@ describe('@mantine/dates/get-month-days', () => {
month: new Date(2021, 1, 2),
firstDayOfWeek: 1,
consistentWeeks: true,
timezone: undefined,
});

expect(monthDays).toHaveLength(6);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
import { DayOfWeek } from '../../../types';
import { shiftTimezone } from '../../../utils';
import { getEndOfWeek } from '../get-end-of-week/get-end-of-week';
import { getStartOfWeek } from '../get-start-of-week/get-start-of-week';

interface GetMonthDaysInput {
month: Date;
firstDayOfWeek: DayOfWeek | undefined;
timezone: string | undefined;
consistentWeeks: boolean | undefined;
}

export function getMonthDays({
month,
firstDayOfWeek = 1,
timezone = undefined,
consistentWeeks,
}: GetMonthDaysInput): Date[][] {
const currentMonth = month.getMonth();
const startOfMonth = shiftTimezone(
'add',
new Date(month.getFullYear(), currentMonth, 1),
timezone
);
const endOfMonth = shiftTimezone(
'add',
new Date(month.getFullYear(), month.getMonth() + 1, 0),
timezone
);
const startOfMonth = new Date(month.getFullYear(), currentMonth, 1);
const endOfMonth = new Date(month.getFullYear(), month.getMonth() + 1, 0);
const endDate = getEndOfWeek(endOfMonth, firstDayOfWeek);
const date = getStartOfWeek(startOfMonth, firstDayOfWeek);
const weeks: Date[][] = [];
Expand Down

0 comments on commit a7829e9

Please sign in to comment.