How can I convert this to proper Kotlin Multiplatf...
# multiplatform
j
How can I convert this to proper Kotlin Multiplatform code?
val ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(d.firstRecv), ZoneId.systemDefault())
Currently, it's not working on iOS:
Uncaught Kotlin exception: kotlinx.datetime.DateTimeArithmeticException: Instant +1000000-12-31T23:59:59.999999999Z is not representable as LocalDateTime.
j
Something like this:
Copy code
Clock.System.now().toLocalDateTime(TimeZone.UTC)
Use kotlinx datetime library. toLocalDateTime is an extension in that library πŸ™‚
j
Thanks, and where do I put the
instant:Long
?
I have epoch milliseconds as the input
j
Ah sorry you want from existing unix timestamp in the other direction kind a πŸ˜› Hmm let me double check how I solved that πŸ˜„
πŸ™Œ 1
In general, always use the kotlinx datetime extensions/classes, and will be safe. That including stuff like timezones.
Like these kind of combos:
Copy code
Clock.System.todayIn(TimeZone.currentSystemDefault())
https://github.com/Kotlin/kotlinx-datetime This provides a lot of nice examples. Its not all ofc.
Instant.fromEpochMilliseconds(d.firstRecv).toLocalDateTime(TimeZone.currentSystemDefault())
I think in your case will be something like this πŸ™‚
πŸ™Œ 1
j
Thanks!!
Maybe I have been using the wrong "Instant"?
From java
For example with TimeZone
j
Depends what mean wrong Instant. I think they partly overlapping some things, and some not. Its similar way had to deal with desugaring in Android or Threeten library wrapping things. As never been able in Android doing this in a nice way. Its nice Jetbrains solve this multiplatform. I wished they had this back in time πŸ˜„
πŸ™Œ 1
But yeah I think if you using kotlinx.datatime.Instant and all those classes it will be fine. And equivalent to the System.currentMilli weird thing, you using Clock.now etc instead.
πŸ™Œ 1