-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a workaround for a possible null offset ID.
- Loading branch information
1 parent
b27b49e
commit b9138bc
Showing
3 changed files
with
54 additions
and
5 deletions.
There are no files selected for viewing
25 changes: 22 additions & 3 deletions
25
app/src/main/java/org/schabi/newpipe/ktx/OffsetDateTime.kt
This file contains 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 |
---|---|---|
@@ -1,10 +1,29 @@ | ||
package org.schabi.newpipe.ktx | ||
|
||
import java.time.OffsetDateTime | ||
import java.time.ZoneId | ||
import java.time.ZoneOffset | ||
import java.time.temporal.ChronoField | ||
import java.util.Calendar | ||
import java.util.Date | ||
import java.util.GregorianCalendar | ||
import java.util.TimeZone | ||
|
||
fun OffsetDateTime.toCalendar(zoneId: ZoneId = ZoneId.systemDefault()): Calendar { | ||
return GregorianCalendar.from(if (zoneId != offset) atZoneSameInstant(zoneId) else toZonedDateTime()) | ||
// This method is a modified version of GregorianCalendar.from(ZonedDateTime). | ||
// Math.addExact() and Math.multiplyExact() are desugared even though lint displays a warning. | ||
@SuppressWarnings("NewApi") | ||
fun OffsetDateTime.toCalendar(): Calendar { | ||
val cal = GregorianCalendar(TimeZone.getTimeZone("UTC")) | ||
val offsetDateTimeUTC = withOffsetSameInstant(ZoneOffset.UTC) | ||
cal.gregorianChange = Date(Long.MIN_VALUE) | ||
cal.firstDayOfWeek = Calendar.MONDAY | ||
cal.minimalDaysInFirstWeek = 4 | ||
try { | ||
cal.timeInMillis = Math.addExact( | ||
Math.multiplyExact(offsetDateTimeUTC.toEpochSecond(), 1000), | ||
offsetDateTimeUTC[ChronoField.MILLI_OF_SECOND].toLong() | ||
) | ||
} catch (ex: ArithmeticException) { | ||
throw IllegalArgumentException(ex) | ||
} | ||
return cal | ||
} |
This file contains 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
30 changes: 30 additions & 0 deletions
30
app/src/test/java/org/schabi/newpipe/ktx/OffsetDateTimeToCalendarTest.kt
This file contains 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,30 @@ | ||
package org.schabi.newpipe.ktx | ||
|
||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
import java.time.LocalDate | ||
import java.time.OffsetDateTime | ||
import java.time.ZoneOffset | ||
import java.util.Calendar | ||
import java.util.TimeZone | ||
|
||
class OffsetDateTimeToCalendarTest { | ||
@Test | ||
fun testRelativeTimeWithCurrentOffsetDateTime() { | ||
val calendar = LocalDate.of(2020, 1, 1).atStartOfDay() | ||
.atOffset(ZoneOffset.UTC).toCalendar() | ||
|
||
assertEquals(2020, calendar[Calendar.YEAR]) | ||
assertEquals(0, calendar[Calendar.MONTH]) | ||
assertEquals(1, calendar[Calendar.DAY_OF_MONTH]) | ||
assertEquals(0, calendar[Calendar.HOUR]) | ||
assertEquals(0, calendar[Calendar.MINUTE]) | ||
assertEquals(0, calendar[Calendar.SECOND]) | ||
assertEquals(TimeZone.getTimeZone("UTC"), calendar.timeZone) | ||
} | ||
|
||
@Test(expected = IllegalArgumentException::class) | ||
fun testRelativeTimeWithFarOffOffsetDateTime() { | ||
OffsetDateTime.MAX.minusYears(1).toCalendar() | ||
} | ||
} |