Skip to content

Commit

Permalink
Merge pull request #633 from camunda/backport-621-to-1.16
Browse files Browse the repository at this point in the history
[Backport 1.16] fix(builtin-function): fix string conversion of days-times-durations
  • Loading branch information
saig0 authored May 8, 2023
2 parents 4002237 + 615ce4e commit 095934c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/main/scala/org/camunda/feel/syntaxtree/Val.scala
Original file line number Diff line number Diff line change
Expand Up @@ -179,21 +179,32 @@ case class ValYearMonthDuration(value: YearMonthDuration) extends Val {

case class ValDayTimeDuration(value: DayTimeDuration) extends Val {
override def toString: String = {
val day = Option(value.toDays).filterNot(_ == 0).map(_ + "D").getOrElse("")
val hour = Option(value.toHours % 24).filterNot(_ == 0).map(_ + "H").getOrElse("")
val minute = Option(value.toMinutes % 60).filterNot(_ == 0).map(_ + "M").getOrElse("")
val second = Option(value.getSeconds % 60).filterNot(_ == 0).map(_ + "S").getOrElse("")

val stringBuilder = new StringBuilder("")
if (value.isNegative) {
stringBuilder.append("-")
def makeString(sign: String, day: Long, hour: Long, minute: Long, second: Long): String = {
val d = Option(day).filterNot(_ == 0).map(_ + "D").getOrElse("")
val h = Option(hour).filterNot(_ == 0).map(_ + "H").getOrElse("")
val m = Option(minute).filterNot(_ == 0).map(_ + "M").getOrElse("")
val s = Option(second).filterNot(_ == 0).map(_ + "S").getOrElse("")

val stringBuilder = new StringBuilder("")
stringBuilder.append(sign).append("P").append(d)
if (h.nonEmpty || m.nonEmpty || s.nonEmpty) {
stringBuilder.append("T")
stringBuilder.append(h).append(m).append(s)
}
stringBuilder.toString()
}
stringBuilder.append("P").append(day)
if (hour.nonEmpty || minute.nonEmpty || hour.nonEmpty) {
stringBuilder.append("T")
}
stringBuilder.append(hour).append(minute).append(second)
stringBuilder.toString()

val day = value.toDays
val hour = value.toHours % 24
val minute = value.toMinutes % 60
val second = value.getSeconds % 60

if (day == 0 && hour == 0 && minute == 0 && second == 0)
"P0D"
else if (day <= 0 && hour <= 0 && minute <= 0 && second <= 0)
makeString("-", -day, -hour, -minute, -second)
else
makeString("", day, hour, minute, second)
}
override val properties: Map[String, Val] = Map(
"days" -> ValNumber(value.toDays),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,21 @@ class BuiltinConversionFunctionsTest
eval(""" string(date and time("2012-12-25T11:00:00+02:00")) """) should be(
ValString("2012-12-25T11:00:00+02:00"))
}
it should "convert zero-length days-time-duration" in {
eval(""" string(@"-PT0S") """) should be(ValString("P0D"))
eval(""" string(@"P0D") """) should be(ValString("P0D"))
eval(""" string(@"PT0H") """) should be(ValString("P0D"))
eval(""" string(@"PT0H0M") """) should be(ValString("P0D"))
eval(""" string(@"PT0H0M0S") """) should be(ValString("P0D"))
eval(""" string(@"P0DT0H0M0S") """) should be(ValString("P0D"))
}
it should "convert negative days-time-duration" in {

eval(""" string(@"-PT1S") """) should be(ValString("-PT1S"))
eval(""" string(@"-PT1H") """) should be(ValString("-PT1H"))
eval(""" string(@"-PT2M30S") """) should be(ValString("-PT2M30S"))
eval(""" string(@"-P1DT2H3M4S") """) should be(ValString("-P1DT2H3M4S"))
}
it should "convert days-time-duration" in {

eval(""" string(@"PT1H") """) should be(ValString("PT1H"))
Expand Down

0 comments on commit 095934c

Please sign in to comment.