dustin-johnson / timezonemap

Java/Kotlin library to map locations or regions to time zones
MIT License
50 stars 8 forks source link

Update for Gradle Version Catalog Kotlin DSL and Android Compose 2024 #15

Open haraldrudell opened 3 months ago

haraldrudell commented 3 months ago

For modern 2024 Android Apps, ie. Kotlin 2.0.0, Kotlin DSL Gradle Version Catalog and Compose:

in Android Studio project file gradle/libs.versions.toml:

[versions]
tzmap = "4.5"
zJni = "1.5.6-4" # 240804
[libraries]
zstd-jni = { group = "com.github.luben", name = "zstd-jni", version.ref = "zJni" } # for timezonemap
time-zone-map = { group = "us.dustinj.timezonemap", name = "timezonemap", version.ref = "tzmap" } # requires zstd-jni

in Android Studio project file app/build.gradle.kts:

dependencies {
    implementation(libs.time.zone.map) {
        exclude(group = "com.github.luben", module = "zstd-jni")
    }
    implementation(libs.zstd.jni) {
        artifact {
            type = "aar"
        }
    }
}

In kotlin:

import us.dustinj.timezonemap.TimeZoneMap

// takes 11 s, may throw
val timeZoneMap = TimeZoneMap.forEverywhere()
// may be null, may throw
val tz = timeZoneMap.getOverlappingTimeZone(
                degreesLatitude,
                degreesLongitude,
            )
…
// “America/Los_Angeles”
timeZoneString = tz.zoneId
…
                // throws if time zone string is unknown
                val timeZone = TimeZone.of(timeZoneString)

I think this should be documented and implemented in the Android example project

dustin-johnson commented 3 months ago

Thank you! I'll gladly incorporate this. If I don't get to it this week, don't hesitate to ping me.