Skip to content

Commit ac5ec5f

Browse files
committed
Add minus operator to DatePeriod and DateTimePeriod
1 parent 5a920ea commit ac5ec5f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

core/common/src/DateTimePeriod.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,17 @@ public operator fun DateTimePeriod.plus(other: DateTimePeriod): DateTimePeriod =
448448
safeAdd(totalNanoseconds, other.totalNanoseconds),
449449
)
450450

451+
/**
452+
* Subtracts one [DateTimePeriod] instance from another.
453+
*
454+
* @throws DateTimeArithmeticException if arithmetic overflow happens.
455+
*/
456+
public operator fun DateTimePeriod.minus(other: DateTimePeriod) : DateTimePeriod = buildDateTimePeriod(
457+
safeAdd(totalMonths, -other.totalMonths),
458+
safeAdd(days, -other.days),
459+
safeAdd(totalNanoseconds, -other.totalNanoseconds)
460+
)
461+
451462
/**
452463
* Adds two [DatePeriod] instances.
453464
*
@@ -457,3 +468,13 @@ public operator fun DatePeriod.plus(other: DatePeriod): DatePeriod = DatePeriod(
457468
safeAdd(totalMonths, other.totalMonths),
458469
safeAdd(days, other.days),
459470
)
471+
472+
/**
473+
* Subtracts one [DatePeriod] instance from another.
474+
*
475+
* @throws DateTimeArithmeticException if arithmetic overflow happens.
476+
*/
477+
public operator fun DatePeriod.minus(other: DatePeriod): DatePeriod = DatePeriod(
478+
safeAdd(totalMonths, -other.totalMonths),
479+
safeAdd(days, -other.days)
480+
)

0 commit comments

Comments
 (0)