Giter Club home page Giter Club logo

ticktock's Introduction

TickTock

TickTock is a timezone data management library for the JVM and Android targeting java.time.* APIs in Java 8 or above. Use this library if you want to bundle timezone data directly with your application rather than rely on the current device timezones (Android) or the default <java.home>/lib version (JVM only).

Usage

Android

Simply add the android tzdb startup dependency:

implementation 'dev.zacsweers.ticktock:ticktock-android-tzdb:<version>'

This will automatically initialize it appropriately without any configuration needed using androidx.startup. If you don't want automatic initialization, you can disable it and do it manually.

<provider
    android:name="androidx.startup.InitializationProvider"
    android:authorities="${applicationId}.androidx-startup"
    android:exported="false"
    tools:node="merge">
    <meta-data android:name="dev.zacsweers.ticktock.android.tzdb.startup.AndroidTzdbRulesInitializer"
        tools:node="remove"/>
</provider>
AndroidTzdbZoneRules.init(<context>)

Note that Android usage assumes use of core library desugaring. If you are not using it and/or are minSdk 26+, this library is of no use to you!

JVM

Add the jvm tzdb dependency:

implementation 'dev.zacsweers.ticktock:ticktock-jvm-tzdb:<version>'

Then call its initializer as early as possible in your application.

JvmTzdbZoneRules.init()

This will make ZoneRulesProvider use TickTock's implementation with its bundled timezone data.

Advanced

Eager caching

TickTock's default behavior is to lazily load timezone data on-demand. If you want to eagerly load data (for instance - on a background thread), TickTock offers a convenience helper API:

// Synchronously load and cache all timezone rules
EagerZoneRulesLoading.cacheZones();
Custom Data Loading

By default, TickTock will try to load timezone data from Java resources via ResourcesZoneDataLoader. If you wish to customize this, you can provide your own loading mechanism via implementing a custom ZoneDataLoader and/or ZoneDataProvider and registering them via TickTockPlugins before using any time APIs that would cause the system ZoneRulesProvider to initialize.

Usually, you would only want to implement a custom ZoneDataLoader and instantiate one of the built-in ZoneRulesProvider implementations with it. TickTock comes with two: TzdbZoneDataProvider (the common case) and LazyZoneDataProvider. You can also implement your own provider on top of any ZoneDataLoader type as you see fit.

CustomZoneDataLoader loader = new CustomZoneDataLoader();
TzdbZoneDataProvider provider = new TzdbZoneDataProvider(loader);
TickTockPlugins.setZoneDataProvider(() -> provider);

The Android artifacts use a custom assets-based loader to avoid the cost of loading from Java resources.

Custom Regions

By default, TickTock's prepackaged timezone data supports all regions. You can define your own via implementing a custom ZoneIdsProvider and registering it via TickTockPlugins before using any time APIs that would cause the system ZoneRulesProvider to initialize.

TickTockPlugins.setZoneIdsProvider(CustomZoneIdsProvider::new);

If no provider is specified, TickTock will use TzdbZoneProvider.

Lazy Zone Rules

TickTock's default behavior is focused around using traditional tzdb.dat files for timezone data implemented via TzdbZoneDataProvider. Early adopters can try a custom, lazy-loading solution via LazyZoneDataProvider inspired by LazyThreeTenBp. In theory, this artifact would be lower overhead on startup for devices with slower IO and a lower application-lifetime memory impact by only keeping used zones in memory. We're seeking feedback on whether this is truly worth supporting though, so please let us know!

Compiler CLI

To manually compile lazy zone rules yourself, you can use the ticktock-compiler API.

Usage: ticktockc [OPTIONS]

Options:
  --version TEXT            Version of the time zone data, e.g. 2017b.
  --srcdir DIRECTORY        Directory containing the unpacked leapsecond and
                            tzdb files.
  --tzdbfiles TEXT          Names of the tzdb files to process.
  --leapfile TEXT           Name of the leapsecond file to process.
  --codeoutdir DIRECTORY    Output directory for the generated java code.
  --tzdboutdir DIRECTORY    Output directory for the generated tzdb files.
  --verbose                 Verbose output.
  --language [JAVA|KOTLIN]  Language output (java or kotlin).
  --packagename TEXT        Package name to output with.
  -h, --help                Show this message and exit

Gradle coordinates:

Maven Central

implementation("dev.zacsweers.ticktock:ticktock-compiler:<version>")

