Skip to content

Commit 4365e84

Browse files
committed
Provide specific tzRegistry in various contexts
1 parent 3dd55d2 commit 4365e84

File tree

7 files changed

+49
-43
lines changed

7 files changed

+49
-43
lines changed

lib/src/androidTest/kotlin/at/bitfire/ical4android/util/AndroidTimeUtilsTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class AndroidTimeUtilsTest {
3232
val tzToronto: TimeZone = tzRegistry.getTimeZone("America/Toronto")!!
3333

3434
val tzCustom by lazy {
35-
val builder = CalendarBuilder()
35+
val builder = CalendarBuilder(tzRegistry)
3636
val cal = builder.build(StringReader("BEGIN:VCALENDAR\n" +
3737
"BEGIN:VTIMEZONE\n" +
3838
"TZID:CustomTime\n" +
@@ -280,7 +280,7 @@ class AndroidTimeUtilsTest {
280280
@Test
281281
fun testAndroidStringToRecurrenceSets_UtcTimes() {
282282
// list of UTC times
283-
val exDate = AndroidTimeUtils.androidStringToRecurrenceSet(tzRegistry, "20150101T103010Z,20150702T103020Z", false) { ExDate(it) }
283+
val exDate = AndroidTimeUtils.androidStringToRecurrenceSet("20150101T103010Z,20150702T103020Z", tzRegistry, false) { ExDate(it) }
284284
assertNull(exDate.timeZone)
285285
val exDates = exDate.dates
286286
assertEquals(Value.DATE_TIME, exDates.type)
@@ -293,7 +293,7 @@ class AndroidTimeUtilsTest {
293293
@Test
294294
fun testAndroidStringToRecurrenceSets_ZonedTimes() {
295295
// list of time zone times
296-
val exDate = AndroidTimeUtils.androidStringToRecurrenceSet(tzRegistry, "${tzToronto.id};20150103T113030,20150704T113040",false) { ExDate(it) }
296+
val exDate = AndroidTimeUtils.androidStringToRecurrenceSet("${tzToronto.id};20150103T113030,20150704T113040", tzRegistry,false) { ExDate(it) }
297297
assertEquals(tzToronto, exDate.timeZone)
298298
assertEquals(tzToronto.id, (exDate.getParameter(Parameter.TZID) as TzId).value)
299299
val exDates = exDate.dates
@@ -307,7 +307,7 @@ class AndroidTimeUtilsTest {
307307
@Test
308308
fun testAndroidStringToRecurrenceSets_Dates() {
309309
// list of dates
310-
val exDate = AndroidTimeUtils.androidStringToRecurrenceSet(tzRegistry, "20150101T103010Z,20150702T103020Z", true) { ExDate(it) }
310+
val exDate = AndroidTimeUtils.androidStringToRecurrenceSet("20150101T103010Z,20150702T103020Z", tzRegistry, true) { ExDate(it) }
311311
val exDates = exDate.dates
312312
assertEquals(Value.DATE, exDates.type)
313313
assertEquals(2, exDates.size)
@@ -317,7 +317,7 @@ class AndroidTimeUtilsTest {
317317

318318
@Test
319319
fun testAndroidStringToRecurrenceSets_Exclude() {
320-
val exDate = AndroidTimeUtils.androidStringToRecurrenceSet(tzRegistry, "${tzToronto.id};20150103T113030",false, 1420302630000L) { ExDate(it) }
320+
val exDate = AndroidTimeUtils.androidStringToRecurrenceSet("${tzToronto.id};20150103T113030", tzRegistry,false, 1420302630000L) { ExDate(it) }
321321
assertEquals(0, exDate.dates.size)
322322
}
323323

lib/src/androidTest/kotlin/at/bitfire/ical4android/util/TimeApiExtensionsTest.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import at.bitfire.ical4android.util.TimeApiExtensions.toZonedDateTime
1818
import net.fortuna.ical4j.model.Date
1919
import net.fortuna.ical4j.model.DateTime
2020
import net.fortuna.ical4j.model.TimeZone
21+
import net.fortuna.ical4j.model.TimeZoneRegistryFactory
2122
import net.fortuna.ical4j.util.TimeZones
2223
import org.junit.Assert.assertEquals
2324
import org.junit.Assert.assertTrue
@@ -34,7 +35,8 @@ import java.time.ZonedDateTime
3435

3536
class TimeApiExtensionsTest {
3637

37-
val tzBerlin: TimeZone = DateUtils.ical4jTimeZone("Europe/Berlin")!!
38+
val tzRegistry = TimeZoneRegistryFactory.getInstance().createRegistry()!!
39+
val tzBerlin = tzRegistry.getTimeZone("Europe/Berlin")!!
3840

3941

4042
@Test
@@ -155,18 +157,17 @@ class TimeApiExtensionsTest {
155157

156158
@Test
157159
fun testZonedDateTime_toIcal4jDateTime_NotUtc() {
158-
val tzBerlin = DateUtils.ical4jTimeZone("Europe/Berlin")
159160
assertEquals(
160161
DateTime("20200705T010203", tzBerlin),
161-
ZonedDateTime.of(2020, 7, 5, 1, 2, 3, 0, ZoneId.of("Europe/Berlin")).toIcal4jDateTime()
162+
ZonedDateTime.of(2020, 7, 5, 1, 2, 3, 0, ZoneId.of("Europe/Berlin")).toIcal4jDateTime(tzRegistry)
162163
)
163164
}
164165

165166
@Test
166167
fun testZonedDateTime_toIcal4jDateTime_Utc() {
167168
assertEquals(
168169
DateTime("20200705T010203Z"),
169-
ZonedDateTime.of(2020, 7, 5, 1, 2, 3, 0, ZoneOffset.UTC).toIcal4jDateTime()
170+
ZonedDateTime.of(2020, 7, 5, 1, 2, 3, 0, ZoneOffset.UTC).toIcal4jDateTime(tzRegistry)
170171
)
171172
}
172173

lib/src/main/kotlin/at/bitfire/ical4android/AndroidEvent.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ class AndroidEvent(
316316
event.rRules += RRule(rule)
317317
}
318318
row.getAsString(Events.RDATE)?.let { datesStr ->
319-
val rDate = AndroidTimeUtils.androidStringToRecurrenceSet(tzRegistry, datesStr, allDay, tsStart) { RDate(it) }
319+
val rDate = AndroidTimeUtils.androidStringToRecurrenceSet(datesStr, tzRegistry, allDay, tsStart) { RDate(it) }
320320
event.rDates += rDate
321321
}
322322

@@ -325,7 +325,7 @@ class AndroidEvent(
325325
event.exRules += ExRule(null, rule)
326326
}
327327
row.getAsString(Events.EXDATE)?.let { datesStr ->
328-
val exDate = AndroidTimeUtils.androidStringToRecurrenceSet(tzRegistry, datesStr, allDay) { ExDate(it) }
328+
val exDate = AndroidTimeUtils.androidStringToRecurrenceSet(datesStr, tzRegistry, allDay) { ExDate(it) }
329329
event.exDates += exDate
330330
}
331331
} catch (e: Exception) {
@@ -672,7 +672,7 @@ class AndroidEvent(
672672
dtStartDate.toLocalTime(),
673673
dtStartDate.requireZoneId()
674674
)
675-
recurrenceDate = zonedTime.toIcal4jDateTime()
675+
recurrenceDate = zonedTime.toIcal4jDateTime(tzRegistry)
676676
}
677677
exBuilder .withValue(Events.ORIGINAL_ALL_DAY, if (DateUtils.isDate(event.dtStart)) 1 else 0)
678678
.withValue(Events.ORIGINAL_INSTANCE_TIME, recurrenceDate.time)
@@ -926,7 +926,7 @@ class AndroidEvent(
926926
} else {
927927
val zonedStartTime = (dtStart.date as DateTime).toZonedDateTime()
928928
val calcEnd = zonedStartTime + duration
929-
val calcDtEnd = DtEnd(calcEnd.toIcal4jDateTime())
929+
val calcDtEnd = DtEnd(calcEnd.toIcal4jDateTime(tzRegistry))
930930
calcDtEnd.timeZone = dtStart.timeZone
931931
dtEnd = calcDtEnd
932932
}

lib/src/main/kotlin/at/bitfire/ical4android/DmfsTask.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,10 @@ abstract class DmfsTask(
255255
}
256256

257257
values.getAsString(Tasks.RDATE)?.let {
258-
task.rDates += AndroidTimeUtils.androidStringToRecurrenceSet(tzRegistry, it, allDay) { dates -> RDate(dates) }
258+
task.rDates += AndroidTimeUtils.androidStringToRecurrenceSet(it, tzRegistry, allDay) { dates -> RDate(dates) }
259259
}
260260
values.getAsString(Tasks.EXDATE)?.let {
261-
task.exDates += AndroidTimeUtils.androidStringToRecurrenceSet(tzRegistry, it, allDay) { dates -> ExDate(dates) }
261+
task.exDates += AndroidTimeUtils.androidStringToRecurrenceSet(it, tzRegistry, allDay) { dates -> ExDate(dates) }
262262
}
263263

264264
values.getAsString(Tasks.RRULE)?.let { task.rRule = RRule(it) }

lib/src/main/kotlin/at/bitfire/ical4android/util/AndroidTimeUtils.kt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ object AndroidTimeUtils {
9090

9191
// periods (RDate only)
9292
val periods = (dateList as? RDate)?.periods
93-
if (periods != null && periods.size > 0 && !periods.isUtc) {
93+
if (periods != null && periods.isNotEmpty() && !periods.isUtc) {
9494
val tzID = DateUtils.findAndroidTimezoneID(periods.timeZone?.id)
9595

9696
// Setting the time zone won't work until resolved in ical4j (https://github.com/ical4j/ical4j/discussions/568)
@@ -102,7 +102,7 @@ object AndroidTimeUtils {
102102

103103
// date-times (RDate and ExDate)
104104
val dates = dateList.dates
105-
if (dates != null && dates.size > 0) {
105+
if (dates != null && dates.isNotEmpty()) {
106106
if (dates.type == Value.DATE_TIME && !dates.isUtc) {
107107
val tzID = DateUtils.findAndroidTimezoneID(dates.timeZone?.id)
108108
dateList.timeZone = tzRegistry.getTimeZone(tzID)
@@ -206,7 +206,7 @@ object AndroidTimeUtils {
206206
// DTSTART is DATE-TIME; amend DATE-TIME with clock time from dtStart
207207
dateListProp.dates.mapTo(strDates) { date ->
208208
// take time (including time zone) from dtStart and date from date
209-
val dtStartTime = (dtStart as DateTime).toZonedDateTime()
209+
val dtStartTime = dtStart.toZonedDateTime()
210210
val localDate = date.toLocalDate()
211211
val dtStartTimeUtc = ZonedDateTime.of(
212212
localDate,
@@ -233,19 +233,20 @@ object AndroidTimeUtils {
233233
* Takes a formatted string as provided by the Android calendar provider and returns a DateListProperty
234234
* constructed from these values.
235235
*
236-
* @param dbStr formatted string from Android calendar provider (RDATE/EXDATE field)
237-
* expected format: "[TZID;]date1,date2,date3" where date is "yyyymmddThhmmss[Z]"
238-
* @param allDay true: list will contain DATE values; false: list will contain DATE_TIME values
239-
* @param exclude this time stamp won't be added to the [DateListProperty]
240-
* @param generator generates the [DateListProperty]; must call the constructor with the one argument of type [DateList]
236+
* @param dbStr formatted string from Android calendar provider (RDATE/EXDATE field)
237+
* expected format: "[TZID;]date1,date2,date3" where date is "yyyymmddThhmmss[Z]"
238+
* @param tzRegistry time zone registry
239+
* @param allDay true: list will contain DATE values; false: list will contain DATE_TIME values
240+
* @param exclude this time stamp won't be added to the [DateListProperty]
241+
* @param generator generates the [DateListProperty]; must call the constructor with the one argument of type [DateList]
241242
*
242-
* @return instance of "type" containing the parsed dates/times from the string
243+
* @return instance of "type" containing the parsed dates/times from the string
243244
*
244245
* @throws ParseException when the string cannot be parsed
245246
*/
246247
fun<T: DateListProperty> androidStringToRecurrenceSet(
247-
tzRegistry: TimeZoneRegistry,
248248
dbStr: String,
249+
tzRegistry: TimeZoneRegistry,
249250
allDay: Boolean,
250251
exclude: Long? = null,
251252
generator: (DateList) -> T

lib/src/main/kotlin/at/bitfire/ical4android/util/DateUtils.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ object DateUtils {
3232
* Global ical4j time zone registry used for event/task processing. Do not
3333
* modify this registry or its entries!
3434
*/
35+
@Deprecated("For every context, it's own TimeZoneRegistry should be used")
3536
private val tzRegistry = TimeZoneRegistryFactory.getInstance().createRegistry()
3637

3738

@@ -46,7 +47,7 @@ object DateUtils {
4647
* 2. Find partial matches (case-sensitive) in both directions, so both "Vienna"
4748
* and "MyClient: Europe/Vienna" will return "Europe/Vienna". This shouln't be
4849
* case-insensitive, because that would for instance return "EST" for "Westeuropäische Sommerzeit".
49-
* 3. If nothing can be found or [tzId] is `null`, return the system default time zone.
50+
* 3. If nothing can be found or [tzID] is `null`, return the system default time zone.
5051
*
5152
* @param tzID time zone ID to be converted into Android time zone ID
5253
*
@@ -87,7 +88,7 @@ object DateUtils {
8788
try {
8889
val zone = ZoneId.of(id)
8990
zone
90-
} catch (e: Exception) {
91+
} catch (_: Exception) {
9192
null
9293
}
9394
}
@@ -127,8 +128,8 @@ object DateUtils {
127128
*
128129
* @return parsed [VTimeZone], or `null` when the timezone definition can't be parsed
129130
*/
130-
fun parseVTimeZone(timezoneDef: String): VTimeZone? {
131-
val builder = CalendarBuilder(tzRegistry)
131+
fun parseVTimeZone(timezoneDef: String ): VTimeZone? {
132+
val builder = CalendarBuilder()
132133
try {
133134
val cal = builder.build(StringReader(timezoneDef))
134135
return cal.getComponent(VTimeZone.VTIMEZONE) as VTimeZone
@@ -138,4 +139,4 @@ object DateUtils {
138139
}
139140
}
140141

141-
}
142+
}

lib/src/main/kotlin/at/bitfire/ical4android/util/TimeApiExtensions.kt

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package at.bitfire.ical4android.util
88

99
import net.fortuna.ical4j.model.Date
1010
import net.fortuna.ical4j.model.DateTime
11+
import net.fortuna.ical4j.model.TimeZoneRegistry
1112
import net.fortuna.ical4j.util.TimeZones
1213
import java.time.Duration
1314
import java.time.Instant
@@ -59,25 +60,25 @@ object TimeApiExtensions {
5960
}
6061

6162
fun DateTime.requireTimeZone(): TimeZone =
62-
if (isUtc)
63-
TimeZones.getUtcTimeZone()
64-
else
65-
timeZone ?: TimeZone.getDefault()
63+
if (isUtc)
64+
TimeZones.getUtcTimeZone()
65+
else
66+
timeZone ?: TimeZone.getDefault()
6667

6768
fun DateTime.requireZoneId(): ZoneId =
68-
if (isUtc)
69-
ZoneOffset.UTC
70-
else
71-
timeZone?.toZoneIdCompat() ?: ZoneId.systemDefault()
69+
if (isUtc)
70+
ZoneOffset.UTC
71+
else
72+
timeZone?.toZoneIdCompat() ?: ZoneId.systemDefault()
7273

7374
fun DateTime.toLocalDate(): LocalDate =
74-
toZonedDateTime().toLocalDate()
75+
toZonedDateTime().toLocalDate()
7576

7677
fun DateTime.toLocalTime(): LocalTime =
77-
toZonedDateTime().toLocalTime()
78+
toZonedDateTime().toLocalTime()
7879

7980
fun DateTime.toZonedDateTime(): ZonedDateTime =
80-
ZonedDateTime.ofInstant(Instant.ofEpochMilli(time), requireZoneId())
81+
ZonedDateTime.ofInstant(Instant.ofEpochMilli(time), requireZoneId())
8182

8283
fun LocalDate.toIcal4jDate(): Date {
8384
val cal = Calendar.getInstance(TimeZones.getDateTimeZone())
@@ -92,14 +93,16 @@ object TimeApiExtensions {
9293
* Sets UTC flag ([DateTime.isUtc], means `...ThhmmddZ` format) when this zone-date time object has a
9394
* time zone of [ZoneOffset.UTC].
9495
*
96+
* @param tzRegistry time zone registry to get the [net.fortuna.ical4j.model.TimeZone] from (if needed)
97+
*
9598
* @return ical4j [DateTime] of the given zoned date-time
9699
*/
97-
fun ZonedDateTime.toIcal4jDateTime(): DateTime {
100+
fun ZonedDateTime.toIcal4jDateTime(tzRegistry: TimeZoneRegistry): DateTime {
98101
val date = DateTime(toEpochSecond() * MILLIS_PER_SECOND)
99102
if (zone == ZoneOffset.UTC)
100103
date.isUtc = true
101104
else
102-
date.timeZone = DateUtils.ical4jTimeZone(zone.id)
105+
date.timeZone = tzRegistry.getTimeZone(zone.id)
103106
return date
104107
}
105108

0 commit comments

Comments
 (0)