This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 736
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Propert time conversion APIs (#3396)
* Implement `TimeConverter` * Move time utils to `:shared:base` * Add capabilities to get the current time * Add unit tests for the time converter * Add property-based tests * Add deprecations * Remove unnecessary test runner * Add `DeviceTimeProviderTest` * Improve the dev-exp for the PR template * Improve the dev-exp for the PR template * Fix Detekt errors * Improve the unit testing guidelines
- Loading branch information
1 parent
c06b73a
commit 51d66c7
Showing
18 changed files
with
454 additions
and
29 deletions.
There are no files selected for viewing
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
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
This file was deleted.
Oops, something went wrong.
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
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
12 changes: 12 additions & 0 deletions
12
shared/base/src/main/java/com/ivy/base/time/TimeConverter.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,12 @@ | ||
package com.ivy.base.time | ||
|
||
import java.time.Instant | ||
import java.time.LocalDate | ||
import java.time.LocalDateTime | ||
|
||
interface TimeConverter { | ||
fun Instant.toLocalDateTime(): LocalDateTime | ||
fun Instant.toLocalDate(): LocalDate | ||
|
||
fun LocalDateTime.toUTC(): Instant | ||
} |
11 changes: 11 additions & 0 deletions
11
shared/base/src/main/java/com/ivy/base/time/TimeProvider.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,11 @@ | ||
package com.ivy.base.time | ||
|
||
import java.time.Instant | ||
import java.time.LocalDateTime | ||
import java.time.ZoneId | ||
|
||
interface TimeProvider { | ||
fun getZoneId(): ZoneId | ||
fun utcNow(): Instant | ||
fun localNow(): LocalDateTime | ||
} |
22 changes: 22 additions & 0 deletions
22
shared/base/src/main/java/com/ivy/base/time/impl/DeviceTimeProvider.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,22 @@ | ||
package com.ivy.base.time.impl | ||
|
||
import com.ivy.base.time.TimeProvider | ||
import java.time.Instant | ||
import java.time.LocalDateTime | ||
import java.time.ZoneId | ||
import javax.inject.Inject | ||
|
||
@Suppress("UnnecessaryPassThroughClass") | ||
class DeviceTimeProvider @Inject constructor() : TimeProvider { | ||
override fun getZoneId(): ZoneId { | ||
return ZoneId.systemDefault() | ||
} | ||
|
||
override fun utcNow(): Instant { | ||
return Instant.now() | ||
} | ||
|
||
override fun localNow(): LocalDateTime { | ||
return LocalDateTime.now() | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
shared/base/src/main/java/com/ivy/base/time/impl/StandardTimeConvert.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,28 @@ | ||
package com.ivy.base.time.impl | ||
|
||
import com.ivy.base.time.TimeConverter | ||
import com.ivy.base.time.TimeProvider | ||
import java.time.Instant | ||
import java.time.LocalDate | ||
import java.time.LocalDateTime | ||
import javax.inject.Inject | ||
|
||
class StandardTimeConvert @Inject constructor( | ||
private val timeZoneProvider: TimeProvider | ||
) : TimeConverter { | ||
|
||
override fun Instant.toLocalDateTime(): LocalDateTime { | ||
val zoneId = timeZoneProvider.getZoneId() | ||
return this.atZone(zoneId).toLocalDateTime() | ||
} | ||
|
||
override fun Instant.toLocalDate(): LocalDate { | ||
val zoneId = timeZoneProvider.getZoneId() | ||
return this.atZone(zoneId).toLocalDate() | ||
} | ||
|
||
override fun LocalDateTime.toUTC(): Instant { | ||
val zoneId = timeZoneProvider.getZoneId() | ||
return this.atZone(zoneId).toInstant() | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
shared/base/src/test/java/com/ivy/base/time/impl/DeviceTimeProviderTest.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,25 @@ | ||
package com.ivy.base.time.impl | ||
|
||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
class DeviceTimeProviderTest { | ||
|
||
private lateinit var timeProvider: DeviceTimeProvider | ||
|
||
@Before | ||
fun setup() { | ||
timeProvider = DeviceTimeProvider() | ||
} | ||
|
||
@Test | ||
fun `validate no crashes`() { | ||
// When | ||
timeProvider.getZoneId() | ||
timeProvider.utcNow() | ||
timeProvider.localNow() | ||
|
||
// Then | ||
// there are no crashes | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
shared/base/src/test/java/com/ivy/base/time/impl/StandardTimeConvertPropertyTest.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,71 @@ | ||
package com.ivy.base.time.impl | ||
|
||
import com.ivy.base.time.TimeConverter | ||
import com.ivy.base.time.TimeProvider | ||
import io.kotest.common.runBlocking | ||
import io.kotest.property.Arb | ||
import io.kotest.property.arbitrary.instant | ||
import io.kotest.property.arbitrary.localDateTime | ||
import io.kotest.property.arbitrary.removeEdgecases | ||
import io.kotest.property.arbitrary.zoneOffset | ||
import io.kotest.property.forAll | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
class StandardTimeConvertPropertyTest { | ||
|
||
private val timeProvider = mockk<TimeProvider>() | ||
|
||
private lateinit var converter: TimeConverter | ||
|
||
@Before | ||
fun setup() { | ||
converter = StandardTimeConvert( | ||
timeZoneProvider = timeProvider | ||
) | ||
} | ||
|
||
@Test | ||
fun `LocalDateTime-Instant isomorphism`(): Unit = runBlocking { | ||
forAll(Arb.zoneOffset(), Arb.localDateTime().removeEdgecases()) { zone, original -> | ||
// Given | ||
every { timeProvider.getZoneId() } returns zone | ||
|
||
// When | ||
val transformed = with(converter) { original.toUTC().toLocalDateTime() } | ||
|
||
// Then | ||
transformed == original | ||
} | ||
|
||
forAll(Arb.zoneOffset(), Arb.instant().removeEdgecases()) { zone, original -> | ||
// Given | ||
every { timeProvider.getZoneId() } returns zone | ||
|
||
// When | ||
val transformed = with(converter) { original.toLocalDateTime().toUTC() } | ||
|
||
// Then | ||
transformed == original | ||
} | ||
} | ||
|
||
@Test | ||
fun `Instant-LocalDate isomorphism up to the same day`(): Unit = runBlocking { | ||
forAll(Arb.zoneOffset(), Arb.instant().removeEdgecases()) { zone, original -> | ||
// Given | ||
every { timeProvider.getZoneId() } returns zone | ||
|
||
// When | ||
val transformed = with(converter) { | ||
val originalLocalDateTime = original.toLocalDateTime() | ||
original.toLocalDate().atTime(originalLocalDateTime.toLocalTime()).toUTC() | ||
} | ||
|
||
// Then | ||
transformed == original | ||
} | ||
} | ||
} |
Oops, something went wrong.