Skip to content

Commit

Permalink
Merge pull request #978 from camunda/backport-971-to-1.19
Browse files Browse the repository at this point in the history
[Backport 1.19] fix: Return normalized years-months-duration
  • Loading branch information
saig0 authored Jan 21, 2025
2 parents b9017be + d28af6e commit 0dd1a93
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/scala/org/camunda/feel/syntaxtree/Val.scala
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ case class ValYearMonthDuration(value: YearMonthDuration) extends Val {
object ValYearMonthDuration {

def format(value: YearMonthDuration): String = {
val year = value.getYears
val month = value.getMonths % 12
val year = value.toTotalMonths / 12
val month = value.toTotalMonths % 12

if (year == 0 && month == 0)
"P0Y"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ class StringRepresentationTypeTest extends AnyFlatSpec with Matchers {
duration.toString should be("P2M")
}

it should "return normalized format " in {
ValYearMonthDuration(Period.parse("P2Y")).toString should be("P2Y")
ValYearMonthDuration(Period.parse("P24M")).toString should be("P2Y")

ValYearMonthDuration(Period.parse("P25M")).toString should be("P2Y1M")
ValYearMonthDuration(Period.parse("P35M")).toString should be("P2Y11M")

ValYearMonthDuration(Period.parse("P2Y13M")).toString should be("P3Y1M")
}

"A days-time-duration" should "return 'P1DT2H3M4S' " in {
val duration = ValDayTimeDuration(Duration.parse("P1DT2H3M4S"))

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

it should "return normalized format " in {
ValDayTimeDuration(Duration.parse("P5D")).toString should be("P5D")
ValDayTimeDuration(Duration.parse("PT120H")).toString should be("P5D")
ValDayTimeDuration(Duration.parse("PT7200M")).toString should be("P5D")
ValDayTimeDuration(Duration.parse("PT432000S")).toString should be("P5D")

ValDayTimeDuration(Duration.parse("PT121H")).toString should be("P5DT1H")
ValDayTimeDuration(Duration.parse("PT7201M")).toString should be("P5DT1M")
ValDayTimeDuration(Duration.parse("PT7261M")).toString should be("P5DT1H1M")
}

"A list" should "return '[1, 2]' " in {
ValList(List(ValNumber(1), ValNumber(2))).toString should be("[1, 2]")
}
Expand Down

0 comments on commit 0dd1a93

Please sign in to comment.