Search code examples
mulemule4mule-esb

How to truncate DateTime to minutes in DW?


Looking for a way to truncate a timestamp DateTime value (e.g. result of now()) to minutes with preserving the time zone i.e. something like this:

From: |2023-10-04T10:48:07.975905Z|

To: |2023-10-04T10:48:00.000000Z|

This of course can be done by first transforming DateTime to String, then applying string manipulations and then transforming back to DateTime. But it would be quite ugly, I guess.

So, looking for more elegant way of achieving this result.

Mule provides the similar function atBeginningOfHour that's doing truncation on an hour level.
I'm looking for a similar function but for minutes.

Mule DW version is 2.4.0 (the latest).


Solution

  • A solution using periods to subtract seconds. I cheated a little bit using string interpolation to combine seconds and nanoseconds. It could be replaced with a mathematical expression but I think the intention may not be as clear. I used the seconds() function of the Periods module that was introduced in Mule 4.4 to simplify creating the periods to be subtracted from the original time.

    fun truncateAtMinutes(t)=t - dw::core::Periods::seconds("$(t.seconds).$(t.nanoseconds)" as Number) 
    ---
    truncateAtMinutes(|2023-10-04T10:48:07.975905-03:00|)
    

    Output:

    |2023-10-04T10:48:00-03:00|