Skip to content

Commit

Permalink
Add addition/subtraction of centuries and decades to date-times (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikc5000 authored May 18, 2021
1 parent eb1d745 commit 8b47393
Show file tree
Hide file tree
Showing 15 changed files with 1,073 additions and 198 deletions.
52 changes: 45 additions & 7 deletions core/src/commonMain/kotlin/io/islandtime/Date.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ class Date(
}
}

/**
* Returns this date with [centuries] added to it.
*/
operator fun plus(centuries: Centuries): Date = plus(centuries.inYears)

/**
* Returns this date with [decades] added to it.
*/
operator fun plus(decades: Decades): Date = plus(decades.inYears)

/**
* Returns this date with [years] added to it.
*/
operator fun plus(years: Years): Date {
return if (years.value == 0L) {
this
Expand All @@ -120,6 +133,9 @@ class Date(
}
}

/**
* Returns this date with [months] added to it.
*/
operator fun plus(months: Months): Date {
return if (months.value == 0L) {
this
Expand All @@ -132,8 +148,14 @@ class Date(
}
}

/**
* Returns this date with [weeks] added to it.
*/
operator fun plus(weeks: Weeks): Date = plus(weeks.inDays)

/**
* Returns this date with [days] added to it.
*/
operator fun plus(days: Days): Date {
return if (days.value == 0L) {
this
Expand All @@ -156,6 +178,19 @@ class Date(
}
}

/**
* Returns this date with [centuries] subtracted from it.
*/
operator fun minus(centuries: Centuries): Date = minus(centuries.inYears)

/**
* Returns this date with [decades] subtracted from it.
*/
operator fun minus(decades: Decades): Date = minus(decades.inYears)

/**
* Returns this date with [years] subtracted from it.
*/
operator fun minus(years: Years): Date {
return if (years.value == Long.MIN_VALUE) {
this + Long.MAX_VALUE.years + 1.years
Expand All @@ -164,6 +199,9 @@ class Date(
}
}

/**
* Returns this date with [months] subtracted from it.
*/
operator fun minus(months: Months): Date {
return if (months.value == Long.MIN_VALUE) {
this + Long.MAX_VALUE.months + 1.months
Expand All @@ -172,14 +210,14 @@ class Date(
}
}

operator fun minus(weeks: Weeks): Date {
return if (weeks.value == Long.MIN_VALUE) {
this + Long.MAX_VALUE.days + 1.weeks
} else {
plus(weeks.negateUnchecked())
}
}
/**
* Returns this date with [weeks] subtracted from it.
*/
operator fun minus(weeks: Weeks): Date = minus(weeks.inDays)

/**
* Returns this date with [days] subtracted from it.
*/
operator fun minus(days: Days): Date {
return if (days.value == Long.MIN_VALUE) {
this + Long.MAX_VALUE.days + 1.days
Expand Down
104 changes: 104 additions & 0 deletions core/src/commonMain/kotlin/io/islandtime/DateTime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,31 @@ class DateTime(
return duration.toComponents { seconds, nanoseconds -> this + seconds + nanoseconds }
}

/**
* Returns this date-tme with [centuries] added to it.
*/
operator fun plus(centuries: Centuries): DateTime {
return if (centuries.value == 0L) {
this
} else {
copy(date = date + centuries)
}
}

/**
* Returns this date-time with [decades] added to it.
*/
operator fun plus(decades: Decades): DateTime {
return if (decades.value == 0L) {
this
} else {
copy(date = date + decades)
}
}

/**
* Returns this date-time with [years] added to it.
*/
operator fun plus(years: Years): DateTime {
return if (years.value == 0L) {
this
Expand All @@ -146,6 +171,9 @@ class DateTime(
}
}

/**
* Returns this date-time with [months] added to it.
*/
operator fun plus(months: Months): DateTime {
return if (months.value == 0L) {
this
Expand All @@ -154,6 +182,9 @@ class DateTime(
}
}

/**
* Returns this date-time with [weeks] added to it.
*/
operator fun plus(weeks: Weeks): DateTime {
return if (weeks.value == 0L) {
this
Expand All @@ -162,6 +193,9 @@ class DateTime(
}
}

/**
* Returns this date-time with [days] added to it.
*/
operator fun plus(days: Days): DateTime {
return if (days.value == 0L) {
this
Expand All @@ -170,6 +204,9 @@ class DateTime(
}
}

/**
* Returns this date-time with [hours] added to it.
*/
operator fun plus(hours: Hours): DateTime {
return if (hours.value == 0L) {
this
Expand All @@ -186,6 +223,9 @@ class DateTime(
}
}

/**
* Returns this date-time with [minutes] added to it.
*/
operator fun plus(minutes: Minutes): DateTime {
return if (minutes.value == 0L) {
this
Expand All @@ -211,6 +251,9 @@ class DateTime(
}
}

/**
* Returns this date-time with [seconds] added to it.
*/
operator fun plus(seconds: Seconds): DateTime {
return if (seconds.value == 0L) {
this
Expand All @@ -234,6 +277,9 @@ class DateTime(
}
}

/**
* Returns this date-time with [milliseconds] added to it.
*/
operator fun plus(milliseconds: Milliseconds): DateTime {
return if (milliseconds.value == 0L) {
this
Expand All @@ -242,6 +288,9 @@ class DateTime(
}
}

/**
* Returns this date-time with [microseconds] added to it.
*/
operator fun plus(microseconds: Microseconds): DateTime {
return if (microseconds.value == 0L) {
this
Expand All @@ -250,6 +299,9 @@ class DateTime(
}
}

/**
* Returns this date-time with [nanoseconds] added to it.
*/
operator fun plus(nanoseconds: Nanoseconds): DateTime {
return if (nanoseconds.value == 0L) {
this
Expand Down Expand Up @@ -287,6 +339,31 @@ class DateTime(
return this - duration.seconds - duration.nanosecondAdjustment
}

/**
* Returns this date-time with [centuries] subtracted from it.
*/
operator fun minus(centuries: Centuries): DateTime {
return if (centuries.value == 0L) {
this
} else {
copy(date = date - centuries)
}
}

/**
* Returns this date-time with [decades] subtracted from it.
*/
operator fun minus(decades: Decades): DateTime {
return if (decades.value == 0L) {
this
} else {
copy(date = date - decades)
}
}

/**
* Returns this date-time with [years] subtracted from it.
*/
operator fun minus(years: Years): DateTime {
return if (years.value == 0L) {
this
Expand All @@ -295,6 +372,9 @@ class DateTime(
}
}

/**
* Returns this date-time with [months] subtracted from it.
*/
operator fun minus(months: Months): DateTime {
return if (months.value == 0L) {
this
Expand All @@ -303,6 +383,9 @@ class DateTime(
}
}

/**
* Returns this date-time with [weeks] subtracted from it.
*/
operator fun minus(weeks: Weeks): DateTime {
return if (weeks.value == 0L) {
this
Expand All @@ -311,6 +394,9 @@ class DateTime(
}
}

/**
* Returns this date-time with [days] subtracted from it.
*/
operator fun minus(days: Days): DateTime {
return if (days.value == 0L) {
this
Expand All @@ -319,6 +405,9 @@ class DateTime(
}
}

/**
* Returns this date-time with [hours] subtracted from it.
*/
operator fun minus(hours: Hours): DateTime {
return if (hours.value == Long.MIN_VALUE) {
this + Long.MAX_VALUE.hours + 1.hours
Expand All @@ -327,6 +416,9 @@ class DateTime(
}
}

/**
* Returns this date-time with [minutes] subtracted from it.
*/
operator fun minus(minutes: Minutes): DateTime {
return if (minutes.value == Long.MIN_VALUE) {
this + Long.MAX_VALUE.minutes + 1.minutes
Expand All @@ -335,6 +427,9 @@ class DateTime(
}
}

/**
* Returns this date-time with [seconds] subtracted from it.
*/
operator fun minus(seconds: Seconds): DateTime {
return if (seconds.value == Long.MIN_VALUE) {
this + Long.MAX_VALUE.seconds + 1.seconds
Expand All @@ -343,6 +438,9 @@ class DateTime(
}
}

/**
* Returns this date-time with [milliseconds] subtracted from it.
*/
operator fun minus(milliseconds: Milliseconds): DateTime {
return if (milliseconds.value == Long.MIN_VALUE) {
this + Long.MAX_VALUE.milliseconds + 1.milliseconds
Expand All @@ -351,6 +449,9 @@ class DateTime(
}
}

/**
* Returns this date-time with [microseconds] subtracted from it.
*/
operator fun minus(microseconds: Microseconds): DateTime {
return if (microseconds.value == Long.MIN_VALUE) {
this + Long.MAX_VALUE.microseconds + 1.microseconds
Expand All @@ -359,6 +460,9 @@ class DateTime(
}
}

/**
* Returns this date-time with [nanoseconds] subtracted from it.
*/
operator fun minus(nanoseconds: Nanoseconds): DateTime {
return if (nanoseconds.value == Long.MIN_VALUE) {
this + Long.MAX_VALUE.nanoseconds + 1.nanoseconds
Expand Down
5 changes: 3 additions & 2 deletions core/src/commonMain/kotlin/io/islandtime/DayOfWeek.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,13 @@ enum class DayOfWeek {
}

/**
* Adds days to this day of the week, wrapping when the beginning or end of the week is reached.
* Returns this day of the week with [days] added to it, wrapping when the beginning or end of the week is reached.
*/
operator fun plus(days: Days): DayOfWeek = plus((days.value % DAYS_PER_WEEK).toInt())

/**
* Subtracts days from this day of the week, wrapping when the beginning or end of the week is reached.
* Returns this day of the week with [days] subtracted from it, wrapping when the beginning or end of the week is
* reached.
*/
operator fun minus(days: Days): DayOfWeek = plus(-(days.value % DAYS_PER_WEEK).toInt())

Expand Down
Loading

0 comments on commit 8b47393

Please sign in to comment.