2

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

3
  • Creating it as string and appending seconds is not that ugly in your case. infact the ootb function atBeginningOfHour that you mention uses exactly that Commented Oct 4, 2023 at 6:38
  • @HarshankBansal, can you pls point me to where I can have a look at the "atBeginningOfHour" source code? Commented Oct 5, 2023 at 0:22
  • 1
    I just do a ctrl+click on the method name when writing dataweave in Anypoint studio Commented Oct 5, 2023 at 1:08

2 Answers 2

1

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|
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the idea. I've made it slightly simpler, as no need in importing the Periods module.
I removed the import by using the full module name.
0

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) 

3 Comments

That was my first approach but I tried to avoid string conversions as much as possible. Note that your answer is missing subtracting the minutes. This is method is valid for Mule 4.3 and earlier versions since it doesn't depends on the new functions introduced in Mule 4.4.
@aled, there is no need in subtracting minutes. If I had a need in subtracting the minutes as well, I would just use the function "atBeginningOfHour" which is doing exactly that. So, you may wish to fix your code because it's a bit confusing now as there is no need in coding a custom function to achieve what your code is doing.
I may have misread that in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.