https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
o

Ola Gawell

08/28/2019, 12:47 PM
Is there any way to get a timestamp (like System.currentTimeMillis()) in a multiplatform project without using expect/actual?
g

gildor

08/28/2019, 12:59 PM
Not yet, but there is already Clock interface in 1.3.50, but for now there is only implementation of Monotonic time
o

Ola Gawell

08/28/2019, 1:08 PM
OK, thanks!
p

Patrick Jackson

08/28/2019, 1:25 PM
Best lib I've found for date/time is Klock https://github.com/korlibs/klock But if you just need timestamp an expect/actual should
s

serebit

08/28/2019, 1:46 PM
It's the only fully implemented lib for date/time in kotlin multiplatform. It's also not very good. If you only need to get the current system time in milliseconds, use expect/actual.
m

Marc Knaup

08/28/2019, 3:22 PM
Or one which is quite alpha: https://github.com/fluidsonic/fluid-time 😄 But only jdk7, jdk8 & osx/ios so far by wrapping platform-specific API. It’s structure is similar to
java.time
.
o

Ola Gawell

08/28/2019, 3:48 PM
If I want to implement it myself using expect/actual and use it in a test, how should I config that? The kotlin tests in a multiplatform project is executed on the jvm right? Then I need to provide the java actual implementation in some way. Do you know how to do that?
p

Patrick Jackson

08/28/2019, 3:52 PM
@Ola Gawell There is an implementation in this sample app. This is link to the JVM implementation https://github.com/reduxkotlin/NameGameSampleApp/blob/master/common/src/jvmMain/kotlin/org/reduxkotlin/namegame/common/util/TimeUtil.kt
This is one way of doing it: common:
Copy code
expect object TimeUtil {
    fun systemTimeMs(): Long
}
iOS:
Copy code
actual object TimeUtil {
    actual fun systemTimeMs(): Long = NSDate().timeIntervalSince1970.toLong() * 1000
}
JVM:
Copy code
actual object TimeUtil {
    actual fun systemTimeMs(): Long = System.currentTimeMillis()
}
m

Marc Knaup

08/28/2019, 5:08 PM
I guess for iOS you mean:
Copy code
(NSDate().timeIntervalSince1970 * 1000).toLong()
otherwise you lose the milliseconds
100 Views