DataWeave 2.0获得时间差为一段时间

发布于 2025-02-11 11:07:16 字数 385 浏览 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七度光 2025-02-18 11:07:16

您可以使用 seconds 功能dw :: core ::周期模块。它需要秒为参数并返回一个周期。

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

另一个建议,因为您在时间段和时间差异上工作,时区并不重要。您可以将代码减少到Dataawave下方

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

You can use seconds function of the dw::core::Periods module. 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

%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) )  
谁人与我共长歌 2025-02-18 11:07:16

“秒”函数在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

的开发人员,首先:

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

说明:自(| PT24H | - (T2 -T1))给出'39600'的结果,结果为几秒钟。 ,现在要将秒钟转换为几分钟,您需要除以60,然后将几分钟转换为小时,您需要再次除以60,因此您可以直接除以(60 * 60 IE 3600)并获取时间。

根据评论中的建议,如果时间不在数小时内,我们可以使用以下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

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:

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

%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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文