Function to fetch the Start time and End time for saved search based on the availability of the processor.

Case: When we schedule a script to run every 6 hours, then it will run at 12 am, 6 am, 12 pm, and 6 pm. when the script runs at 6 am, then we need the data from the time 12 am up to 5.59 am. But in case of priority, the scheduler runs at 7 am. so when the script is initiated between 6 am to 12 pm, we need the data in the range of 12 am up to 5.59 am. In this case, we created a time window function to fetch this time 12 am and 5.59 am.

timeWindow: function (time) {
    var currentTime = new Date();
    var currenthour = currentTime.getHours();
    var timeObj = {};
    var year = currentTime.getFullYear();
    var month = currentTime.getMonth();
    var date = currentTime.getDate();

    if ((currenthour >= 0) && currenthour < 6) {
        var today = new Date();
        var previousDay = (today.getDate())-1;
        var startDateAndTime = new Date(year, month, previousDay, 18, 00, 00);
        var endDate = new Date(year, month, previousDay, 23, 59, 59);
    }
    else if (currenthour >= 6 && currenthour < 12) {
        var startDateAndTime = new Date(year, month, date, 0, 00, 00);
        var endDate = new Date(year, 1, date, 05, 59, 59);
    }
    else if (currenthour >= 12 && currenthour < 18) {
        var startDateAndTime = new Date(year, month, date, 06, 00, 00);
        var endDate = new Date(year, 1, date, 11, 59, 59);
    }
    else if (currenthour >= 18 && currenthour < 24) {
        var startDateAndTime = new Date(year, month, date, 12, 00, 00);
        var endDate = new Date(year, 1, date, 17, 59, 59);
    }

    var companyInfo = config.load({
        type: config.Type.COMPANY_INFORMATION
    });
    var TIMEZONE = companyInfo.getValue({
        fieldId: 'timezone'
    });
    log.debug('TIMEZONE', TIMEZONE)

    var formatStartDate = format.format({
        value: startDateAndTime,
        type: format.Type.DATE,
        timezone: format.Timezone.TIMEZONE
    });

    var formatStartTime = format.format({
        value: startDateAndTime,
        type: format.Type.TIMEOFDAY,
        timezone: format.Timezone.TIMEZONE
    });
    var formatEndDate = format.format({
        value: endDate,
        type: format.Type.DATE,
        timezone: format.Timezone.TIMEZONE
    });

    var formatEndTime = format.format({
        value: endDate,
        type: format.Type.TIMEOFDAY,
        timezone: format.Timezone.TIMEZONE
    });
    var actualStartDateAndTime = formatStartDate + ' ' + formatStartTime
    var actualEndDateAndTime = formatEndDate + ' ' + formatEndTime

    timeObj.startTimeAndDate = actualStartDateAndTime;
    timeObj.endTimeAndDate = actualEndDateAndTime
    return timeObj
}

Here, we convert the time to the account-specific time and date using the ‘Format’ module. this ‘startTimeAndDate’ and ‘endTimeAndDate’ are passed to the saved search to fetch the data in between this time period.

The script is selected the time window based on the current execution time. When the current time is 1.00 pm, then it will enter into the ‘(currenthour >= 12 && currenthour < 18)’ time period, return the value as

        var startDateAndTime = new Date(year, month, date, 06, 00, 00);
        var endDate = new Date(year, 1, date, 11, 59, 59);

If the execution time is 2 am, the script will take the previous day’s date and time range.

Leave a comment

Your email address will not be published. Required fields are marked *