All Projects → AllanHasegawa → Tempo

AllanHasegawa / Tempo

Licence: apache-2.0
A Kotlin library for Android to get the current time from multiple sources: SNTP, GPS; or your own time source.

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Tempo

Lora Serialization
LoraWAN serialization/deserialization library for The Things Network
Stars: ✭ 120 (+172.73%)
Mutual labels:  gps, time
Anti Testportal
Bypass for blur check on testportal.pl(net)
Stars: ✭ 39 (-11.36%)
Mutual labels:  time
Singledateandtimepicker
You can now select a date and a time with only one widget !
Stars: ✭ 921 (+1993.18%)
Mutual labels:  time
Time
Windows tool for measuring command/program execution speed
Stars: ✭ 21 (-52.27%)
Mutual labels:  time
Vue Datetime
Mobile friendly datetime picker for Vue. Supports date and datetime modes, i18n and more.
Stars: ✭ 928 (+2009.09%)
Mutual labels:  time
React Picky Date Time
A react component for date time picker. Online demo examples
Stars: ✭ 31 (-29.55%)
Mutual labels:  time
137 Stopmove
Algorithms to automatically discover stops and moves in GPS trajectories.
Stars: ✭ 19 (-56.82%)
Mutual labels:  gps
Pickadate.js
The mobile-friendly, responsive, and lightweight jQuery date & time input picker.
Stars: ✭ 7,753 (+17520.45%)
Mutual labels:  time
Tinydate
A tiny (349B) reusable date formatter. Extremely fast!
Stars: ✭ 990 (+2150%)
Mutual labels:  time
Igcwebview2
Modularised version of IGCWebview
Stars: ✭ 10 (-77.27%)
Mutual labels:  gps
Pytorch Forecasting
Time series forecasting with PyTorch
Stars: ✭ 849 (+1829.55%)
Mutual labels:  time
Time
Type-safe time calculations in Kotlin, powered by generics.
Stars: ✭ 939 (+2034.09%)
Mutual labels:  time
Zeit
Zeit, erfassen. A command line tool for tracking time spent on activities.
Stars: ✭ 33 (-25%)
Mutual labels:  time
Gpsdetector Library
🌎 Android library. If GPS disabled, dialog shown and if user confirms GPS enabled. (2016)
Stars: ✭ 24 (-45.45%)
Mutual labels:  gps
Letnice
A simple javascript calendar
Stars: ✭ 41 (-6.82%)
Mutual labels:  time
Seconds
Helpers for converting time to seconds.
Stars: ✭ 20 (-54.55%)
Mutual labels:  time
Esp32 Paxcounter
Wifi & BLE driven passenger flow metering with cheap ESP32 boards
Stars: ✭ 844 (+1818.18%)
Mutual labels:  gps
Dayjs
⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API
Stars: ✭ 37,373 (+84838.64%)
Mutual labels:  time
Covid19 Brazil Timeseries
Data collection to analyze the dissemination of COVID-19 through Brazilian states. Contributions are welcome.
Stars: ✭ 43 (-2.27%)
Mutual labels:  time
Period
Complex period comparisons
Stars: ✭ 1,001 (+2175%)
Mutual labels:  time

Tempo

A Kotlin library for Android to get the current time from multiple sources: SNTP, GPS; or your own time source.

Why is it important?

System.currentTimeMillis() [...] can be set by the user [...] so the time may jump backwards or forwards unpredictably.

-- https://developer.android.com/reference/android/os/SystemClock.html. July, 2017.

You can check how Tempo works in this blog post.

Basic usage

Initialize the library in your Application class:

class MyApp : Application {
    override fun onCreate() {
        Tempo.initialize(this)
        ...
    }
}

After the library is initialized, you can get the time with:

val timeNowInMs = Tempo.nowOrNull()

Tempo::nowOrNull() will return either a Long or a null. A null is returned when Tempo has not been initialized yet. When initialized, Tempo::nowOrNull() returns the current unix epoch time in milliseconds.

You can observe all the events emitted by the library:

Tempo.addEventsListener { event -> Log.d("TempoEvent", event.toString()) }

Dependency

Add the snippet below in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Then, add the dependency to your module:

dependencies {
    compile 'com.github.AllanHasegawa.Tempo:tempo:x.y.z'
}

Release

Time Sources

Tempo comes with two sources for time: SlackSntpTimeSource and AndroidGPSTimeSource.

SlackSntpTimeSource

The SlackSntpTimeSource is the default time source. Tempo is using a SNTP client implementation from the Android Framework. However, it's named "slack" because we are not enforcing a minimum roundtrip delay. The main reason behind this decision is because users with poor connection (very common on mobile) may never get a low enough roundtrip delay to successfully complete a SNTP request; retrying will just waste battery and increate data consumption. Therefore, this is the recommended time source to be used for Android.

This time source requires an active internet connection to work.

AndroidGPSTimeSource

The AndroidGPSTimeSource uses the device's GPS to get the current time. The accuracy will vary depending on the GPS.

This time source is in a separated module because it adds the ACCESS_FINE_LOCATION permission. Only use this module if you need this functionality.

To include it in your project, include the dependency:

compile 'com.github.AllanHasegawa.Tempo:tempo-android-gps-time-source:x.y.z'

Then, add it during initialization:

Tempo.initialize(this,
  timeSources = listOf(SlackSntpTimeSource(), AndroidGPSTimeSource(this))

Warning: If you are targeting Android SDK 23 or higher, you will have to request for the GPS permission at runtime.

Custom time source

You can create your own time source. Implement the io.tempo.TimeSource interface and then add it during initialization:

val customTs = MyCustomTs()
Tempo.initialize(this,
  timeSources = listOf(customTs))

Schedulers

A device's clock slowly drifts away from an accurate time. By default Tempo will periodically sync itself when the app is running.

However, because Tempo will not sync while the app is not running then starting the app may require a full sync, which can take time. To be able to sync even when the app is not running and making sure the app will always have a fresh cache, Tempo also offers a scheduler using Android's WorkManager.

To add, first include its module to your gradle build file:

compile 'com.github.AllanHasegawa.Tempo:tempo-android-workmanager-scheduler:x.y.z'

Then, add it during initialization:

Tempo.initialize(this,
  scheduler = WorkManagerScheduler(periodicIntervalMinutes = 60L))

FAQ

  1. What happens if the application gets destroyed?

By default, Tempo survives an application's process death. It accomplishes it by saving its state in the app's shared preference storage.

  1. What happens if the user reboots the device?

We invalidate all cache and a complete sync is required.

  1. What happens if the user has no internet access, no GPS, and rebooted his phone?

Then you should use a fallback strategy, like System.currentTimeMillis().

  1. Will Tempo ever support Java?

No official support for Java because Kotlin is now the official language for Android. However, it "should" work with Java.

License

Copyright (c) 2020 Allan Yoshio Hasegawa

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].