• Stars
    star
    1,392
  • Rank 33,065 (Top 0.7 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created almost 8 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Android NTP time library. Get the true current time impervious to device clock time changes

TrueTime for Android

TrueTime

⚠️ See work in progress section below. TrueTime is undergoing some changes


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

What is TrueTime?

TrueTime is an (S)NTP client for Android. It helps you calculate the date and time "now" impervious to manual changes to device clock time.

Why do I need TrueTime?

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 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 intro blog post .

How does TrueTime work?

In a conference talk, we explained how the full NTP implementation works. Check the video and slides out for implementation details.

TrueTime has since been migrated to Kotlin & Coroutines and no longer requires the additional Rx dependency. The concept hasn't changed but the above video is still a good explainer on the concept.

How do I use TrueTime?

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:<release-version>'
}

Usage

In your application class start the TrueTime sync-er like so:

// App.kt
class App : Application() {

    val trueTime = TrueTimeImpl()

    override fun onCreate() {
        super.onCreate()
        trueTime.sync()
    }
}

Once TrueTime gets a fix with an NTP time server, you can simply use:

(application as App).trueTime.now()

Btw don't do ↑, inject TrueTime into your app and then just call trueTime.now()

Installation

Usage

val trueTime = TrueTimeImpl()
trueTime.sync()
trueTime.now()

💥

⚠️ Work in Progress 4.0

With the move to Kotlin & Coroutines TrueTime 4 was a major overhaul. We still haven't ported some of the additional bells & whistles. This section keeps track of those features (that will come in the near future). TrueTime is completely functional without these additional features, so feel free to start using it.

Most of these todos should have corresponding "TODO" comments within the code.

  • Introduce a Cache provider
  • Add an interface CacheProvider so folks can inject in their preferred caching mechanisms
  • Provide a default cache implementation (probably using the non-android version of DataStore)
  • ? Provide example of using this with a Database like Realm
  • Algorithmic improvements

There are some exciting improvements that we have planned and use internally. Will have to upstream these changes (with a cleaner api + implementation)

  • Move android dependency to separate package

There's no reason for TrueTime (with the move to coroutines) to be an "android" library. It can be a pure kotlin lib.

The only remaining dependency is SystemClock (which we should just have a provider for).

  • Utilize all ntp pool addresses from TrueTimeParameters.ntpHostPool

We currently only take the first ntp host pool address from the supplied parameters. In the future, it would be nice to provide multiple ntp "pool" addresses like time.google.com, time.apple.com and utilize all of those to get the "best" value.

  • BootCompletedBroadcastReceiver sample

Everytime a device is rebooted, the Truetime info is invalid. Previous libraries included an actual BroadcastReceiver but this is better handled by the application than the library. For safe measure, I'll include an example of how this can be done in case folks are curious.

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.

More Repositories

1

lore

Lore makes machine learning approachable for Software Engineers and maintainable for Machine Learning Researchers
Python
1,549
star
2

Nantes

Swift TTTAttributedLabel replacement
Swift
1,110
star
3

makara

A Read-Write Proxy for Connections; Also provides an ActiveRecord adapter.
Ruby
902
star
4

TrueTime.swift

NTP library for Swift and Objective-C. Get the true time impervious to device clock changes.
Swift
583
star
5

wilson_score

Simple, dependency-free Wilson score
Ruby
154
star
6

formula

A functional reactive framework for managing state and side effects based on RxJava.
Kotlin
148
star
7

ohmycron

Run cron jobs in a standardized environment with logs and locking
Shell
138
star
8

ahab

Docker event handling with Python
Python
137
star
9

amountable

An easy and transparent way to attach, manage and sum Money fields to your ActiveRecord models.
Ruby
122
star
10

jardin-archived

A pandas.DataFrame-based ORM.
Python
84
star
11

Snacks

The Instacart Component Library
JavaScript
80
star
12

redux-rails

Redux and your server talking without fuss.
JavaScript
56
star
13

arn

A Python library for parsing AWS ARNs.
Python
44
star
14

ahoy-android

Android attribution library build on top of Ahoy for Ruby on Rails.
Java
20
star
15

cwam

CloudWatch Alarms Manager. Easy way to create default CloudWatch Alarms for AWS resources.
Python
19
star
16

instacart-android-pp-sandbox

Android challenge sandbox
Kotlin
9
star
17

aws-scripts-mon

[TO BE DELETED] Clone of AWS Cloudwatch Monitor Scripts + report inodes
Perl
4
star
18

redux-rails-resource

Simple interface of redux-rails resources for react components
JavaScript
3
star
19

OmniAX

UIAccessibility wrapper
Swift
2
star
20

instacart-ios-pp-starter

Starter app to give candidates starting on pair programming exercise
Swift
2
star
21

ExploreRxSwift

Swift
2
star
22

cloudflare-rails

Ruby
2
star
23

SocketLogger.swift

Lightweight, flexible logging utility compatible with any socket-based syslog service.
Swift
2
star
24

optplayground

A playground for optimization!
Python
1
star
25

instacart-android-pp-starter

Starter app to give candidates starting on pair programming exercise
Java
1
star
26

activerecord-import_with_callbacks

A library for bulk importing data using ActiveRecord
Ruby
1
star