AVIO Consulting

BPEL Date & Duration Manipulation

May 16, 2012 | BPEL, BPM

Dates are a common type seen in XML documents.  Many times when working on a project, the schema will use a date field but have it set to a string type.  When a date comes into the process as a string, the representation is not standardized and can come in a variety of formats.  In order to perform date manipulation like checking the time since a date or adding duration to a date the string should be converted to a date.  The following are a set of examples that illustrate manipulation of a date field used in XLST.

String to Date Conversion

Once the element is a date type, other functions can be applied to the field. 

Example of a typical date provided as a string

<ns0:appDateyyyymmdd>20120515</ns0:appDateyyyymmdd>

Date data types use the following form “YYYY-MM-DD”.  To convert the string representation to a date, concatenate each component of the date adding the dashes.  Also, note that the appDate would by type date as specified in the xsd schema, for example <xs:element name=”appDate”/>

<ns1:appDate>

   <xsl:value-of select=’concat(substring(ns0:appDateyyyymmdd,1.0,4.0), “-“, substring(ns0:appDateyyyymmdd,5.0,2.0), “-“, substring(ns0:appDateyyyymmdd,7.0,2.0))’/>

</ns1:appDate>

 

Results – YYYY-MM-DD 

   <ns1:appDate>2012-05-15</ns1:appDate>

 

Date to String Conversion

There may be cases where the date needs to be returned as a string. This will convert the date to a string of a specified format.

<xsl:value-of select=”xp20:format-dateTime(string(xp20:current-dateTime()),'[M01][D01] [Y0001] ‘)”/>

Results - MMDDYYYY 

   05152012 

Here’s another example that includes the time component of a date.

<tns2:communReqDt> 

  <xsl:value-of select='xp20:format-dateTime(xp20:current-dateTime(),"[M01]/[D01]/[Y0001] [h]:[m01]:[s01] [PN]")'/> 

</tns2:communReqDt>

Results 

<tns2:communReqDt>10/24/2011 5:13:31 PM</tns2:communReqDt> 

 

Date Comparison using a Duration

Once the element is a date type, it can be used in a comparison. This example compares to see if appDate happened in the last 49 days.

“xp20:current-date() – bpws:getVariableData(‘Eligibility_Out’,’payload’,’/ns1:appDate’)  <= ‘P49D’”

 

Adding a duration to a Date

Adding time to a date to come up with a future date is also a common need.  This example adds 11 months to the AcctEffDt.

xp20:add-dayTimeDuration-to-dateTime(string(bpws:getVariableData 

       ('Eligibility_Out','payload','/ns3:response/ns3:AcctEffDt')), string('P11M'))

 

Date to Duration

Duration is a time period and can be obtained by calculating the differences of two dates.  Sometimes there may be a more complicated requirement. For example, the duration needed to be calculated differently based on the time the request was submitted to a batch process. The resulting duration was used on a wait.

Using the hour of the day to create a duration variable (49 – (current hour of the day 22:00 (10PM))) = “PT27H”.   If this is used when the hour is >= 19:00 & < 24:00 it would return an hour range of 30-25.

concat("PT", string(49 - xp20:hours-from-dateTime(xp20:current-dateTime())), "H")

 

Results 

   f(19:00) = "PT30H" 

   f(23:00) = "PT26H"

In an ideal world, date values for all services provided or consumed would be date types and conversion from one string format to another wouldn’t be required.  But with these guidelines we can still get our work done.

References:

http://www.w3schools.com/schema/schema_dtypes_date.asp

http://www.w3.org/TR/xslt20/#function-format-dateTime