Transform rule to get days difference between today's date and past date

Problem

We often get use cases where we want the difference between today’s date and a past or future date. Current transforms in IdentityNow do not give us the difference between 2 dates in days. There are workarounds to convert date into epoch, calculate the difference, and convert that number back into days, but those are hacky and not clean.

Diagnosis

The purpose of this knowledge article is not just to share this simple rule but also to start a conversation about whether Sailpoint Professional Services should just build a standard rule for all tenants that can be used by all customers so that all those repetitive hacky workarounds are not needed.

Solution

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE Rule PUBLIC "sailpoint.dtd" "sailpoint.dtd">
<Rule name="DaysDifferenceGeneric" type="Transform">
    <Description>Gets date input from transform and returns days diffrence to today's date</Description>
    <Source><![CDATA[
        import java.util.Date;
        import sailpoint.object.Identity;
        import java.text.ParseException;
        import java.text.SimpleDateFormat;

            log.error("Inside-DaysDifferenceGeneric-rule: started");

            String lastLogin=date;
            if(lastLogin == null) {
                log.error("Inside-DaysDifferenceGeneric-rule: lastlogin is null");
                return null;
            }
            log.error("Inside-DaysDifferenceGeneric-rule: lastlogin value in string "+lastLogin );


            try {
                // Define the date format
                SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
                // Parse the input string into a Date object
                Date staticDate = isoFormat.parse(lastLogin);
                log.error("Inside-DaysDifferenceGeneric-rule: lastlogin value in date format "+staticDate );
                // Current date
                Date currentDate = new Date();

                // Calculate the difference in milliseconds
                long diffInMillies = Math.abs(currentDate.getTime() - staticDate.getTime());

                // Calculate different
                // Calculate different time units
                long diffInDays = diffInMillies / (1000 * 60 * 60 * 24);
                return String.valueOf(diffInDays);


            } catch (ParseException e) {
                throw new Exception("Invalid date format. Please use ISO 8601 format.");
            }
        ]]></Source>
</Rule>
1 Like

I don’t know, I think my transform logic to handle this is pretty clean :stuck_out_tongue: (converting the date to yyMMddHH format and then using Velocity to do the math)…

That said, I have a submitted Idea in the Ideas Portal to have the Velocity dateTool functions (and numberTool functions) added to Transforms. These are already in play in email templates, but not in Transforms. Please throw an upvote on that Idea to simplify this process in Transforms directly.

But nice work on the Rule as another way to solve this problem!

Getting things in product takes time, that is the reason it is good to have standard/generic rule deployed for all tenants so everyone can start using it.
I assume deploying rule would not be as difficult as building new product functionality