-
Notifications
You must be signed in to change notification settings - Fork 3
[ical4j 4.x] Migrate event builders part 3 #238
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
Merged
cketti
merged 5 commits into
ical4j-3to4
from
228-ical4j-4x-migrate-event-builders-part-3
Mar 17, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c60bb43
Migrate `OriginalInstanceTimeBuilder`
cketti 99cb867
Migrate `DurationBuilder`
cketti 9237ba1
Migrate `RecurrenceFieldsBuilder`
cketti 8e49351
Move functions to `AndroidRecurrenceMapper`
cketti 485d064
Add `Temporal.toLocalDate()` helper
cketti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
lib/src/main/kotlin/at/bitfire/synctools/mapping/calendar/builder/AndroidRecurrenceMapper.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /* | ||
| * This file is part of bitfireAT/synctools which is released under GPLv3. | ||
| * Copyright © All Contributors. See the LICENSE and AUTHOR files in the root directory for details. | ||
| * SPDX-License-Identifier: GPL-3.0-or-later | ||
| */ | ||
|
|
||
| package at.bitfire.synctools.mapping.calendar.builder | ||
|
|
||
| import at.bitfire.ical4android.util.DateUtils | ||
| import at.bitfire.synctools.mapping.calendar.builder.AndroidTemporalMapper.toTimestamp | ||
| import at.bitfire.synctools.mapping.calendar.builder.AndroidTemporalMapper.toZonedDateTime | ||
| import net.fortuna.ical4j.model.Property | ||
| import java.time.Instant | ||
| import java.time.LocalDate | ||
| import java.time.LocalDateTime | ||
| import java.time.ZoneOffset | ||
| import java.time.ZonedDateTime | ||
| import java.time.format.DateTimeFormatter | ||
| import java.time.temporal.Temporal | ||
| import java.util.Locale | ||
|
|
||
| object AndroidRecurrenceMapper { | ||
|
|
||
| private const val RECURRENCE_LIST_TZID_SEPARATOR = ';' | ||
| private const val RECURRENCE_LIST_VALUE_SEPARATOR = "," | ||
|
|
||
| /** | ||
| * Used to separate multiple RRULEs/EXRULEs in the RRULE/EXRULE storage field. | ||
| */ | ||
| private const val RECURRENCE_RULE_SEPARATOR = "\n" | ||
|
|
||
|
|
||
| /** | ||
| * Format multiple RRULEs/EXRULEs as string to be used with Android's calendar provider. | ||
| */ | ||
| fun androidRecurrenceRuleString(rules: List<Property>): String { | ||
| return rules.joinToString(RECURRENCE_RULE_SEPARATOR) { it.value } | ||
| } | ||
|
|
||
| /** | ||
| * Concatenates, if necessary, multiple RDATE/EXDATE lists and converts them to | ||
| * a formatted string which Android calendar provider can process. | ||
| * | ||
| * Android [expects this format](https://android.googlesource.com/platform/frameworks/opt/calendar/+/68b3632330e7a9a4f9813b7eb671dbfd78c25bcd/src/com/android/calendarcommon2/RecurrenceSet.java#138): | ||
| * `[TZID;]date1,date2,date3` where date is `yyyymmddThhmmss` (when | ||
| * TZID is given) or `yyyymmddThhmmssZ`. | ||
| * | ||
| * This method converts the values to the type of [startDate], if necessary: | ||
| * | ||
| * - DTSTART (DATE-TIME) and RDATE/EXDATE (DATE) → method converts RDATE/EXDATE to DATE-TIME with same time as DTSTART | ||
| * - DTSTART (DATE) and RDATE/EXDATE (DATE-TIME) → method converts RDATE/EXDATE to DATE (just drops time) | ||
| * | ||
| * @param dates list of `Temporal`s from RDATE or EXDATE properties | ||
| * @param startDate used to determine whether the event is an all-day event or not; also used to | ||
| * generate the date-time if the event is not all-day but the exception is | ||
| * | ||
| * @return formatted string for Android calendar provider | ||
| */ | ||
| fun androidRecurrenceDatesString(dates: List<Temporal>, startDate: Temporal): String { | ||
| /* rdate/exdate: DATE DATE_TIME | ||
| all-day store as ...T000000Z cut off time and store as ...T000000Z | ||
| event with time use time and zone from DTSTART store as ...ThhmmssZ | ||
| */ | ||
| val utcDateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'", Locale.ROOT) | ||
| val dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss", Locale.ROOT) | ||
| val convertedDates = mutableListOf<ZonedDateTime>() | ||
| val allDay = DateUtils.isDate(startDate) | ||
|
|
||
| // use time zone of first entry for the whole set; null for UTC | ||
| val zoneId = (dates.firstOrNull() as? ZonedDateTime)?.zone | ||
|
|
||
| for (date in dates) { | ||
| if (date is LocalDate) { | ||
| // RDATE/EXDATE is DATE | ||
| if (allDay) { | ||
| // DTSTART is DATE; DATE values have to be returned as <date>T000000Z for Android | ||
|
|
||
| convertedDates.add(date.atStartOfDay().atZone(ZoneOffset.UTC)) | ||
| } else { | ||
| // DTSTART is DATE-TIME; amend DATE-TIME with clock time from DTSTART | ||
| val zonedStartDate = startDate.toZonedDateTime() | ||
| val amendedDate = ZonedDateTime.of( | ||
| date, | ||
| zonedStartDate.toLocalTime(), | ||
| zonedStartDate.zone | ||
| ) | ||
|
|
||
| val convertedDate = if (zoneId != null) { | ||
| amendedDate.withZoneSameInstant(zoneId) | ||
| } else { | ||
| amendedDate.withZoneSameInstant(ZoneOffset.UTC) | ||
| } | ||
|
|
||
| convertedDates.add(convertedDate) | ||
| } | ||
|
|
||
| } else { | ||
| // RDATE/EXDATE is DATE-TIME | ||
| val convertedDate = if (allDay) { | ||
| // DTSTART is DATE | ||
| val localDate = if (date is LocalDateTime) { | ||
| date.toLocalDate() | ||
| } else { | ||
| Instant.ofEpochMilli(date.toTimestamp()).atZone(ZoneOffset.UTC).toLocalDate() | ||
| } | ||
| localDate.atStartOfDay().atZone(ZoneOffset.UTC) | ||
| } else { | ||
| // DTSTART is DATE-TIME | ||
| val instant = Instant.ofEpochMilli(date.toTimestamp()) | ||
| if (zoneId != null) { | ||
| instant.atZone(zoneId) | ||
| } else { | ||
| instant.atZone(ZoneOffset.UTC) | ||
| } | ||
| } | ||
| convertedDates.add(convertedDate) | ||
| } | ||
| } | ||
|
|
||
| // format expected by Android: [tzid;]value1,value2,... | ||
| return buildString { | ||
| if (zoneId != null) { | ||
| append(zoneId.id) | ||
| append(RECURRENCE_LIST_TZID_SEPARATOR) | ||
| } | ||
|
|
||
| val formatter = if (zoneId == null) utcDateFormatter else dateFormatter | ||
| convertedDates.joinTo(buffer = this, separator = RECURRENCE_LIST_VALUE_SEPARATOR) { | ||
| formatter.format(it) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.