How to truncate DateTime to minutes in DW?

95 Views Asked by At

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).

2

There are 2 best solutions below

2
aled On BEST ANSWER

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|
3
spoonboy On

The variation of the aled's version but without using import:

fun atBeginningOfMinute(t: DateTime):DateTime = 
 t-('PT$(t.seconds).$(t.nanoseconds)S' as Period)