1

I am working with WSO2 ESB and i would like to know how can i get secondes betwenn date A and B. Basically, i would like to calcul time between system time and 12.00pm. Do you have any idea ? I found how to do get number of days but nothing for secondes with Xpath only. I am restricted with Xpath because i can't use a Java script.

Thank you.

Community
  • 1
  • 1
Robyn.D
  • 339
  • 2
  • 20
  • 2
    See if this answers your question: https://stackoverflow.com/questions/38604414/finding-the-difference-between-two-datetimes-in-xslt If not, add more details. – michael.hor257k Mar 25 '19 at 14:17
  • It gave me some interesting functions but not what i am looking for. Thank you for your time, Ophychius found what i was looking for – Robyn.D Mar 26 '19 at 08:41
  • This answer there https://stackoverflow.com/a/38607378/3016153 is identical to the one you have accepted here. – michael.hor257k Mar 26 '19 at 08:56
  • Ophychius answer is more understandable for me, sorry if my question is a duplication. – Robyn.D Mar 26 '19 at 13:01

1 Answers1

1

The basic is to turn both into proper xs:dateTime objects, then calculate the difference. This will give you a dayTimeDuration. Divide this by the dayTimeDuration of 1 second and you have your difference in seconds. Example:

<property xmlns:xs="http://www.w3.org/2001/XMLSchema" name="now" 
expression="concat(get-property('SYSTEM_DATE', 'yyyy-MM-dd'),'T',get-property('SYSTEM_DATE', 'HH:mm:ss')) "/>
<property xmlns:xs="http://www.w3.org/2001/XMLSchema" name="12pm" 
expression="concat(get-property('SYSTEM_DATE', 'yyyy-MM-dd'),'T12:00:00')"/>


<property xmlns:xs="http://www.w3.org/2001/XMLSchema" name="diff" 
expression="xs:dayTimeDuration(xs:dateTime(syn:get-property('now'))-xs:dateTime(syn:get-property('12pm'))) div xs:dayTimeDuration('PT1S')"/>

Keep in mind you need to enable xpath 2.0 for this on the ESB/EI. You can do this by uncommenting the following line in your [WSO2CARBON_HOM]/repository/conf/synapse.properties file.

#synapse.xpath.dom.failover.enabled=true

Also keep in mind that once you have done that, xpath expression where you combine functions from synapse namespace (like get-property) with normal xpath functions will require the use of a prefix like in the code example.

ophychius
  • 2,623
  • 2
  • 27
  • 49