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

Migrate to Instant from Date #150

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
15 changes: 8 additions & 7 deletions app/src/main/java/com/instacart/sample/SampleActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import com.instacart.truetime.time.TrueTimeParameters
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.*
import java.time.Instant
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.*
import kotlin.concurrent.schedule
import kotlinx.coroutines.*

@SuppressLint("SetTextI18n")
@RequiresApi(Build.VERSION_CODES.O)
Expand Down Expand Up @@ -55,7 +55,7 @@ class SampleActivity : AppCompatActivity() {
kickOffTruetimeCoroutines()
kickOffTrueTimeRx()

binding.deviceTime.text = "Device Time: ${formatDate(Date())}"
binding.deviceTime.text = "Device Time: ${formatInstant(Instant.now())}"
}

private fun kickOffTruetimeCoroutines() {
Expand Down Expand Up @@ -85,7 +85,7 @@ class SampleActivity : AppCompatActivity() {
delay(500)
}

binding.truetimeNew.text = "(Coroutines): ${formatDate(sampleTrueTime.now())}"
binding.truetimeNew.text = "(Coroutines): ${formatInstant(sampleTrueTime.now())}"
}

if (false) {
Expand All @@ -106,14 +106,15 @@ class SampleActivity : AppCompatActivity() {
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ date -> binding.truetimeLegacy.text = "(Rx) : ${formatDate(date)}" },
{ Log.e("Demo", "something went wrong when trying to initializeRx TrueTime", it) })
{ date -> binding.truetimeLegacy.text = "(Rx) : ${formatInstant(date.toInstant())}" },
{ Log.e("Demo", "something went wrong when trying to initializeRx TrueTime", it) },
)

disposables.add(d)
}

private fun formatDate(date: Date): String {
return Instant.ofEpochMilli(date.time)
private fun formatInstant(instant: Instant): String {
return instant
.atZone(ZoneId.of("America/Los_Angeles"))
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.instacart.truetime.TrueTimeEventListener
import com.instacart.truetime.sntp.SntpResult
import com.instacart.truetime.time.TrueTimeParameters
import java.net.InetAddress
import java.util.*
import java.time.Instant

class TrueTimeLogEventListener : TrueTimeEventListener {
override fun initialize(params: TrueTimeParameters) {
Expand Down Expand Up @@ -57,7 +57,7 @@ class TrueTimeLogEventListener : TrueTimeEventListener {
Log.v("TrueTime4 TimeKeeper", "TimeKeeper storing time $ntpResult")
}

override fun returningTrueTime(trueTime: Date) {
override fun returningTrueTime(trueTime: Instant) {
Log.v("TrueTime4 TimeKeeper", "returning TrueTime $trueTime")
}

Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ buildscript {
ext {
kotlin_version = '1.8.0'
coroutines_version = '1.6.4'
desugar_version = '2.0.2'
}

repositories {
Expand Down
3 changes: 3 additions & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ android {
aarMetadata {
minCompileSdk = rootProject.ext.compileSdkVersion
}
multiDexEnabled true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be needed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I looked here. Do you know if it is wrong?
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see! I forgot this project was using minSdk 14. It should probably be bumped to 21 😅

}

buildTypes {
Expand All @@ -29,6 +30,7 @@ android {
}

compileOptions {
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
Expand All @@ -39,6 +41,7 @@ android {

dependencies {
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
coreLibraryDesugaring "com.android.tools:desugar_jdk_libs:$desugar_version"
}

afterEvaluate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.instacart.truetime
import com.instacart.truetime.sntp.SntpResult
import com.instacart.truetime.time.TrueTimeParameters
import java.net.InetAddress
import java.util.*
import java.time.Instant

interface TrueTimeEventListener : SntpEventListener, TimeKeeperListener {
/** [com.instacart.truetime.time.TrueTime] initialize call performed */
Expand Down Expand Up @@ -42,7 +42,7 @@ interface TimeKeeperListener {
fun storingTrueTime(ntpResult: SntpResult)

/** TimeKeeper has the "true" time and is returning it */
fun returningTrueTime(trueTime: Date)
fun returningTrueTime(trueTime: Instant)

/** TimeKeeper does not have the "true" time returning device time. */
fun returningDeviceTime()
Expand All @@ -63,6 +63,6 @@ object NoOpEventListener : TrueTimeEventListener {
override fun sntpRequestFailed(address: InetAddress, e: Exception) {}

override fun storingTrueTime(ntpResult: SntpResult) {}
override fun returningTrueTime(trueTime: Date) {}
override fun returningTrueTime(trueTime: Instant) {}
override fun returningDeviceTime() {}
}
12 changes: 6 additions & 6 deletions library/src/main/java/com/instacart/truetime/time/TimeKeeper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.os.SystemClock
import com.instacart.truetime.CacheProvider
import com.instacart.truetime.TimeKeeperListener
import com.instacart.truetime.sntp.SntpResult
import java.util.*
import java.time.Instant

// TODO: move android dependency to separate package
// so we can make Truetime a pure kotlin library
Expand All @@ -22,27 +22,27 @@ internal class TimeKeeper(

fun hasTheTime(): Boolean = cacheProvider.hasInfo()

fun nowSafely(): Date {
fun nowSafely(): Instant {
return if (hasTheTime()) {
nowTrueOnly()
} else {
listener.returningDeviceTime()
Date()
Instant.now()
}
}

fun nowTrueOnly(): Date {
fun nowTrueOnly(): Instant {
if (!hasTheTime()) throw IllegalStateException("TrueTime was not initialized successfully yet")
return now()
}

/** Given the available information provide the best known time */
private fun now(): Date {
private fun now(): Instant {
val ntpResult = cacheProvider.fetch()!!
val savedSntpTime: Long = ntpResult.trueTime()
val timeSinceBoot: Long = ntpResult.timeSinceBoot()
val currentTimeSinceBoot: Long = SystemClock.elapsedRealtime()
val trueTime = Date(savedSntpTime + (currentTimeSinceBoot - timeSinceBoot))
val trueTime = Instant.ofEpochMilli(savedSntpTime + (currentTimeSinceBoot - timeSinceBoot))
listener.returningTrueTime(trueTime)
return trueTime
}
Expand Down
9 changes: 5 additions & 4 deletions library/src/main/java/com/instacart/truetime/time/TrueTime.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.instacart.truetime.time

import java.util.*
import kotlinx.coroutines.Job
import java.time.Instant
import java.util.*

/** This is the main class that has the APIs for accessing Truetime. */
interface TrueTime {
Expand All @@ -18,16 +19,16 @@ interface TrueTime {
* This is [TrueTime]'s main function to get time It should respect
* [TrueTimeParameters.returnSafelyWhenUninitialized] setting
*/
fun now(): Date
fun now(): Instant

/**
* return the current time as calculated by TrueTime. If TrueTime doesn't [hasTheTime], will throw
* [IllegalStateException]
*/
@Throws(IllegalStateException::class) fun nowTrueOnly(): Date
@Throws(IllegalStateException::class) fun nowTrueOnly(): Instant

/** return [nowTrueOnly] if TrueTime is available otherwise fallback to System clock date */
fun nowSafely(): Date
fun nowSafely(): Instant

/** Does [TrueTime] have the "true" time or about to default to device time */
fun hasTheTime(): Boolean
Expand Down