Igor Milakovic
03/30/2023, 11:14 PMsomeRepository.getData(forDate: "2023-03-30")
b) someRepository.getData(forDate: Date() /*or whatever kind of Date object shared can work with*/)
And then the same when you get the data back, does it make more sense to try and set up some actual/expect KMPDate
class or just work with plain String and parse them on each platform?
Thanks in advance!
P.S. I'd really like to avoid using any libraries for this, if possible.chr
03/30/2023, 11:18 PMIgor Milakovic
03/30/2023, 11:19 PMchr
03/30/2023, 11:20 PMjava.time
APIs under the hood on JVM-based targets anywaychr
03/30/2023, 11:21 PMIgor Milakovic
03/30/2023, 11:23 PMLandry Norris
03/31/2023, 12:47 AMephemient
03/31/2023, 1:43 AMSam
03/31/2023, 8:02 AMIgor Milakovic
03/31/2023, 2:49 PMkotlinx-datetime
library, but keeping it to commonMain as much as possible, and for communication/interface from iOS/Android to shared and back from shared to iOS/Android you're just using plain strings?
I mean, if I understood you correctly, this actually makes a lot of sense to me, because I treat my shared module very much like a backend of the app (or "mid-end" in the big picture, haha π
). So just like you prepare your dates as strings before a call to an API and parse the string-y response back to date objects, I don't see why this same approach wouldn't be very solid here. Yes, the drawback is handling dates/formatting twice (on iOS and Android separately), but you have the full flexibility.Sam
04/01/2023, 6:42 PMIgor Milakovic
04/03/2023, 4:18 PMSam
04/03/2023, 9:03 PM// Common source set
expect fun LocalDateTime.format(format: String): String
fun Instant.format(format: String) = toLocalDateTime(TimeZone.currentSystemDefault()).format(format)
// Android source set
import java.time.format.DateTimeFormatter
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.toJavaLocalDateTime
actual fun LocalDateTime.format(
format: String,
): String = DateTimeFormatter.ofPattern(format).format(this.toJavaLocalDateTime())
// iOS source set
import platform.Foundation.*
import kotlinx.datetime.*
actual fun LocalDateTime.format(format: String): String {
val dateFormatter = NSDateFormatter().apply {
dateFormat = format
}
return dateFormatter.stringFromDate(
toNSDate(NSCalendar.currentCalendar)
?: throw IllegalStateException("Could not convert kotlin date to NSDate $this")
)
}
Igor Milakovic
04/03/2023, 9:14 PM