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(ui5-calendar): respect component level calendarType in week calculation #8971

Merged
merged 1 commit into from
May 21, 2024
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
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 @@ -312,7 +312,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