react-weekly-planning 1.0.36 → 1.0.37

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,11 +116,11 @@ 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
125
  if (currentDayOfWeek === 0) {
126
126
  day.setDate(currentDate.getDate() + diff + jump - 7);
@@ -171,11 +171,19 @@ function updateSelectedDateForEcartSemaine(dateSelectionnee) {
171
171
  * @returns The week difference in days.
172
172
  */
173
173
  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);
174
+ const dateActuelle = getCalendarDate(timeZone);
175
+ // 1. Retrieve only the year, month, and day, and force it to midnight UTC.
176
+ // This eliminates any risk related to time zones and daylight saving time.
177
+ const utcSelected = Date.UTC(dateSelectionnee.getFullYear(), dateSelectionnee.getMonth(), dateSelectionnee.getDate());
178
+ const utcActuelle = Date.UTC(dateActuelle.getFullYear(), dateActuelle.getMonth(), dateActuelle.getDate());
179
+ const MS_PAR_JOUR = 86400000;
180
+ // 2. Resize each date to the Sunday of its corresponding week.
181
+ // getDay() returns a number from 0 (Sunday) to 6 (Saturday).
182
+ // By subtracting (day * ms_per_day), we arrive at Sunday at midnight.
183
+ const dimancheSelected = utcSelected - (dateSelectionnee.getDay() * MS_PAR_JOUR);
184
+ const dimancheActuelle = utcActuelle - (dateActuelle.getDay() * MS_PAR_JOUR);
185
+ const ecartJours = Math.round((dimancheSelected - dimancheActuelle) / MS_PAR_JOUR);
186
+ return ecartJours; // Retournera 0, 7, -7, 14, -14...
179
187
  }
180
188
  /**
181
189
  * Calculate the number of weeks since an arbitrary origin date (January 1, 2022).
@@ -216,10 +224,12 @@ export function compareWeekOffset(calendarDate, weekOffset, taskDate, timeZone)
216
224
  // if (taskDate.getDay() === 0 && calculerEcartSemaine(taskDate) === -7) {
217
225
  // return true;
218
226
  // }
227
+ const currentDate = getCalendarDate(timeZone);
228
+ const currentWeekOffset = calculerEcartSemaine(currentDate, timeZone);
219
229
  const localTaskDate = getArbitraryDateInTimeZone(taskDate, timeZone);
220
230
  // if (calendarDate)
221
231
  // return (calculerEcartSemaine(calendarDate) === calculerEcartSemaine(taskDate));
222
- const ecartTask = calculerEcartSemaine(taskDate, timeZone) + (localTaskDate.getDay() === 0 ? 7 : 0);
232
+ const ecartTask = calculerEcartSemaine(taskDate, timeZone);
223
233
  return weekOffset === ecartTask;
224
234
  }
225
235
  export const sumHoursByGroups = (groupId, tasks, weekOffset, calendarDate, timeZone) => {
@@ -353,6 +363,14 @@ export function getDateObjectInTimeZone(timeZone) {
353
363
  return new Date();
354
364
  }
355
365
  }
366
+ /**
367
+ * Returns the current date according to the selected timezone or the local time.
368
+ * @param timeZone - The optional timezone.
369
+ * @returns The current date.
370
+ */
371
+ export function getCalendarDate(timeZone) {
372
+ return timeZone ? getDateObjectInTimeZone(timeZone) : new Date();
373
+ }
356
374
  export function getArbitraryDateInTimeZone(date, timeZone) {
357
375
  if (!timeZone)
358
376
  return date;
@@ -437,7 +455,7 @@ export function getHoursByGroup(tasks, groupId, weekOffset, timeZone) {
437
455
  return sum;
438
456
  }
439
457
  export function getTaskProgression(task, timeZone) {
440
- const now = timeZone ? getDateObjectInTimeZone(timeZone) : new Date();
458
+ const now = getCalendarDate(timeZone);
441
459
  if (task.taskStart >= now.getTime())
442
460
  return 0;
443
461
  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.37",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/types/index.d.ts",
6
6
  "exports": {