Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2901: Fix recurring dates yet again #2902

Merged
merged 7 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions shared/api/models/DateModel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DateTime, Duration } from 'luxon'
import { RRule as RRuleType } from 'rrule'
import { RRule as RRuleType, rrulestr } from 'rrule'

const MAX_RECURRENCE_YEARS = 5

Expand Down Expand Up @@ -137,14 +137,12 @@ class DateModel {

private getRecurrenceRuleInLocalTime(recurrenceRule: RRuleType): RRuleType {
const startDate = recurrenceRule.options.dtstart
const offsetStartDate = DateTime.fromJSDate(startDate).minus({ minutes: startDate.getTimezoneOffset() }).toJSDate()
return new RRuleType({
freq: recurrenceRule.options.freq,
byweekday: recurrenceRule.options.byweekday,
interval: recurrenceRule.options.interval,
until: recurrenceRule.options.until,
dtstart: offsetStartDate,
})
const offsetStartDate = DateTime.fromJSDate(startDate)
.minus({ minutes: startDate.getTimezoneOffset() })
.toUTC()
.toFormat("yyyyMMdd'T'HHmmss")
const regexForFindingDate = /\d{8}T\d{6}/
return rrulestr(recurrenceRule.toString().replace(regexForFindingDate, offsetStartDate))
LeandraH marked this conversation as resolved.
Show resolved Hide resolved
}

isEqual(other: DateModel): boolean {
Expand Down
169 changes: 169 additions & 0 deletions shared/api/models/__tests__/DateModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,5 +400,174 @@ describe('DateModel', () => {
}),
])
})

it('should correctly handle events recurring every second week of the month', () => {
jest.useFakeTimers({ now: new Date('2024-07-28T15:23:57.443+02:00') })
const recurrenceRule = rrulestr('DTSTART:20240620T083000\nRRULE:FREQ=MONTHLY;BYDAY=+2MO')
const date = new DateModel({
startDate: DateTime.fromISO('2024-06-20T10:30:00.000+02:00'),
endDate: DateTime.fromISO('2024-06-20T12:00:00.000+02:00'),
allDay: false,
recurrenceRule,
})

expect(date.recurrences(3)).toEqual([
new DateModel({
allDay: false,
recurrenceRule,
startDate: DateTime.fromISO('2024-08-12T10:30:00.000+02:00'),
endDate: DateTime.fromISO('2024-08-12T12:00:00.000+02:00'),
offset: 120,
}),
new DateModel({
allDay: false,
recurrenceRule,
startDate: DateTime.fromISO('2024-09-09T10:30:00.000+02:00'),
endDate: DateTime.fromISO('2024-09-09T12:00:00.000+02:00'),
offset: 120,
}),
new DateModel({
allDay: false,
recurrenceRule,
startDate: DateTime.fromISO('2024-10-14T10:30:00.000+02:00'),
endDate: DateTime.fromISO('2024-10-14T12:00:00.000+02:00'),
offset: 120,
}),
])
})

it('should correctly handle events recurring every last week of the month', () => {
jest.useFakeTimers({ now: new Date('2024-08-28T15:23:57.443+02:00') })
const recurrenceRule = rrulestr('DTSTART:20240827T220000\nRRULE:FREQ=MONTHLY;BYDAY=-1WE')
const date = new DateModel({
startDate: DateTime.fromISO('2024-08-28T00:00:00.000+02:00'),
endDate: DateTime.fromISO('2024-08-28T23:59:00.000+02:00'),
allDay: true,
recurrenceRule,
})

expect(date.recurrences(3)).toEqual([
new DateModel({
allDay: true,
recurrenceRule,
startDate: DateTime.fromISO('2024-08-28T00:00:00.000+02:00'),
endDate: DateTime.fromISO('2024-08-28T23:59:00.000+02:00'),
offset: 120,
}),
new DateModel({
allDay: true,
recurrenceRule,
startDate: DateTime.fromISO('2024-09-25T00:00:00.000+02:00'),
endDate: DateTime.fromISO('2024-09-25T23:59:00.000+02:00'),
offset: 120,
}),
new DateModel({
allDay: true,
recurrenceRule,
startDate: DateTime.fromISO('2024-10-30T00:00:00.000+01:00'),
endDate: DateTime.fromISO('2024-10-30T23:59:00.000+01:00'),
offset: 120,
}),
])
})

