DataWeave 2.0获得时间差为一段时间
我的数据驱车代码如下:
%dw 2.0
output application/java
var timezone = (now() >> "Pacific/Auckland") as String {format: "XXX"}
var t1 = '08:00:00.000' ++ timezone
var t2 = '21:00:00.000' ++ timezone
---
(|PT24H| - (t2-t1))
这会在秒内响应39600,即11小时。我希望响应为| pt11h |而不是39600。当我们将秒数转换为3600至11小时时,它可以工作,但我们需要像| pt11h |而不是39600。
上方输入T1和T2来自config。我们无法更改设置。
I have DataWeave code that looks like below:
%dw 2.0
output application/java
var timezone = (now() >> "Pacific/Auckland") as String {format: "XXX"}
var t1 = '08:00:00.000' ++ timezone
var t2 = '21:00:00.000' ++ timezone
---
(|PT24H| - (t2-t1))
This results in a response in seconds as 39600 which is 11 hours. I want the response to be as |PT11H| rather than 39600. It works when we convert seconds to hours when dividing by 3600 to 11 hours but we need that to be like |PT11H| rather than 39600.
Above input t1 and t2 comes from config. we can't change the setup.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
seconds
功能dw :: core ::周期
模块。它需要秒为参数并返回一个周期。另一个建议,因为您在时间段和时间差异上工作,时区并不重要。您可以将代码减少到Dataawave下方
You can use
seconds
function of thedw::core::Periods
module. It takes the number of seconds as Parameter and returns a Period.Another suggestion, since you are working on time periods and time differences the timezone does not really matter. You can reduce your code to below datawave
“秒”函数在DataWeave版本2.4中引入,并在周期模块下的Mule Runtime 4.4
参考:秒| mulesoft文档 https:// https://datawaweave.com/datawaweave/2.4/2.4/2.4/ dw-periods-funnctions秒
因此,如果您的m子运行时版本低于4.4,那么您必须按照问题本身提到的3600进行分割。
下面的解决方案也适用于使用m子运行时版本低于4.4
的开发人员,首先:
说明:自(| PT24H | - (T2 -T1))给出'39600'的结果,结果为几秒钟。 ,现在要将秒钟转换为几分钟,您需要除以60,然后将几分钟转换为小时,您需要再次除以60,因此您可以直接除以(60 * 60 IE 3600)并获取时间。
根据评论中的建议,如果时间不在数小时内,我们可以使用以下DataWeave脚本
second:
The 'seconds' function is introduced in DataWeave version 2.4 and Mule Runtime 4.4 under the periodic module
Reference: seconds | MuleSoft Documentation https://docs.mulesoft.com/dataweave/2.4/dw-periods-functions-seconds
so if your Mule Runtime Version is lower than 4.4 then you have to go for dividing by 3600 as mentioned in the question itself.
Below is that solution too for developers who are using Mule Runtime Version lower than 4.4
First:
Explanation: Since (|PT24H| - (t2-t1)) gives '39600' as the result which is in seconds, now to convert seconds to minutes you need to divide by 60, and then to convert minutes to hours you need to again divide by 60, So instead you can directly divide by (60 * 60 i.e. 3600) and get the hours.
as per suggestion in a comment if time is not in exact hours then we can use the below DataWeave script
Second: