Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 1.18] fix: Return normalized years-months-duration #977

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading