在dataweave中将时区从未定义更改为utc

Change timezone from undefined to utc in dataweave

我有一个普通的日期/时间字符串(当地欧洲夏令时/冬令时)我想转换成 UTC。

我收到的日期是这样的

{"message": "2021-05-01 15:39"}

但是像这样使用 LocalDateTime

(payload.message as LocalDateTime  {format: "yyyy-MM-dd HH:mm"}  >> "UTC")

将提供“2021-05-01T15:49:00Z”-正确(分别是我想要的)应该是“2021-05-01T13:49:00Z".

您不能将它直接解析为 DateTime(带有时间和时区的日期),因为您没有时区。因此,手动添加时区然后解析它,然后允许您转换UTC 时区。

(("$(payload.message) +02:00") as DateTime { format: "yyyy-MM-dd HH:mm ZZZZZ" }) >> "UTC"

输出:

"2021-05-01T13:39:00Z"

编辑:

如果我们假设 mule 应用程序的时区与通过的日期时间的时区相匹配,并且我们想动态地添加它,我们可以这样做:

%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"

这就是为什么人们在传输时间时包含有关时区的信息如此重要的原因......如果他们不这样做,解析起来会很棘手,尤其是在每年两次更改时区的地方。

一种解决方案是手动添加时区:

%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"

输出:

"2021-05-01T13:39:00Z"