Skip to content

Commit 8fb9067

Browse files
authoredMay 23, 2024
fix(ui5-calendar): respect component level calendarType in week calculation (#9043)
Previously when a calendarType was being set to Islamic or Buddhist in the document configuration, and a primary-calendar-type="Gregorian" on a date component level where week numbering is present, for example <ui5-date-picker primary-calendar-type="Gregorian">, <ui5-calendar primary-calendar-type="Gregorian", etc the week numbers were calculated wrong. Fixes: #6835

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed
 

‎packages/localization/src/dates/calculateWeekNumber.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type Locale from "@ui5/webcomponents-base/dist/locale/Locale.js";
2+
import CalendarType from "@ui5/webcomponents-base/dist/types/CalendarType.js";
23
import UniversalDate from "./UniversalDate.js";
34
import type LocaleData from "../LocaleData.js";
45
import type UI5Date from "./UI5Date.js";
56

6-
const calculateWeekNumber = (confFirstDayOfWeek: number | undefined, oDate: Date | UI5Date, iYear: number, oLocale: Locale, oLocaleData: LocaleData) => {
7+
const calculateWeekNumber = (confFirstDayOfWeek: number | undefined, oDate: Date | UI5Date, iYear: number, oLocale: Locale, oLocaleData: LocaleData, calendarType: CalendarType) => {
78
let iWeekNum = 0;
89
let iWeekDay = 0;
910
const iFirstDayOfWeek = Number.isInteger(confFirstDayOfWeek) ? confFirstDayOfWeek! : oLocaleData.getFirstDayOfWeek();
@@ -17,32 +18,32 @@ const calculateWeekNumber = (confFirstDayOfWeek: number | undefined, oDate: Date
1718
* The first week of the year starts with January 1st. But Dec. 31 is still in the last year
1819
* So the week beginning in December and ending in January has 2 week numbers
1920
*/
20-
const oJanFirst = new UniversalDate(oDate.getTime());
21+
const oJanFirst = UniversalDate.getInstance(oDate, calendarType);
2122
oJanFirst.setUTCFullYear(iYear, 0, 1);
2223
iWeekDay = oJanFirst.getUTCDay();
2324

2425
// get the date for the same weekday like jan 1.
25-
const oCheckDate = new UniversalDate(oDate.getTime());
26+
const oCheckDate = UniversalDate.getInstance(oDate, calendarType);
2627
oCheckDate.setUTCDate(oCheckDate.getUTCDate() - oCheckDate.getUTCDay() + iWeekDay);
2728

2829
iWeekNum = Math.round((oCheckDate.getTime() - oJanFirst.getTime()) / 86400000 / 7) + 1;
2930
} else {
3031
// normally the first week of the year is the one where the first Thursday of the year is
3132
// find Thursday of this week
3233
// if the checked day is before the 1. day of the week use a day of the previous week to check
33-
const oThursday = new UniversalDate(oDate.getTime());
34+
const oThursday = UniversalDate.getInstance(oDate, calendarType);
3435
oThursday.setUTCDate(oThursday.getUTCDate() - iFirstDayOfWeek);
3536
iWeekDay = oThursday.getUTCDay();
3637
oThursday.setUTCDate(oThursday.getUTCDate() - iWeekDay + 4);
3738

38-
const oFirstDayOfYear = new UniversalDate(oThursday.getTime());
39+
const oFirstDayOfYear = UniversalDate.getInstance(new Date(oThursday.getTime()), calendarType);
3940
oFirstDayOfYear.setUTCMonth(0, 1);
4041
iWeekDay = oFirstDayOfYear.getUTCDay();
4142
let iAddDays = 0;
4243
if (iWeekDay > 4) {
4344
iAddDays = 7; // first day of year is after Thursday, so first Thursday is in the next week
4445
}
45-
const oFirstThursday = new UniversalDate(oFirstDayOfYear.getTime());
46+
const oFirstThursday = UniversalDate.getInstance(new Date(oFirstDayOfYear.getTime()), calendarType);
4647
oFirstThursday.setUTCDate(1 - iWeekDay + 4 + iAddDays);
4748

4849
iWeekNum = Math.round((oThursday.getTime() - oFirstThursday.getTime()) / 86400000 / 7) + 1;

‎packages/main/src/DayPicker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ class DayPicker extends CalendarPart implements ICalendarPicker {
311311

312312
if (dayOfTheWeek === DAYS_IN_WEEK - 1) { // 0-indexed so 6 is the last day of the week
313313
week.unshift({
314-
weekNum: calculateWeekNumber(getFirstDayOfWeek(), tempDate.toUTCJSDate(), tempDate.getYear(), getLocale(), localeData),
314+
weekNum: calculateWeekNumber(getFirstDayOfWeek(), tempDate.toUTCJSDate(), tempDate.getYear(), getLocale(), localeData, this._primaryCalendarType as CalendarType),
315315
isHidden: this.shouldHideWeekNumbers,
316316
});
317317
}

0 commit comments

Comments
 (0)
Please sign in to comment.