kotlinx-datetime
kotlinx-datetime copied to clipboard
Add LocalDate to NSDate and LocalDateTime to NSDate conversions
In iOS is much more convenient to work with NSDate than with NSDateComponents.
But currently there is no direct converters from LocalDate
/LocalDateTime
to NSDate
.
This PR fixes that.
Hey @dkhalanskyjb, Any news on this? Is there any more required changes to merge this PR? Thanks 👍
Hi!
-
Regarding
public fun LocalDateTime.toNSDate(): NSDate? {
, we do suppose thatnil
is not possible, but if we are wrong, this will lead to crashes in edge cases. This needs testing. -
NSDateComponents
is just a bag of values, the conversion is pretty straightforward.NSDate
, on the other hand, is a specific moment in time, the conversion is easy to get wrong. For example, I remember seeing issues with how years < 0 were treated in Darwin, and also, there's a difference between the ISO-8601 calendar we use and the Gregorian calendar which may be passed as an argument, so the wrong results may arise much closer to our time, in year 1582. This should be thoroughly checked and (I'm sure some problems will arise) fixed. -
The behavior of
LocalDate.toNSDate
needs to be clarified. What does it do, exactly? A function with a similar signature in our library isLocalDate.atStartOfDayIn(timeZone: TimeZone): Instant
. The function is tricky, as it needs to account for DST transitions, due to which a day sometimes starts at 01:00 (or, possibly, some other time), not at 00:00. Does the conversion by the Foundation library do the same? If so, this needs to be documented, and the name should probably be changed as well. -
It feels logical to have an inverse conversion then:
NSDate.toKotlinLocalDateTime
.
Closing due to inactivity and a lack of interest from our users. We can always create a new PR if there's a demand.
a note: just last week I was writing my own converter. fyi: my first attempt ended up with a timezone bug.
use case: I needed localized formatting of my local date.
I also need this, I just gave up on this because I don't have the time to do this with all the suggestions made
Ok, I filed an issue: https://github.com/Kotlin/kotlinx-datetime/issues/322 Please feel free to share any information.