Skip to content

Common: add Instant.fromEpochSeconds(Long, Int) #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions core/jsMain/src/Instant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ public actual class Instant internal constructor(internal val value: jtInstant)
}

actual fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Long): Instant = try {
Instant(jtInstant.ofEpochSecond(epochSeconds, nanosecondAdjustment))
val secs = safeAdd(epochSeconds, floorDiv(nanosecondAdjustment, NANOS_PER_ONE.toLong()))
val nos = floorMod(nanosecondAdjustment, NANOS_PER_ONE.toLong()).toInt()
Instant(jtInstant.ofEpochSecond(secs, nos))
} catch (e: Throwable) {
if (!e.isJodaDateTimeException()) throw e
if (!e.isJodaDateTimeException() && e !is ArithmeticException) throw e
if (epochSeconds > 0) MAX else MIN
}

Expand Down
32 changes: 32 additions & 0 deletions core/jsMain/src/mathJs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,35 @@ internal actual fun safeMultiply(a: Int, b: Int): Int {
if (result > Int.MAX_VALUE || result < Int.MIN_VALUE) throw ArithmeticException("Multiplication overflows Int range: $a * $b.")
return result.toInt()
}

/**
* Returns the floor division.
* <p>
* This returns {@code 0} for {@code floorDiv(0, 4)}.<br />
* This returns {@code -1} for {@code floorDiv(-1, 4)}.<br />
* This returns {@code -1} for {@code floorDiv(-2, 4)}.<br />
* This returns {@code -1} for {@code floorDiv(-3, 4)}.<br />
* This returns {@code -1} for {@code floorDiv(-4, 4)}.<br />
* This returns {@code -2} for {@code floorDiv(-5, 4)}.<br />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use {@code} and <br> tags in kdocs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, and also removed such constructs where they were copied from.

*
* @param a the dividend
* @param b the divisor
* @return the floor division
*/
internal fun floorDiv(a: Long, b: Long): Long = if (a >= 0) a / b else (a + 1) / b - 1

/**
* Returns the floor modulus.
*
*
* This returns `0` for `floorMod(0, 4)`.<br></br>
* This returns `1` for `floorMod(-1, 4)`.<br></br>
* This returns `2` for `floorMod(-2, 4)`.<br></br>
* This returns `3` for `floorMod(-3, 4)`.<br></br>
* This returns `0` for `floorMod(-4, 4)`.<br></br>
*
* @param a the dividend
* @param b the divisor
* @return the floor modulus (positive)
*/
internal fun floorMod(a: Long, b: Long): Long = (a % b + b) % b