If you want a fat jar binary, you can clone and run ./gradlew :ticktock-compiler:installDist. Binaries will be generated to ticktock-compiler/build/install/ticktock-compiler/bin. If there is interest, we may explore automatically uploading these as GitHub release artifacts.

Gradle Plugin

The Gradle plugin can be used to automatically download new TZ data, package it, and/or generate lazy zone rules if you want to manage data yourself.

plugins {
  id("dev.zacsweers.ticktock")
}

To generate a standard tzdb.dat: run the generateTzdbDat task.

To generate lazy zone rules: run the generateLazyZoneRules task.

Extension and configuration:

ticktock {
 /** The IANA timezone data version */
 val tzVersion: Property<String> // default to '2020d'

 /** The output directory to generate tz data to. Defaults to src/main/resources.  */
 val tzOutputDir: DirectoryProperty // defaults to src/main/resources

 /** Output directory for generated code, if generating for lazy rules. */
 val codeOutputDir: DirectoryProperty

 /** The language to generate in if generating for lazy rules, either `java` or `kotlin`. */
 val language: Property<String> // defaults to java

 /** The package name to generate in if generating for lazy rules. */
 val packageName: Property<String> // defaults to 'ticktock'
}

Download

Maven Central

// Core runtime artifact
implementation 'dev.zacsweers.ticktock:ticktock-runtime:<version>'

// TZDB artifacts
implementation 'dev.zacsweers.ticktock:ticktock-jvm-tzdb:<version>'
implementation 'dev.zacsweers.ticktock:ticktock-android-tzdb:<version>'

// Lazy zone rules artifacts
implementation 'dev.zacsweers.ticktock:ticktock-jvm-lazyzonerules:<version>'
implementation 'dev.zacsweers.ticktock:ticktock-android-lazyzonerules:<version>'

Snapshots of the development version are available in Sonatype's snapshots repository.

Versioning

Versions are semver + the current IANA TZ data version it's packaged with.

Example: 1.0.0-2020d

Note that while some artifacts don't contain TZ data, we use the same version for everything in the interest of simplicity.

Why?

https://www.zacsweers.dev/ticktock-desugaring-timezones/

License

Copyright (C) 2020 Zac Sweers & Gabriel Ittner

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

   https://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.

ticktock's People

Contributors

gabrielittner avatar github-actions[bot] avatar koral-- avatar zacsweers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

ticktock's Issues

TickTock not works with R8 minifyEnabled true

Hi!

I faced with a problem when build my app with minifyEnabled true.
After analyzing apk I found that R8 delete all ticktock classes except AndroidTzdbRulesInitializer.

I found ticktock-runtime.pro in sources of ticktock-runtime, but rule that specified there seems to be not valid:
-keep dev.zacsweers.ticktock.runtime.TickTockZoneRulesProvider

There should be something like this:
-keep class dev.zacsweers.ticktock.runtime.TickTockZoneRulesProvider { *; }

I add that line to my proguard-rules.pro, but app crashes anyway.

It crashes on that line with exception
java.lang.NoSuchMethodException: j$.time.zone.ZoneRules.<init>

I analyze intermediate desugar_lib_dex that L8 generates for desugaring classes and j$.time.zone.ZoneRules present there and in desugar_lib_project_keep_rules rules there is line -keep class j$.time.zone.ZoneRules, but as I understand this will not keep all class members, it should be -keep class j$.time.zone.ZoneRules { *; }.

So it seems to be a bug in L8 mechanism of minifying of desugaring classes.

I try to add -keep class j$.time.zone.ZoneRules { *; } to my proguard-rules.pro but there is no effect.
I try to add -keep class java.time.zone.ZoneRules { *; } to my proguard-rules.pro but there is also no effect.
I think there is because "L8 shrinks this dex file in isolation using the provided rules to produce the final, second dex file." as Jake Wharton pointed in his article.

Which workaround we can apply to make ticktock work with R8 minify?

Ticktock crashes when updating AGP & Desugar

What happened?

When I update AGP from 7.2.2 to 7.3.0 and Desugar from 1.1.6 to 1.2.0, Ticktock crashes with the following stacktrace

java.lang.Error: java.lang.ClassCastException: Cannot cast dev.zacsweers.ticktock.runtime.TickTockZoneRulesProvider to j$.time.zone.ZoneRulesProvider
    at j$.time.zone.ZoneRulesProvider$1.run(ZoneRulesProvider.java:165)
    at java.security.AccessController.doPrivileged(AccessController.java:43)
    at j$.time.zone.ZoneRulesProvider.<clinit>(ZoneRulesProvider.java:153)
    at j$.time.zone.ZoneRulesProvider.getRules(ZoneRulesProvider.java:240)
    at j$.time.ZoneRegion.ofId(ZoneRegion.java:120)
    at j$.time.ZoneId.of(ZoneId.java:409)
    at j$.time.ZoneId.of(ZoneId.java:357)

