Skip to content

Commit 4cee41e

Browse files
authored
Merge pull request #971 from camunda/953-normalize-duration
fix: Return normalized years-months-duration
2 parents 970b8d5 + 8acb203 commit 4cee41e

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/main/scala/org/camunda/feel/syntaxtree/Val.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ case class ValYearMonthDuration(value: YearMonthDuration) extends Val {
219219
object ValYearMonthDuration {
220220

221221
def format(value: YearMonthDuration): String = {
222-
val year = value.getYears
223-
val month = value.getMonths % 12
222+
val year = value.toTotalMonths / 12
223+
val month = value.toTotalMonths % 12
224224

225225
if (year == 0 && month == 0)
226226
"P0Y"

src/test/scala/org/camunda/feel/api/StringRepresentationTypeTest.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ class StringRepresentationTypeTest extends AnyFlatSpec with Matchers {
121121
duration.toString should be("P2M")
122122
}
123123

124+
it should "return normalized format " in {
125+
ValYearMonthDuration(Period.parse("P2Y")).toString should be("P2Y")
126+
ValYearMonthDuration(Period.parse("P24M")).toString should be("P2Y")
127+
128+
ValYearMonthDuration(Period.parse("P25M")).toString should be("P2Y1M")
129+
ValYearMonthDuration(Period.parse("P35M")).toString should be("P2Y11M")
130+
131+
ValYearMonthDuration(Period.parse("P2Y13M")).toString should be("P3Y1M")
132+
}
133+
124134
"A days-time-duration" should "return 'P1DT2H3M4S' " in {
125135
val duration = ValDayTimeDuration(Duration.parse("P1DT2H3M4S"))
126136

@@ -151,6 +161,17 @@ class StringRepresentationTypeTest extends AnyFlatSpec with Matchers {
151161
duration.toString should be("PT4S")
152162
}
153163

164+
it should "return normalized format " in {
165+
ValDayTimeDuration(Duration.parse("P5D")).toString should be("P5D")
166+
ValDayTimeDuration(Duration.parse("PT120H")).toString should be("P5D")
167+
ValDayTimeDuration(Duration.parse("PT7200M")).toString should be("P5D")
168+
ValDayTimeDuration(Duration.parse("PT432000S")).toString should be("P5D")
169+
170+
ValDayTimeDuration(Duration.parse("PT121H")).toString should be("P5DT1H")
171+
ValDayTimeDuration(Duration.parse("PT7201M")).toString should be("P5DT1M")
172+
ValDayTimeDuration(Duration.parse("PT7261M")).toString should be("P5DT1H1M")
173+
}
174+
154175
"A list" should "return '[1, 2]' " in {
155176
ValList(List(ValNumber(1), ValNumber(2))).toString should be("[1, 2]")
156177
}

0 commit comments

Comments
 (0)