react-weekly-planning 1.0.36 → 1.0.38

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.
package/README.md CHANGED
@@ -239,6 +239,20 @@ Props for the Calendar component.
239
239
 
240
240
  ## Additional Functions
241
241
 
242
+ ### `getCalendarDate`
243
+
244
+ - **Description**: Returns the current date according to the selected timezone or the local time.
245
+ - **Parameters**:
246
+ - `timeZone` (TimeZone, optional): The timezone to use for the date calculation.
247
+ - **Returns**: A `Date` object representing the current time in the specified timezone (or local time if omitted).
248
+
249
+ **Example**:
250
+ ```javascript
251
+ import { getCalendarDate } from "react-weekly-planning";
252
+ const now = getCalendarDate("Europe/Paris");
253
+ console.log(now); // Logs the current date in Paris
254
+ ```
255
+
242
256
  ### `updateCalendarDateWithOffset`
243
257
 
244
258
  - **Description**: Updates the calendar date based on the week offset.
package/dist/lib/utils.js CHANGED
@@ -30,7 +30,7 @@ export const endDateMilliseconds = endDate.getTime();
30
30
  export function getDayHourly(weekOffset, timeZone) {
31
31
  const dailyHours = [];
32
32
  let dayOffset = weekOffset;
33
- const resolvedCurrentDate = timeZone ? getDateObjectInTimeZone(timeZone) : new Date();
33
+ const resolvedCurrentDate = getCalendarDate(timeZone);
34
34
  const currentDayOfWeek = resolvedCurrentDate.getDay();
35
35
  const resolvedStartDate = new Date(resolvedCurrentDate);
36
36
  resolvedStartDate.setDate(resolvedStartDate.getDate() - currentDayOfWeek);
@@ -116,18 +116,13 @@ export function getWeekDays(jump, timeZone) {
116
116
  "Nov",
117
117
  "Dec",
118
118
  ];
119
- const currentDate = timeZone ? getDateObjectInTimeZone(timeZone) : new Date();
119
+ const currentDate = getCalendarDate(timeZone);
120
120
  const currentDayOfWeek = currentDate.getDay();
121
121
  let weekDays = [];
122
122
  for (let i = 0; i < 7; i++) {
123
- const day = timeZone ? getDateObjectInTimeZone(timeZone) : new Date();
123
+ const day = getCalendarDate(timeZone);
124
124
  const diff = i - currentDayOfWeek;
125
- if (currentDayOfWeek === 0) {
126
- day.setDate(currentDate.getDate() + diff + jump - 7);
127
- }
128
- else {
129
- day.setDate(currentDate.getDate() + diff + jump);
130
- }
125
+ day.setDate(currentDate.getDate() + diff + jump);
131
126
  const formattedDay = `${days[day.getDay()]}. ${day.getDate()}, ${month[day.getMonth()]} ${day.getFullYear()}`;
132
127
  weekDays.push({
133
128
  day: days[day.getDay()],
@@ -171,11 +166,19 @@ function updateSelectedDateForEcartSemaine(dateSelectionnee) {
171
166
  * @returns The week difference in days.
172
167
  */
173
168
  export function calculerEcartSemaine(dateSelectionnee, timeZone) {
174
- const dateActuelle = timeZone ? getDateObjectInTimeZone(timeZone) : new Date();
175
- const recupDate = new Date(dateSelectionnee);
176
- recupDate.setUTCDate(recupDate.getUTCDate() + 4 - (recupDate.getUTCDay() || 7));
177
- dateActuelle.setUTCDate(dateActuelle.getUTCDate() + 4 - (dateActuelle.getUTCDay() || 7));
178
- return Math.ceil((recupDate.getTime() - dateActuelle.getTime()) / 86400000);
169
+ const dateActuelle = getCalendarDate(timeZone);
170
+ // 1. Retrieve only the year, month, and day, and force it to midnight UTC.
171
+ // This eliminates any risk related to time zones and daylight saving time.
172
+ const utcSelected = Date.UTC(dateSelectionnee.getFullYear(), dateSelectionnee.getMonth(), dateSelectionnee.getDate());
173
+ const utcActuelle = Date.UTC(dateActuelle.getFullYear(), dateActuelle.getMonth(), dateActuelle.getDate());
174
+ const MS_PAR_JOUR = 86400000;
175
+ // 2. Resize each date to the Sunday of its corresponding week.
176
+ // getDay() returns a number from 0 (Sunday) to 6 (Saturday).
177
+ // By subtracting (day * ms_per_day), we arrive at Sunday at midnight.
178
+ const dimancheSelected = utcSelected - (dateSelectionnee.getDay() * MS_PAR_JOUR);
179
+ const dimancheActuelle = utcActuelle - (dateActuelle.getDay() * MS_PAR_JOUR);
180
+ const ecartJours = Math.round((dimancheSelected - dimancheActuelle) / MS_PAR_JOUR);
181
+ return ecartJours; // Retournera 0, 7, -7, 14, -14...
179
182
  }
180
183
  /**
181
184
  * Calculate the number of weeks since an arbitrary origin date (January 1, 2022).
@@ -216,10 +219,12 @@ export function compareWeekOffset(calendarDate, weekOffset, taskDate, timeZone)
216
219
  // if (taskDate.getDay() === 0 && calculerEcartSemaine(taskDate) === -7) {
217
220
  // return true;
218
221
  // }
222
+ const currentDate = getCalendarDate(timeZone);
223
+ const currentWeekOffset = calculerEcartSemaine(currentDate, timeZone);
219
224
  const localTaskDate = getArbitraryDateInTimeZone(taskDate, timeZone);
220
225
  // if (calendarDate)
221
226
  // return (calculerEcartSemaine(calendarDate) === calculerEcartSemaine(taskDate));
222
- const ecartTask = calculerEcartSemaine(taskDate, timeZone) + (localTaskDate.getDay() === 0 ? 7 : 0);
227
+ const ecartTask = calculerEcartSemaine(taskDate, timeZone);
223
228
  return weekOffset === ecartTask;
224
229
  }
225
230
  export const sumHoursByGroups = (groupId, tasks, weekOffset, calendarDate, timeZone) => {
@@ -353,6 +358,14 @@ export function getDateObjectInTimeZone(timeZone) {
353
358
  return new Date();
354
359
  }
355
360
  }
361
+ /**
362
+ * Returns the current date according to the selected timezone or the local time.
363
+ * @param timeZone - The optional timezone.
364
+ * @returns The current date.
365
+ */
366
+ export function getCalendarDate(timeZone) {
367
+ return timeZone ? getDateObjectInTimeZone(timeZone) : new Date();
368
+ }
356
369
  export function getArbitraryDateInTimeZone(date, timeZone) {
357
370
  if (!timeZone)
358
371
  return date;
@@ -437,7 +450,7 @@ export function getHoursByGroup(tasks, groupId, weekOffset, timeZone) {
437
450
  return sum;
438
451
  }
439
452
  export function getTaskProgression(task, timeZone) {
440
- const now = timeZone ? getDateObjectInTimeZone(timeZone) : new Date();
453
+ const now = getCalendarDate(timeZone);
441
454
  if (task.taskStart >= now.getTime())
442
455
  return 0;
443
456
  if (now.getTime() >= task.taskEnd)
@@ -55,6 +55,12 @@ export declare const deleteTasksSaved: () => void;
55
55
  export declare function getWeeksInMonth(year: number, month: number): number;
56
56
  export declare function getMonthNumber(monthName: string): number;
57
57
  export declare function getDateObjectInTimeZone(timeZone: string): Date;
58
+ /**
59
+ * Returns the current date according to the selected timezone or the local time.
60
+ * @param timeZone - The optional timezone.
61
+ * @returns The current date.
62
+ */
63
+ export declare function getCalendarDate(timeZone?: TimeZone): Date;
58
64
  export declare function getArbitraryDateInTimeZone(date: Date, timeZone?: string): Date;
59
65
  export declare function recurringTasks(allTasks: TasksType, task: TaskFeildsType, recurrenceType: "daily" | "weekly" | "monthly", occurrences: number, timeZone?: TimeZone): TaskFeildsType[];
60
66
  export declare function getHoursByday(tasks: TaskFeildsType[], dayIndex: 0 | 1 | 2 | 3 | 4 | 5 | 6, weekOffset?: number, timeZone?: TimeZone): number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-weekly-planning",
3
- "version": "1.0.36",
3
+ "version": "1.0.38",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/types/index.d.ts",
6
6
  "exports": {