Skip to content

Latest commit

 

History

History
78 lines (72 loc) · 3.17 KB

File metadata and controls

78 lines (72 loc) · 3.17 KB

Kronos Multiplatform Library

Maven Central Build & Publish CI/CD

Kotlin Multiplatform library for network time synchronization. It extends the kotlin.time API and supports the following platforms:

  • Android
  • iOS
  • Desktop JVM (MacOS, Linux, Windows)

Usage

kotlin.time Extension

The library extends the main Clock interface from the standard library. You can use the Clock.Network class to retrieve the current network time, similar to using the built-in Clock.System instance.

import kotlin.time.Clock
import kotlin.time.Instant
import kotlin.time.Duration
val networkTime: Instant = Clock.Network.now()
val systemTime: Instant = Clock.System.now()
val diff: Duration = networkTime - systemTime

kotlinx-datetime Deprecation

Starting from kotlinx-datetime 0.7.0, the Clock interface and Instant class were moved to the Kotlin standard library as kotlin.time.Clock and kotlin.time.Instant. The old types are now deprecated.

If your project still relies on them, replace the kotlinx.datetime.Clock and kotlinx.datetime.Instant imports with the corresponding kotlin.time ones. No other code changes are required.

Synchronization

When running the application, it's necessary to synchronize the time with the network using the platform-specific code:

  • Android:
class App : Application() {
    override fun onCreate() {
        super.onCreate()
        Clock.Network.sync(applicationContext)
    }
}
  • iOS:
@main
struct iosApp: App {
    init() {
        Clock.Network.sync()
    }
    var body: some Scene { ... }
}
  • Desktop JVM:
fun main() {
    Clock.Network.sync()
    ...
}

Installation

The latest release is available on Maven Central.

  1. Add the Maven Central repository if it is not already included:
repositories {
    mavenCentral()
}
  1. In multiplatform projects, add the following dependency to the commonMain source set dependencies:
commonMain {
    dependencies {
        implementation("io.github.softartdev:kronos:$latestVersion")
    }
}

Implementation

The main common interface is implemented using the following:

The project is built and tested using the following: