FIX: recurring was not working for some cases (eg: hours and unit > 1) (#11657)

This commit is contained in:
Joffrey JAFFEUX
2021-01-07 12:21:51 +01:00
committed by GitHub
parent 0c451d14d9
commit 49f4c75080
4 changed files with 37 additions and 15 deletions

View File

@ -10,7 +10,7 @@ const { getProperties } = Ember;
- isDST() allows to know if a date in a specified timezone is currently under DST
- datetimeWithZone(timezone) returns a new moment object with timezone applied
- datetime returns the moment object
- repetitionsBetweenDates(duration, date) return the number of repertitions of
- unitRepetitionsBetweenDates(duration, date) return the number of repertitions of
duration between two dates, eg for duration: "1.weeks", "2.months"...
*/
export default class DateWithZoneHelper {
@ -35,11 +35,18 @@ export default class DateWithZoneHelper {
return this.datetime.tz(this.localTimezone).isDST();
}
repetitionsBetweenDates(duration, date) {
unitRepetitionsBetweenDates(duration, date) {
const [count, unit] = duration.split(".");
const diff = this.datetime.diff(date, unit);
const repetitions = diff / parseInt(count, 10);
return Math.abs((Math.round(repetitions * 10) / 10).toFixed(1));
// get the diff in the specified units with decimals
const diff = Math.abs(this.datetime.diff(date, unit, true));
// get integer count of duration in diff, eg: 4 hours diff is 2 for 2.hours duration
const integer = Math.trunc(diff / count);
// get fractional to define if we have to add one "duration"
const fractional = (diff / count) % 1;
return (
integer * parseInt(count, 10) + (fractional > 0 ? parseInt(count, 10) : 0)
);
}
add(count, unit) {

View File

@ -48,14 +48,14 @@ export default class LocalDateBuilder {
});
if (this.recurring && moment().isAfter(localDate.datetime)) {
const [count, type] = this.recurring.split(".");
const type = this.recurring.split(".")[1];
const repetitionsForType = localDate.repetitionsBetweenDates(
const repetitionsForType = localDate.unitRepetitionsBetweenDates(
this.recurring,
moment.tz(this.localTimezone)
);
localDate = localDate.add(repetitionsForType + parseInt(count, 10), type);
localDate = localDate.add(repetitionsForType, type);
}
const previews = this._generatePreviews(localDate, displayedTimezone);