I need to sum the below hours and minutes.
[{"hours": 20.50},{"hours":30.55}]
expecting result should be: [{"hours" : 51.45}]
How do you think I can add the hours in dataweave 2.0 if not help me in java.
I need to sum the below hours and minutes.
[{"hours": 20.50},{"hours":30.55}]
expecting result should be: [{"hours" : 51.45}]
How do you think I can add the hours in dataweave 2.0 if not help me in java.
I'm not sure if we can take advantage of the Time Types that include DW to solve this, so I wrote a solution that uses a custom type HoursMinutes that let the hours to be more than 24, parse a Number to HoursMinutes, add them, and finally transform it again to Number.
Maybe I over complicate it... hahah
%dw 2.0
output application/json
type HoursMinutes = {hour:Number, minute: Number}
fun toHours(n: Number): HoursMinutes = do {
var split = n as String splitBy "."
var fromMin = floor(split[1] as Number / 60)
---
{hour:floor(n) + fromMin, minute:split[1] as Number mod 60}
}
fun add(hour1:HoursMinutes, hour2: HoursMinutes): HoursMinutes = do {
var fromMin = floor((hour1.minute + hour2.minute) / 60)
---
{hour: hour1.hour + hour2.hour + fromMin, minute: (hour1.minute + hour2.minute) mod 60}
}
fun toNumber(h: HoursMinutes) = (h.hour as String ++"."++ h.minute as String) as Number
---
[
{
"hours": toNumber(payload reduce ((item, accumulator:HoursMinutes = {hour:0, minute:0}) -> accumulator add toHours(item.hours)))
}
]