Archive for the ‘SCPI’ Category

SCPI – Covert DateTime to Epoch (Unix time)

June 15, 2021

Upserting to OData entities in Successfactors need the date to be in the Unix (Epoch) time format. The normal datetime value can be changed to Unix time either by groovy script, or by XSLT. For eg, think the input XML like this :

<Input>
   <UnixTime>1444150760</UnixTime>
   <ISODateTime>2015-10-06T16:59:20.555</ISODateTime>
</Input> 

The conversion can be done with XSLT like this :

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/Input">
    <output>
		<ISODateTime>
			<xsl:value-of select="xs:dateTime('1970-01-01T00:00:00') + xs:dayTimeDuration(concat('PT', UnixTime, 'S'))"/>
		</ISODateTime>
		<UnixTimestamp>
			<xsl:value-of select="floor((xs:dateTime(ISODateTime) - xs:dateTime('1970-01-01T00:00:00')) div xs:dayTimeDuration('PT1S')) "/>
		</UnixTimestamp>
	</output>
</xsl:template> 

</xsl:stylesheet>

The epoch basically, is the seconds after the date 01.01.1970. So apparently, the calculation includes taking the difference.

Thats it, happy coding!

SCPI – Add days with TimeZone

June 11, 2021

We may need to create a date value in header/property, which could we use in the filter at a later point.
Can be easily achieved using groovy script. Usually its very easily with normal java util class. But problems come when we have to add this based on a particular timezone. Code below :

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.time.ZonedDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter;

def Message processData(Message message) {    
    
    ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/Berlin"));	
    futureLimitDate = now.plusDays(30).format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"));

    println futureLimitDate

    message.setProperty("futureLimitDate", futureLimitDate);
    return message;
}

That’s it. Happy Coding. :B

SCPI – Get current time in different Timezone

June 11, 2021

The current datetime can be easily retrieved and assigned to a property or header with the Content Modifier Step like this. Use a Camel Expression like these :

${date:now:yyyy-MM-dd’T’00:00:00’Z’}

or

${date:now:yyyy-MM-dd}

based on the date format you need.

But the problem comes when you need the time in different timezone. For eg, I wanted time in CET, but the camel expression always gives the time in UTC zone. This could be tackled with a groovy script to convert the time into any desired timezone.

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.time.ZonedDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter;

def Message processData(Message message) {    
    
    ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/Berlin"));	
    todayTZ = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'00:00:00'Z'"));
    today = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

    println today
    println todayTZ
    println futureLimitDate

    message.setProperty("todayTZ", todayTZ);
    message.setProperty("today", today);
    return message;
}

That’s it. Println is just for debugging in the IDE console, you can remove it.

Happy Coding. 😉