How to reproduce

In this repo, you can find the working version in main branch and the crash version in crash branch

If you can tell an workaround or create a hotfix, that would be superb

Crash when launching a build with `minifyEnabled true`

When launching an app with minifyEnabled true, I receive a crash with a ClassNotFoundException (see stacktrace below).

Seems like there ought to be a default proguard rule set up so that AndroidTzdbRulesInitializer isn't stripped.

Here is the complete stacktrace:

    java.lang.RuntimeException: Unable to get provider androidx.startup.InitializationProvider: androidx.startup.StartupException: java.lang.ClassNotFoundException: dev.zacsweers.ticktock.android.tzdb.startup.AndroidTzdbRulesInitializer
        at android.app.ActivityThread.installProvider(ActivityThread.java:7244)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:6780)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6697)
        at android.app.ActivityThread.access$1300(ActivityThread.java:237)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: androidx.startup.StartupException: java.lang.ClassNotFoundException: dev.zacsweers.ticktock.android.tzdb.startup.AndroidTzdbRulesInitializer
        at androidx.startup.a.a(AppInitializer.java:186)
        at androidx.startup.InitializationProvider.onCreate(InitializationProvider.java:42)
        at android.content.ContentProvider.attachInfo(ContentProvider.java:2388)
        at android.content.ContentProvider.attachInfo(ContentProvider.java:2358)
        at android.app.ActivityThread.installProvider(ActivityThread.java:7239)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:6780) 
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6697) 
        at android.app.ActivityThread.access$1300(ActivityThread.java:237) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
     Caused by: java.lang.ClassNotFoundException: dev.zacsweers.ticktock.android.tzdb.startup.AndroidTzdbRulesInitializer
        at java.lang.Class.classForName(Native Method)
        at java.lang.Class.forName(Class.java:454)
        at java.lang.Class.forName(Class.java:379)
        at androidx.startup.a.a(AppInitializer.java:173)
        at androidx.startup.InitializationProvider.onCreate(InitializationProvider.java:42) 
        at android.content.ContentProvider.attachInfo(ContentProvider.java:2388) 
        at android.content.ContentProvider.attachInfo(ContentProvider.java:2358) 
        at android.app.ActivityThread.installProvider(ActivityThread.java:7239) 
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:6780) 
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6697) 
        at android.app.ActivityThread.access$1300(ActivityThread.java:237) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
     Caused by: java.lang.ClassNotFoundException: Didn't find class "dev.zacsweers.ticktock.android.tzdb.startup.AndroidTzdbRulesInitializer" on path: DexPathList[[zip file "/data/app/~~aaa==/com.redacted.beta-TnFgwproPLfm87dk8g65mA==/base.apk"],nativeLibraryDirectories=[/data/app/~~aaa==/com.redacted.beta-aaa==/lib/arm64, /data/app/~~aaa==/com.redacted.beta-aaa==/base.apk!/lib/arm64-v8a, /system/lib64, /system_ext/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at java.lang.Class.classForName(Native Method) 
        at java.lang.Class.forName(Class.java:454) 
        at java.lang.Class.forName(Class.java:379) 
        at androidx.startup.a.a(AppInitializer.java:173) 
        at androidx.startup.InitializationProvider.onCreate(InitializationProvider.java:42) 
        at android.content.ContentProvider.attachInfo(ContentProvider.java:2388) 
        at android.content.ContentProvider.attachInfo(ContentProvider.java:2358) 
        at android.app.ActivityThread.installProvider(ActivityThread.java:7239) 
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:6780) 
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6697) 
        at android.app.ActivityThread.access$1300(ActivityThread.java:237) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

Can we automatically publish releases?

  • IANA has a "latest" endpoint we can download from and compare to current
  • We could set this up on a cron job, checking once a day
  • If the version differs from what's in gradle.properties
    • Update gradle.properties' version
    • Re-generate tz data in jvm/android artifacts
  • Push? Open a PR + auto-merge? Leaning toward build/check + push + tag + upload release

Would also be fine with starting with opening a PR first

Another thing to consider is possibly publishing tzdb.dat and a zip of lazy rules via github releases, allowing them to be downloaded remotely.

Possibly useful: https://github.com/marketplace/actions/git-release

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.