2.android.pool.ntp.org,GitHub - instacart/truetime-android at 2.0

TrueTime for Android

62a43c54ec965000d0e7f9e0de4884ae.png

Make sure to check out our counterpart too: TrueTime, an SNTP library for Swift.

NTP client for Android. Calculate the date and time "now" impervious to manual changes to device clock time.

In certain applications it becomes important to get the real or "true" date and time. On most devices, if the clock has been changed manually, then a new Date() instance gives you a time impacted by local settings.

Users may do this for a variety of reasons, like being in different timezones, trying to be punctual by setting their clocks 5 – 10 minutes early, etc. Your application or service may want a date that is unaffected by these changes and reliable as a source of truth. TrueTime gives you that.

You can read more about the use case in our blog post.

How is TrueTime calculated?

It's pretty simple actually. We make a request to an NTP server that gives us the actual time. We then establish the delta between device uptime and uptime at the time of the network response. Each time "now" is requested subsequently, we account for that offset and return a corrected Date object.

Also, once we have this information it's valid until the next time you boot your device. This means if you enable the disk caching feature, after a single successfull NTP request you can use the information on disk directly without ever making another network request. This applies even across application kills which can happen frequently if your users have a memory starved device.

Installation

We use Jitpack to host the library.

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

repositories {

maven {

url "https://jitpack.io"

}

}

dependencies {

// ...

compile 'com.github.instacart.truetime-android:library-extension-rx:'

// or if you want the vanilla version of Truetime:

compile 'com.github.instacart.truetime-android:library:'

}

Usage

Vanilla version

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

TrueTime.build().initialize();

initialize 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.

TrueTimeRx.build()

.initializeRx("0.north-america.pool.ntp.org")

.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.

What is nifty about the Rx version?

as against just SNTP, you get full NTP (read: far more accurate time)

the NTP pool address you provide is resolved into multiple IP addresses

we query each IP multiple times, guarding against checks, and taking the best response

if any one of the requests fail, we retry that failed request (alone) for a specified number of times

we collect all the responses and again filter for the best result as per the NTP spec

Notes/tips:

Each initialize call makes an SNTP network request. TrueTime needs to be initialized only once ever, per device boot. Use TrueTime's withSharedPreferences option to make use of this feature and avoid repeated network request calls.

Preferable use dependency injection (like Dagger) and create a TrueTime @Singleton object

You can read up on Wikipedia the differences between SNTP and NTP.

Exception handling:

an InvalidNtpServerResponseException is thrown every time the server gets an invalid response (this can happen with the individual SNTP calls).

If TrueTime fails to initialize (because of the above exception being throw), then an IllegalStateException is thrown if you try to call TrueTime.now() at a later point.

License

Licensed under the Apache License, Version 2.0 (the "License");

you may not use this file except in compliance with the License.

You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值