https://github.com/softartdev/kronos-multiplatform
Kotlin Multiplatform SNTP library
https://github.com/softartdev/kronos-multiplatform
android datetime ios java kotlin kotlin-coroutines kotlin-multiplatform kotlin-multiplatform-mobile kotlin-native kronos ntp sntp time
Last synced: 2 months ago
JSON representation
Kotlin Multiplatform SNTP library
- Host: GitHub
- URL: https://github.com/softartdev/kronos-multiplatform
- Owner: softartdev
- Created: 2023-03-24T22:57:58.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-07-01T11:15:14.000Z (6 months ago)
- Last Synced: 2025-07-07T02:50:07.813Z (6 months ago)
- Topics: android, datetime, ios, java, kotlin, kotlin-coroutines, kotlin-multiplatform, kotlin-multiplatform-mobile, kotlin-native, kronos, ntp, sntp, time
- Language: Swift
- Homepage:
- Size: 254 KB
- Stars: 21
- Watchers: 1
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.MD
Awesome Lists containing this project
README
# Kronos Multiplatform Library

[](https://github.com/softartdev/Kronos-Multiplatform/actions/workflows/build_publish.yml)
Kotlin Multiplatform library for network time synchronization. It extends the [`kotlin.time`](https://kotlinlang.org/api/core/kotlin-stdlib/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](https://github.com/softartdev/Kronos-Multiplatform/blob/main/kronos/src/commonMain/kotlin/com/softartdev/kronos/ClockExt.kt) from the standard library. You can use the [`Clock.Network`](https://github.com/softartdev/Kronos-Multiplatform/blob/main/kronos/src/commonMain/kotlin/com/softartdev/kronos/NetworkClock.kt) class to retrieve the current network time, similar to using the built-in [`Clock.System`](https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/src/kotlin/time/Clock.kt#L60) instance.
```kotlin
val networkTime: Instant = Clock.Network.now() // 2025-06-30T13:42:43.712Z
val systemTime: Instant = Clock.System.now() // 2025-06-30T13:42:43.566455Z
val diff: Duration = networkTime - systemTime // 145.545ms
```
### Synchronization
When running the application, it's necessary to synchronize the time with the network using the platform-specific code:
- Android:
```kotlin
class App : Application() {
override fun onCreate() {
super.onCreate()
Clock.Network.sync(applicationContext)
}
}
```
- iOS:
```swift
@main
struct iosApp: App {
init() {
Clock.Network.sync()
}
var body: some Scene { ... }
}
```
- Desktop JVM:
```kotlin
fun main() {
Clock.Network.sync()
...
}
```
### Installation
The latest release is available on [Maven Central](https://repo1.maven.org/maven2/io/github/softartdev/kronos/).
1. Add the Maven Central repository if it is not already included:
```kotlin
repositories {
mavenCentral()
}
```
2. In multiplatform projects, add the following dependency to the `commonMain` source set dependencies:
```kotlin
commonMain {
dependencies {
implementation("io.github.softartdev:kronos:$latestVersion")
}
}
```
## Implementation
The main common interface is implemented using the following:
- [lyft/Kronos](https://github.com/lyft/Kronos-Android) for Java & Android
- [MobileNativeFoundation/Kronos](https://github.com/MobileNativeFoundation/Kronos) for iOS
The project is built and tested using the following:
- [Swift Klib Gradle Plugin](https://github.com/ttypic/swift-klib-plugin) for including Swift source files in KMM shared module
- [Compose Multiplatform, by JetBrains](https://github.com/JetBrains/compose-jb) for UI samples