Skip to content

Commit 63f1f82

Browse files
committed
Better precision when converting to NSDate
1 parent 3ee5406 commit 63f1f82

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

darwin-integration/nativeMain/src/Converters.kt

+4-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@ import platform.Foundation.*
1414
* resolution.
1515
*/
1616
public fun Instant.toNSDate(): NSDate {
17-
// TODO: support nanosecond precision.
18-
// This is not urgent, as Darwin itself only uses millisecond precision.
19-
// However, we could at least round to the nearest millisecond so that it would be true that
20-
// Instant.parse(...).toNSDate() == [formatter dateFromString: ...],
21-
// as Darwin does perform rounding.
22-
val secs = toEpochMilliseconds() / 1000.0
17+
val secs = epochSeconds * 1.0 + nanosecondsOfSecond / 1.0e9
18+
if (secs < NSDate.distantPast.timeIntervalSince1970 || secs > NSDate.distantFuture.timeIntervalSince1970) {
19+
throw DateTimeException("Boundaries of NSDate exceeded")
20+
}
2321
return NSDate.dateWithTimeIntervalSince1970(secs)
2422
}
2523

0 commit comments

Comments
 (0)