Skip to content

Commit

Permalink
Merge pull request #19 from Mobile-Innowise-Group/refactor/math_funct…
Browse files Browse the repository at this point in the history
…ions

Refactor math functions
  • Loading branch information
AndrKriv authored Aug 31, 2022
2 parents 1b8db9c + dc77dc6 commit 1f562c7
Showing 1 changed file with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class LocalDate private constructor(val day: Int, val month: Int, val year: Int)
if (daysToAdd == 0) {
this
} else {
val mjDay = Math.addExact(toEpochDay(), daysToAdd)
ofEpochDay(mjDay)
val mjDay = addExact(toEpochDay().toLong(), daysToAdd.toLong())
ofEpochDay(mjDay.toInt())
}

fun minusDays(daysToMinus: Int): LocalDate = plusDays(-daysToMinus)
Expand All @@ -29,8 +29,8 @@ class LocalDate private constructor(val day: Int, val month: Int, val year: Int)
} else {
val monthCount = year * 12L + (month - 1)
val calcMonths: Long = monthCount + monthsToAdd
val newYear = Math.floorDiv(calcMonths, 12).toInt()
val newMonth = Math.floorMod(calcMonths, 12) + 1
val newYear = floorDiv(calcMonths, 12).toInt()
val newMonth = (floorMod(calcMonths, 12) + 1).toInt()
resolvePreviousValid(day, newMonth, newYear)
}

Expand Down Expand Up @@ -68,6 +68,19 @@ class LocalDate private constructor(val day: Int, val month: Int, val year: Int)

internal fun daysUntil(end: LocalDate): Int = end.toEpochDay() - toEpochDay()

private fun addExact(a: Long, b: Long): Long {
val sum = a + b
return if (a xor sum < 0L && a xor b >= 0L) {
throw ArithmeticException("Addition overflows a long: $a + $b")
} else {
sum
}
}

private fun floorDiv(a: Long, b: Long): Long = if (a >= 0L) a / b else (a + 1L) / b - 1L

private fun floorMod(a: Long, b: Long): Long = (a % b + b) % b

private fun resolvePreviousValid(day: Int, month: Int, year: Int): LocalDate {
var day2 = day
when (month) {
Expand Down

0 comments on commit 1f562c7

Please sign in to comment.