Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: 6 days 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 1 year ago)
- Default Branch: main
- Last Pushed: 2023-08-20T20:44:59.000Z (about 1 year ago)
- Last Synced: 2024-08-02T09:26:37.544Z (3 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: 229 KB
- Stars: 14
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
Awesome Lists containing this project
- awesome-kotlin-multiplatform - Kronos Multiplatform - Network Time Protocol (NTP) library (Libraries / Utility)
- kmp-awesome - Kronos Multiplatform - Network Time Protocol (NTP) client (Libraries / ⏰ Date-Time)
README
# Kronos Multiplatform Library
![Maven Central](https://img.shields.io/maven-central/v/io.github.softartdev/kronos)
[![Build & Publish CI/CD](https://github.com/softartdev/Kronos-Multiplatform/actions/workflows/build_publish.yml/badge.svg)](https://github.com/softartdev/Kronos-Multiplatform/actions/workflows/build_publish.yml)Kotlin Multiplatform library for network time synchronization. It is an extension for the [kotlinx-datetime](https://github.com/Kotlin/kotlinx-datetime) library and supports the following platforms:
- Android
- iOS
- Desktop JVM (MacOS, Linux, Windows)
## Usage
### kotlinx-datetime 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) of the kotlinx-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/Kotlin/kotlinx-datetime/blob/master/core/common/src/Clock.kt) instance.
```kotlin
val networkTime: Instant = Clock.Network.now()
val systemTime: Instant = Clock.System.now()
val diff: Duration = networkTime - systemTime
```
### 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 iOSThe 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