Skip to content

How to use this library

Kaushik Gopal edited this page Sep 5, 2018 · 4 revisions

Installation

We use JitPack to host the library.

Add this to your application's build.gradle file:

repositories {
    maven {
        url "https://jitpack.io"
    }
}

dependencies {
    // ...
    implementation 'com.github.instacart.truetime-android:library-extension-rx:<release-version>'

    // or if you want the vanilla version of Truetime:
    implementation 'com.github.instacart.truetime-android:library:<release-version>'
}

Usage

Vanilla version

Importing 'com.github.instacart.truetime-android:library:<release-version>' should be sufficient for this.

Then you must initialize it in onCreate() in your class that extendsandroid.app.Application.

TrueTime.build().initialize();

initialize also must be run on a background thread. If you run it on the main thread, you will get a NetworkOnMainThreadException

You can then use:

Date noReallyThisIsTheTrueDateAndTime = TrueTime.now();

... #winning

Rx-ified Version

If you're down to using RxJava then we go all the way and implement the full NTP. Use the nifty initializeRx() api which takes in an NTP pool server host.

Again, you must initialize it in onCreate() in your class that extendsandroid.app.Application.

TrueTimeRx.build()
        .initializeRx("time.google.com")
        .subscribeOn(Schedulers.io())
        .subscribe(date -> {
            Log.v(TAG, "TrueTime was initialized and we have a time: " + date);
        }, throwable -> {
            throwable.printStackTrace();
        });

Now, as before:

TrueTimeRx.now(); // return a Date object with the "true" time.
You can’t perform that action at this time.