Is there any way to get a timestamp (like System.c...
# multiplatform
o
Is there any way to get a timestamp (like System.currentTimeMillis()) in a multiplatform project without using expect/actual?
g
Not yet, but there is already Clock interface in 1.3.50, but for now there is only implementation of Monotonic time
o
OK, thanks!
p
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
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
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
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
@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
I guess for iOS you mean:
Copy code
(NSDate().timeIntervalSince1970 * 1000).toLong()
otherwise you lose the milliseconds
694 Views