@siemens/element-ng 47.12.1 → 47.12.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -33,8 +33,9 @@ export declare const getDayStrings: (locale: string, weekStart?: WeekStart, form
33
33
  */
34
34
  export declare const getFirstDayInMonth: (year: number, month: number) => Date;
35
35
  /**
36
- * Gets the week number of the specified date.
37
- * Week number according to the ISO-8601 standard, weeks starting on Monday.
36
+ * Returns the ISO-8601 week number for the user's week containing the specified date.
37
+ * ISO-8601 weeks always start on Monday. When the user's week starts on Sunday or Saturday,
38
+ * the date is adjusted to find the corresponding ISO week (based on the Thursday rule).
38
39
  * The first week of a year is the week that contains the first Thursday of the year (='First 4-day week').
39
40
  * The highest week number in a year is either 52 or 53.
40
41
  *
@@ -91,8 +91,9 @@ const getDayStrings = (locale, weekStart = 'monday', format = 'short') => {
91
91
  */
92
92
  const getFirstDayInMonth = (year, month) => new Date(year, month - 1, 1);
93
93
  /**
94
- * Gets the week number of the specified date.
95
- * Week number according to the ISO-8601 standard, weeks starting on Monday.
94
+ * Returns the ISO-8601 week number for the user's week containing the specified date.
95
+ * ISO-8601 weeks always start on Monday. When the user's week starts on Sunday or Saturday,
96
+ * the date is adjusted to find the corresponding ISO week (based on the Thursday rule).
96
97
  * The first week of a year is the week that contains the first Thursday of the year (='First 4-day week').
97
98
  * The highest week number in a year is either 52 or 53.
98
99
  *
@@ -101,16 +102,21 @@ const getFirstDayInMonth = (year, month) => new Date(year, month - 1, 1);
101
102
  * @returns The number of the Week
102
103
  */
103
104
  const getWeekOfYear = (date, weekStart) => {
104
- // Algorithm rewritten from C# example given at http://en.wikipedia.org/wiki/Talk:ISO_week_date
105
- const dayOfWeek = getWeekDayOffset(date, weekStart) + 1;
106
105
  const nearestThu = new Date(date);
107
- nearestThu.setDate(date.getDate() + (UNITS.thursday - dayOfWeek)); // get nearest Thursday (-3..+3 days)
106
+ nearestThu.setHours(0, 0, 0, 0);
107
+ const offset = weekStart === 'monday' ? 0 : weekStart === 'sunday' ? 1 : 2;
108
+ nearestThu.setDate(nearestThu.getDate() + offset);
109
+ adjustToThursday(nearestThu);
108
110
  const year = nearestThu.getFullYear();
109
- const janfirst = getFirstDayInMonth(year, 1);
110
- const days = Math.floor((nearestThu - janfirst) / UNITS.millisecondsPerDay);
111
+ const weekOne = new Date(year, 0, 4);
112
+ adjustToThursday(weekOne);
113
+ const days = Math.round((nearestThu.getTime() - weekOne.getTime()) / UNITS.millisecondsPerDay);
111
114
  const week = 1 + Math.floor(days / UNITS.daysPerWeek); // Count of Thursdays
112
115
  return week;
113
116
  };
117
+ const adjustToThursday = (date) => {
118
+ date.setDate(date.getDate() + 3 - ((date.getDay() + 6) % 7));
119
+ };
114
120
  const getWeekDayOffset = (date, weekStart) => {
115
121
  const offset = WEEK_START_OFFSET[weekStart ?? 'monday'];
116
122
  return (date.getDay() + 6 - offset) % 7;