Skip to content

Commit

Permalink
fix(ui5-calendar): respect component level calendarType in week calcu…
Browse files Browse the repository at this point in the history
…lation (#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
  • Loading branch information
hinzzx committed May 23, 2024
1 parent 880b71c commit 8fb9067
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
13 changes: 7 additions & 6 deletions packages/localization/src/dates/calculateWeekNumber.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type Locale from "@ui5/webcomponents-base/dist/locale/Locale.js";
import CalendarType from "@ui5/webcomponents-base/dist/types/CalendarType.js";
import UniversalDate from "./UniversalDate.js";
import type LocaleData from "../LocaleData.js";
import type UI5Date from "./UI5Date.js";

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

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

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

const oFirstDayOfYear = new UniversalDate(oThursday.getTime());
const oFirstDayOfYear = UniversalDate.getInstance(new Date(oThursday.getTime()), calendarType);
oFirstDayOfYear.setUTCMonth(0, 1);
iWeekDay = oFirstDayOfYear.getUTCDay();
let iAddDays = 0;
if (iWeekDay > 4) {
iAddDays = 7; // first day of year is after Thursday, so first Thursday is in the next week
}
const oFirstThursday = new UniversalDate(oFirstDayOfYear.getTime());
const oFirstThursday = UniversalDate.getInstance(new Date(oFirstDayOfYear.getTime()), calendarType);
oFirstThursday.setUTCDate(1 - iWeekDay + 4 + iAddDays);

iWeekNum = Math.round((oThursday.getTime() - oFirstThursday.getTime()) / 86400000 / 7) + 1;
Expand Down
2 changes: 1 addition & 1 deletion packages/main/src/DayPicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ class DayPicker extends CalendarPart implements ICalendarPicker {

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

0 comments on commit 8fb9067

Please sign in to comment.