msramalho / SigTools

📆 Sigarra Tools | An extension that makes the information system of the University of Porto slightly better.
https://chrome.google.com/webstore/detail/sigarra-to-calendar/piefgbacnljenipiifjopkfifeljjkme
Apache License 2.0
37 stars 0 forks source link

Custom period ranges for recurring events does not affect the starting date #110

Closed fabiodrg closed 2 years ago

fabiodrg commented 2 years ago

image

Suppose an event on 1st March (Tuesday) that recurs weekly. If the user selects a later starting date, e.g. 23rd March (Wednesday), the starting date for the event should be shifted to 29th March (the first Tuesday from the selected period). Regarding the ending date, that works fine as the user input is directly used for the recurring rule.

fabiodrg commented 2 years ago

Initial experiment with Luxon

const luxon = require("luxon");

const d1 = luxon.DateTime.local(2022, 03, 01, 15, 00);

const from1 = luxon.DateTime.local(2022, 02, 02);
const from2 = luxon.DateTime.local(2022, 02, 27);
const from3 = luxon.DateTime.local(2022, 03, 21);
const from4 = luxon.DateTime.local(2022, 03, 23);

const shiftDate = ((from, d) => {
  if (from > d) {
    const weeksDiff = from.diff(d, "weeks").toObject().weeks;
    return d.plus({weeks: Math.floor(weeksDiff) + 1});
  }

  return d;
});

console.log(shiftDate(from1, d1).toString());
console.log(shiftDate(from2, d1).toString());
console.log(shiftDate(from3, d1).toString());
console.log(shiftDate(from4, d1).toString());
2022-03-01T15:00:00.000+00:00
2022-03-01T15:00:00.000+00:00
2022-03-22T15:00:00.000+00:00
2022-03-29T15:00:00.000+01:00