it('should correctly handle events recurring every third week of every second month', () => {
jest.useFakeTimers({ now: new Date('2024-08-28T15:23:57.443+02:00') })
const recurrenceRule = rrulestr('DTSTART:20240620T083000\nRRULE:FREQ=MONTHLY;INTERVAL=2;BYDAY=+3TH')
const date = new DateModel({
startDate: DateTime.fromISO('2024-06-20T10:30:00.000+02:00'),
endDate: DateTime.fromISO('2024-06-20T12:00:00.000+02:00'),
allDay: false,
recurrenceRule,
})

expect(date.recurrences(3)).toEqual([
new DateModel({
allDay: false,
recurrenceRule,
startDate: DateTime.fromISO('2024-10-17T10:30:00.000+02:00'),
endDate: DateTime.fromISO('2024-10-17T12:00:00.000+02:00'),
offset: 120,
}),
new DateModel({
allDay: false,
recurrenceRule,
startDate: DateTime.fromISO('2024-12-19T10:30:00.000+01:00'),
endDate: DateTime.fromISO('2024-12-19T12:00:00.000+01:00'),
offset: 120,
}),
new DateModel({
allDay: false,
recurrenceRule,
startDate: DateTime.fromISO('2025-02-20T10:30:00.000+01:00'),
endDate: DateTime.fromISO('2025-02-20T12:00:00.000+01:00'),
offset: 120,
}),
])
})

it('should correctly handle events repeating annually', () => {
jest.useFakeTimers({ now: new Date('2024-08-28T15:23:57.443+02:00') })
const recurrenceRule = rrulestr('DTSTART:20241205T230000\nRRULE:FREQ=YEARLY')
const date = new DateModel({
startDate: DateTime.fromISO('2024-12-06T00:00:00.000+01:00'),
endDate: DateTime.fromISO('2024-12-06T23:59:00.000+01:00'),
allDay: true,
recurrenceRule,
})

expect(date.recurrences(3)).toEqual([
new DateModel({
allDay: true,
startDate: DateTime.fromISO('2024-12-06T00:00:00.000+01:00'),
endDate: DateTime.fromISO('2024-12-06T23:59:00.000+01:00'),
recurrenceRule,
}),
new DateModel({
allDay: true,
startDate: DateTime.fromISO('2025-12-06T00:00:00.000+01:00'),
endDate: DateTime.fromISO('2025-12-06T23:59:00.000+01:00'),
recurrenceRule,
}),
new DateModel({
allDay: true,
startDate: DateTime.fromISO('2026-12-06T00:00:00.000+01:00'),
endDate: DateTime.fromISO('2026-12-06T23:59:00.000+01:00'),
recurrenceRule,
}),
])
})

it('should correctly handle events repeating every 2 years', () => {
jest.useFakeTimers({ now: new Date('2024-08-28T15:23:57.443+02:00') })
const recurrenceRule = rrulestr('DTSTART:20241205T230000\nRRULE:FREQ=YEARLY;INTERVAL=2')
const date = new DateModel({
startDate: DateTime.fromISO('2024-12-06T00:00:00.000+01:00'),
endDate: DateTime.fromISO('2024-12-06T23:59:00.000+01:00'),
allDay: true,
recurrenceRule,
})

expect(date.recurrences(3)).toEqual([
new DateModel({
allDay: true,
startDate: DateTime.fromISO('2024-12-06T00:00:00.000+01:00'),
endDate: DateTime.fromISO('2024-12-06T23:59:00.000+01:00'),
recurrenceRule,
}),
new DateModel({
allDay: true,
startDate: DateTime.fromISO('2026-12-06T00:00:00.000+01:00'),
endDate: DateTime.fromISO('2026-12-06T23:59:00.000+01:00'),
recurrenceRule,
}),
new DateModel({
allDay: true,
startDate: DateTime.fromISO('2028-12-06T00:00:00.000+01:00'),
endDate: DateTime.fromISO('2028-12-06T23:59:00.000+01:00'),
recurrenceRule,
}),
])
})
})
})
Loading