简体   繁体   English

DataWeave 2.0 将时间差作为一个周期

[英]DataWeave 2.0 get time difference as a period

I have DataWeave code that looks like below:我有如下所示的 DataWeave 代码:

%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.这导致以秒为单位的响应为 39600,即 11 小时。 I want the response to be as |PT11H|我希望响应为 |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|而不是 39600。当我们将秒数除以 3600 到 11 小时时,它可以工作,但我们需要它像 |PT11H| rather than 39600.而不是39600。

Above input t1 and t2 comes from config.以上输入 t1 和 t2 来自配置。 we can't change the setup.我们无法更改设置。

You can use seconds function of the dw::core::Periods module.您可以使用dw::core::Periods模块的seconds函数。 It takes the number of seconds as Parameter and returns a Period.它将秒数作为参数并返回一个周期。

%dw 2.0
import seconds from dw::core::Periods
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
---
seconds( |PT24H| - (t2 - t1) ) 

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您可以将代码减少到 datawave 以下

%dw 2.0
import seconds from dw::core::Periods
output application/java

var t1 = '08:00:00.000' as Time
var t2 = '21:00:00.000' as Time
---
seconds( |PT24H| - (t2 - t1) )  

The 'seconds' function is introduced in DataWeave version 2.4 and Mule Runtime 4.4 under the periodic module DataWeave 2.4 版和 Mule Runtime 4.4 中的“秒”功能在周期模块下引入

Reference: seconds |参考:秒 | MuleSoft Documentation https://docs.mulesoft.com/dataweave/2.4/dw-periods-functions-seconds MuleSoft 文档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.因此,如果您的 Mule 运行时版本低于 4.4,那么您必须按照问题本身所述除以 3600。

Below is that solution too for developers who are using Mule Runtime Version lower than 4.4对于使用低于 4.4 的 Mule 运行时版本的开发人员,以下也是该解决方案

First:第一的:

%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
---
"|PT" ++ (|PT24H| - (t2-t1))/3600 ++ "H|"

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 ie 3600) and get the hours.解释:由于 (|PT24H| - (t2-t1)) 给出 '39600' 作为以秒为单位的结果,现在要将秒转换为分钟,您需要除以 60,然后将分钟转换为小时,您需要再次除以 60,因此您可以直接除以 (60 * 60 即 3600) 并获得小时数。

as per suggestion in a comment if time is not in exact hours then we can use the below DataWeave script根据评论中的建议,如果时间不是精确的小时,​​那么我们可以使用下面的 DataWeave 脚本

Second:第二:

%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
---
"PT$(|PT24H| - (t2-t1))S" as Period

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM