-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
76 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
32 changes: 22 additions & 10 deletions
32
src/main/kotlin/io/webcodr/sweetdate/extension/LocalDate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,29 @@ | ||
package io.webcodr.sweetdate.extension | ||
|
||
import io.webcodr.sweetdate.unit.Days | ||
import io.webcodr.sweetdate.unit.Months | ||
import io.webcodr.sweetdate.unit.Unit | ||
import io.webcodr.sweetdate.unit.Weeks | ||
import io.webcodr.sweetdate.unit.Years | ||
import java.time.LocalDate | ||
|
||
operator fun LocalDate.plus(unit: Unit): LocalDate { | ||
return when(unit) { | ||
is Days -> this.plusDays(unit.days) | ||
is Weeks -> this.plusWeeks(unit.weeks) | ||
is Months -> this.plusMonths(unit.months) | ||
is Years -> this.plusYears(unit.years) | ||
enum class Sign { | ||
PLUS, MINUS | ||
} | ||
|
||
fun add( | ||
date: LocalDate, | ||
unit: Unit, | ||
sign: Sign | ||
): LocalDate { | ||
val value = when(sign) { | ||
Sign.PLUS -> unit.value | ||
Sign.MINUS -> -unit.value | ||
} | ||
|
||
return date.plus(value, unit.chronoUnit) | ||
} | ||
|
||
operator fun LocalDate.plus(unit: Unit): LocalDate { | ||
return add(this, unit, Sign.PLUS) | ||
} | ||
|
||
operator fun LocalDate.minus(unit: Unit): LocalDate { | ||
return add(this, unit, Sign.MINUS) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,20 @@ | ||
package io.webcodr.sweetdate.unit | ||
|
||
sealed class Unit | ||
import java.time.temporal.ChronoUnit | ||
|
||
class Days(val days: Long): Unit() | ||
class Weeks(val weeks: Long): Unit() | ||
class Months(val months: Long): Unit() | ||
class Years(val years: Long): Unit() | ||
sealed class Unit(val value: Long) { | ||
abstract val chronoUnit: ChronoUnit | ||
} | ||
|
||
class Days(value: Long): Unit(value) { | ||
override val chronoUnit = ChronoUnit.DAYS | ||
} | ||
class Weeks(value: Long): Unit(value) { | ||
override val chronoUnit = ChronoUnit.WEEKS | ||
} | ||
class Months(value: Long): Unit(value) { | ||
override val chronoUnit = ChronoUnit.MONTHS | ||
} | ||
class Years(value: Long): Unit(value) { | ||
override val chronoUnit = ChronoUnit.YEARS | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters