Mapping to a UTC timestamp with milliseconds in BusinessWorks
Facts - Tibco
Thursday, 25 August 2011 20:27

This note is for Tibco BusinessWorks 5.8.

For a project I have the requirement to send a UTC timestamp in the following format:

[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}Z

Or in different notation: "yyyy-MM-ddTHH:mm:ss.SSSZ".

An example is "2011-08-23T20:34:55.492Z". The problem with this timestamp is in the milliseconds part. To map to this format I made the following code in a BusinessWorks mapper:

$timestampInMilliSeconds=tib:timestamp()

$timestampInSeconds=floor(tib:timestamp() div 1000)

$millSecondsPart = $timestampInMilliSeconds mod 1000

$dateTimeTS=tib:add-to-dateTime(
  tib:create-dateTime(1970, 1, 1, 0, 0, 0, $timestampInMilliSeconds -1000*$timestampInSeconds), 
    0, 0, 0, 0, 0, $timestampInSeconds
)

$formattedUTC_TS=concat(
  tib:format-dateTime('yyyy-MM-dd', $dateTimeTS),
  'T',
  tib:format-dateTime('HH:mm:ss', $dateTimeTS), '.',
  tib:pad-front($milliSecondsPart, 3, '0'),
  'Z'
)

If you use formatting 'HH:mm:ss.SSS' instead of 'HH:mm:ss' and just concatenate the "Z" then the milliseconds part occasionally contains only one or two digits, which results in a formatting error. The third mapping, to $formattedUTC_TS, overcomes this problem.