Thursday, October 17, 2024

OIC - Convert DateTime from one timezone to another with Daylight saving in OIC

Usecase: 

Here, we will demonstrate how to convert one timezone to another considering the daylight saving time.

Here, we will see UTC to Australia timezone change.

Implementation steps:

  1. Create 2 javascript functions
    1. 1st one will determine whether the datetime under DST or not. Response will be in True or False.
    2. 2nd one will add UTC +11.00  or UTC+10.00 depends on the daylight saving.
  2. Import it to OIC library
  3. Create an integration and feed a datetime
  4. Feed the input date time to javascript and get the the converted timezone from the integration.

Javascript code used: (save .js extension)

function isDaylightSavingsInEffect(dateTimeInput) {

    var dateTime = new Date(dateTimeInput);

    var month = dateTime.getMonth();

    month = month + 1;

    var year = dateTime.getFullYear();

    var day = dateTime.getDate();

    var hours = dateTime.getHours();

    var firstDateofSunday = 0;

    var flag = false;

   // get first Sunday of any Month

    var firstSunday = new Date(year, month - 1, 1);

    var dayOfWeek = firstSunday.getDay();

    if (dayOfWeek == 0) {

        firstDateofSunday = 1;

    } else {

        firstDateofSunday = 7 - (dayOfWeek - 1);

    }

    // return for DST Eligibility

    if (month < 4 || month > 10 || (month == 10 && day >= firstDateofSunday) || (month == 4 && day <= firstDateofSunday)) {

        flag = true;

    } else {

        flag = false;

    }

    return flag;

}

// returns date and time YYYY-MM-DD HH:MM:SS 
function sc_convertUTC2AEST (utcDate) {
var inDate = new Date(utcDate);
var offset = 0;
var response = '';
if (!isNaN(inDate)) {
offset = isDaylightSavingsInEffect(inDate) ? 11 : 10; // Using your function to decide offset
inDate.setHours(inDate.getHours() + offset);
string ='';
response= string.concat(inDate.getUTCFullYear(), '-', (('0' + (inDate.getUTCMonth() + 1)).slice(-2)),'-', ('0' + inDate.getUTCDate()).slice(-2),' ', ('0' +
inDate.getUTCHours()).slice(-2), ('0' + inDate.getUTCMinutes()).slice(-2), ('0' + inDate.getUTCSeconds()).slice(-2))
}
return response;
}

Detailed screenshots:

Js Codes:



Testing:



Import .js file to libraries and save.


Create a integration and take js action and choose the javascript 2nd function >> feed the input UTC date time.



Map the js action response.


Note: Instead of using Javascript action, we can also call the function from xslt mapper.


No comments:

Post a Comment

Featured Post

OIC - how can I use XSLT functions to remove leading zeros from numeric and alphanumeric fields?

To remove leading zeros from an numeric field in Oracle Integration Cloud (OIC) using XSLT, you can Use number() Function The number() funct...