Reputation: 65
I have a plain date / time string (local european summer / winter time) I would like to transform into UTC.
The date I receive looks like this
{"message": "2021-05-01 15:39"}
But using LocalDateTime like this
(payload.message as LocalDateTime {format: "yyyy-MM-dd HH:mm"} >> "UTC")
will deliver "2021-05-01T15:49:00Z" - while correctly (respectively what I want) it should be "2021-05-01T13:49:00Z".
Upvotes: 3
Views: 2032
Reputation: 1910
You can't parse it directly to a DateTime (a date with time and timezone) because you don't have a TimeZone.. so, manually add the timezone and then parse it, which then allows you to convert the timezone to UTC.
(("$(payload.message) +02:00") as DateTime { format: "yyyy-MM-dd HH:mm ZZZZZ" }) >> "UTC"
Which outputs:
"2021-05-01T13:39:00Z"
Edit:
If we are assuming the timezone of the mule application matches the timezone of the date time coming through, and we want to dynamically tack that on, we could do this:
%dw 2.0
output application/json
fun currentUTCOffset() = now() as String { format: "ZZZ" }
---
(("$(payload.message) $(currentUTCOffset())") as DateTime { format: "yyyy-MM-dd HH:mm ZZZ" }) >> "UTC"
This is why it is so important for people to include the information about the timezone when transmitting times... it gets tricky to parse when they don't, especially in places that change their timezone twice a year.
Upvotes: 3
Reputation: 25699
One solution is to add the timezone manually:
%dw 2.0
output application/json
var value={"message": "2021-05-01 15:39"}
---
(value.message ++ " CET") as DateTime {format: "yyyy-MM-dd HH:mm zz"} >> "UTC"
Output:
"2021-05-01T13:39:00Z"
Upvotes: 6