3
3
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
4
4
*/
5
5
6
- package kotlinx.datetimex .darwin.converters
6
+ package kotlinx.datetime .darwin.converters
7
7
import kotlinx.cinterop.*
8
8
import kotlinx.datetime.*
9
9
import platform.Foundation.*
@@ -26,19 +26,19 @@ public fun Instant.toNSDate(): NSDate {
26
26
/* *
27
27
* Builds the corresponding [Instant].
28
28
* Even though Darwin only uses millisecond precision, it is possible that [date] uses larger resolution, storing
29
- * microseconds or even nanoseconds. In this case, the sub-millisecond parts of [date] are ignored, given that they
30
- * are likely to be conversion artifacts.
29
+ * microseconds or even nanoseconds. In this case, the sub-millisecond parts of [date] are rounded to the nearest
30
+ * millisecond, given that they are likely to be conversion artifacts.
31
31
*/
32
32
public fun NSDate.toKotlinInstant (): Instant {
33
33
val secs = timeIntervalSince1970()
34
- val millis = secs * 1000
34
+ val millis = secs * 1000 + if (secs > 0 ) 0.5 else - 0.5
35
35
return Instant .fromUnixMillis(millis.toLong())
36
36
}
37
37
38
38
/* *
39
39
* Converts the time zone to [NSTimeZone].
40
40
* If the time zone is represented as a fixed number of seconds from GMT (for example, if it is the result of a call to
41
- * [Instant .offset]) and the offset is not given in even minutes but also includes seconds, this method throws
41
+ * [TimeZone .offset]) and the offset is not given in even minutes but also includes seconds, this method throws
42
42
* [DateTimeException] to denote that lossy conversion would happen, as Darwin internally rounds the offsets to the
43
43
* nearest minute.
44
44
*/
0 commit comments