• This repository has been archived on 19/Jun/2024
  • Stars
    star
    138
  • Rank 264,508 (Top 6 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 4 years ago
  • Updated 5 months ago

Reviews

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

Repository Details

A timezone data management library for the JVM and Android targeting java.time APIs in Java 8+

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.

More Repositories

1

CatchUp

An app for catching up on things.
Kotlin
1,871
star
2

barber

A custom view styling library for Android that generates the obtainStyledAttributes() and TypedArray boilerplate code for you.
Java
708
star
3

MoshiX

Extensions for Moshi including IR plugins, moshi-sealed, and more.
Kotlin
514
star
4

redacted-compiler-plugin

A Kotlin compiler plugin that generates redacted toString() implementations.
Kotlin
382
star
5

RxPalette

RxJava bindings for the Palette library on Android
Java
199
star
6

palettehelper

Utility Android app for generating color palettes of images using the Palette library. Written in Kotlin.
Kotlin
155
star
7

auto-service-ksp

A KSP implementation of AutoService
Kotlin
124
star
8

inspector

A tiny class validation library.
Java
62
star
9

json-serialization-benchmarking

Miscellaneous benchmarks for JSON serialization on JVM/Android
Java
58
star
10

copydynamic

Prototype of generating `copyDynamic` extension functions for kotlin data classes
Kotlin
56
star
11

FB_Mod_Bot

A Facebook bot for moderating groups
Python
45
star
12

kgp-150-leak-patcher

A simple plugin to patch the memory leak in Kotlin Gradle Plugin 1.5.0
Kotlin
40
star
13

blackmirror

Java
39
star
14

FieldSpottr

An app for checking field permit status on nycgovparks
Kotlin
36
star
15

RxNormalize

A normalizing operator for RxJava Observables.
Java
27
star
16

SUREwalk_android

Source code for the SUREwalk Android app
Java
19
star
17

kotlin-template

Kotlin
16
star
18

ArraySetBackport

Backport of Android's ArraySet implementation that supports API 14+
Java
16
star
19

ZacSweers

Kotlin
15
star
20

SpursGifs_xposterbot

Reddit bot I wrote while sitting bored in a hotel room for x-posting gifs/vines/gyfcats from /r/coys to /r/SpursGifs
Python
11
star
21

check-gradle-checksums

Check your gradle-wrapper jar's checksum on CI
Shell
10
star
22

empty-android-project

Empty boilerplate Android project with my preferred defaults
Java
9
star
23

kotlinpoet-contracts

Contracts support for KotlinPoet
Kotlin
7
star
24

AutoTransient

A transient annotation for AutoValue extensions.
Java
7
star
25

auto-service-kt

A Kotlin compiler plugin implementation of AutoService
Kotlin
6
star
26

configurable-checkreturnvalue

Configurable Lint and Error-Prone checker alternatives for @CheckReturnValue
Java
6
star
27

gfy_mirror

Gfy_mirror bot source code
Python
6
star
28

kgp-160-patcher

A simple monkeypatch for issues in Kotlin Gradle Plugin 1.6.0.
5
star
29

android-codepad

Automatically exported from code.google.com/p/android-codepad
Java
4
star
30

Android-TicTacToe

Java
4
star
31

aak

An annotation processor to generate Proxy-based helper methods for creating Kotlin annotations at runtime. Based on Google's AutoAnnotation processor for Java.
Kotlin
4
star
32

LabStatus

A very minimal JSON API for the UTCS labs using cgi scripts
3
star
33

Holo-Goo.gl

My first Android application, a Holo Goo.gl URL shortening app.
Java
3
star
34

succession

Kotlin
3
star
35

akibstool

Akib's tool
Kotlin
2
star
36

fb_group_analytics

Some Python code for analyzing Facebook groups
Python
2
star
37

entitydataclassfrommodules

Demo project for https://issuetracker.google.com/issues/67181813
Kotlin
1
star
38

hzsweers.github.io

Moved to https://zacsweers.dev
CSS
1
star
39

zacsweers.github.io

HTML
1
star
40

373_practice

Python
1
star
41

371p_practice

A group for getting extra practice and review for Downing's 371p OOP class
C++
1
star