A very simple reactive Android library to obtain the current UTC time from the internet.
It fetches the current UTC time from www.timeapi.org using Retrofit.
Useful for getting current UTC time independently of device timezone and the set time.
On first use, UTC time is fetched from the API at www.timeapi.org/utc/now and stored as a base value.
On further requests, UTC time is calculated from the base value and device uptime, to save on network calls.
As a result, the UTC time is always correct, independently of the timezone set on device, even if the timezone (or time) is changed on the device after (or before) RxTime is used.
Add using Gradle:
compile 'com.github.simonpercic:rxtime:1.0.0'
Create a singleton instance of RxTime and use it:
// a singleton somewhere
RxTime rxTime = new RxTime();
...
rxTime.currentTime()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Long>() {
@Override public void call(Long time) {
// use time
}
});
Use the awesome Gradle Retrolambda Plugin with Java 8 to use lambdas:
rxTime.currentTime()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(time -> { // use time });
Use Dagger to hold a single instance of RxTime.
The Observable returned by RxTime runs on a background thread by default (using .subscribeOn(Schedulers.io())
).
See the included sample application to see a practical example of usage.
Open source, distributed under the MIT License. See LICENSE for details.