imports/vx/both/code/util.js
/**
     * For scheduling daemon jobs and reports, given a timeInterval, timeUnit, timeOption, timezone and
     * optional lastDate, compute and return the nextDate that the process should run.
     *
     * @param {number} timeInterval Time interval.
     * @param {string} timeUnit Time unit (see codes.timeUnit and also supports "SECOND" undocumented).
     * @param {string} timeOption Time option (see codes.timeOptions).
     * @param {string} Optional timezone in IANA format.
     * @param {date} Optional last date.
     * @return {date} Next date that the process should execute.
     */
For scheduling daemon jobs and reports, given a timeInterval, timeUnit, timeOption, timezone and
optional lastDate, compute and return the nextDate that the process should run.

@param {number} timeInterval Time interval.
@param {string} timeUnit Time unit .
@param {string} timeOption Time option .
@param {string} Optional timezone in IANA format.
@param {date} Optional last date.
@return {date} Next date that the process should execute.
For scheduling daemon jobs and reports given a timeInterval timeUnit timeOption timezone and optional lastDate compute and return the nextDate that the process should run . @param { number } timeInterval Time interval . @param { string } timeUnit Time unit . @param { string } timeOption Time option . @param { string } Optional timezone in IANA format . @param { date } Optional last date . @return { date } Next date that the process should execute .
computeNextDate(timeInterval, timeUnit, timeOption, timezone, lastDate) {
        lastDate = lastDate || new Date()
        const lastMoment = moment.tz(lastDate, timezone)
        const nextMoment = lastMoment.clone()
        if (timeUnit === "SECOND") {
            nextMoment.add(timeInterval, "seconds")
        }
        if (timeUnit === "MINUTE") {
            nextMoment.startOf("minute")
            nextMoment.add(timeInterval, "minutes")
            if (timeOption === "BOTTOM") {
                nextMoment.add(30, "seconds")
            }
        }
        if (timeUnit === "HOUR") {
            nextMoment.startOf("hour")
            nextMoment.add(timeInterval, "hours")
            if (timeOption === "BOTTOM") {
                nextMoment.add(30, "minutes")
            }
        }
        if (timeUnit === "DAY") {
            nextMoment.startOf("day")
            nextMoment.add(timeInterval, "days")
            if (timeOption === "SIXAM") nextMoment.add(6, "hours")
            if (timeOption === "NOON") nextMoment.add(12, "hours")
            if (timeOption === "SIXPM") nextMoment.add(18, "hours")
            if (timeOption === "MIDNIGHT") nextMoment.add(24, "hours")
        }
        if (timeUnit === "WEEK") {
            nextMoment.startOf("day")
            nextMoment.add(timeInterval, "weeks")
            if (timeOption === "SUNDAY") nextMoment.day(0)
            if (timeOption === "MONDAY") nextMoment.day(1)
            if (timeOption === "TUESDAY") nextMoment.day(2)
            if (timeOption === "WEDNESDAY") nextMoment.day(3)
            if (timeOption === "THURSDAY") nextMoment.day(4)
            if (timeOption === "FRIDAY") nextMoment.day(5)
            if (timeOption === "SATURDAY") nextMoment.day(6)
        }
        if (timeUnit === "MONTH") {
            nextMoment.startOf("month")
            nextMoment.add(timeInterval, "months")
            if (timeOption === "FIRSTDAY") nextMoment.startOf("month").startOf("day")
            if (timeOption === "LASTDAY") nextMoment.endOf("month").startOf("day")
            if (timeOption === "FIRSTMONDAY") {
                nextMoment.startOf("month").startOf("day")
                while (nextMoment.day() !== 1) {
                    nextMoment.add(1, "day")
                }
            }
            if (timeOption === "LASTFRIDAY") {
                nextMoment.endOf("month").startOf("day")
                while (nextMoment.day() !== 5) {
                    nextMoment.subtract(1, "day")
                }
            }
        }
        return nextMoment.toDate()
    }
computeNextDate ( timeInterval , timeUnit , timeOption , timezone , lastDate ) { lastDate = lastDate || new Date ( ) const lastMoment = moment . tz ( lastDate , timezone ) const nextMoment = lastMoment . clone ( ) if ( timeUnit === "SECOND" ) { nextMoment . add ( timeInterval , "seconds" ) } if ( timeUnit === "MINUTE" ) { nextMoment . startOf ( "minute" ) nextMoment . add ( timeInterval , "minutes" ) if ( timeOption === "BOTTOM" ) { nextMoment . add ( 30 , "seconds" ) } } if ( timeUnit === "HOUR" ) { nextMoment . startOf ( "hour" ) nextMoment . add ( timeInterval , "hours" ) if ( timeOption === "BOTTOM" ) { nextMoment . add ( 30 , "minutes" ) } } if ( timeUnit === "DAY" ) { nextMoment . startOf ( "day" ) nextMoment . add ( timeInterval , "days" ) if ( timeOption === "SIXAM" ) nextMoment . add ( 6 , "hours" ) if ( timeOption === "NOON" ) nextMoment . add ( 12 , "hours" ) if ( timeOption === "SIXPM" ) nextMoment . add ( 18 , "hours" ) if ( timeOption === "MIDNIGHT" ) nextMoment . add ( 24 , "hours" ) } if ( timeUnit === "WEEK" ) { nextMoment . startOf ( "day" ) nextMoment . add ( timeInterval , "weeks" ) if ( timeOption === "SUNDAY" ) nextMoment . day ( 0 ) if ( timeOption === "MONDAY" ) nextMoment . day ( 1 ) if ( timeOption === "TUESDAY" ) nextMoment . day ( 2 ) if ( timeOption === "WEDNESDAY" ) nextMoment . day ( 3 ) if ( timeOption === "THURSDAY" ) nextMoment . day ( 4 ) if ( timeOption === "FRIDAY" ) nextMoment . day ( 5 ) if ( timeOption === "SATURDAY" ) nextMoment . day ( 6 ) } if ( timeUnit === "MONTH" ) { nextMoment . startOf ( "month" ) nextMoment . add ( timeInterval , "months" ) if ( timeOption === "FIRSTDAY" ) nextMoment . startOf ( "month" ) . startOf ( "day" ) if ( timeOption === "LASTDAY" ) nextMoment . endOf ( "month" ) . startOf ( "day" ) if ( timeOption === "FIRSTMONDAY" ) { nextMoment . startOf ( "month" ) . startOf ( "day" ) while ( nextMoment . day ( ) !== 1 ) { nextMoment . add ( 1 , "day" ) } } if ( timeOption === "LASTFRIDAY" ) { nextMoment . endOf ( "month" ) . startOf ( "day" ) while ( nextMoment . day ( ) !== 5 ) { nextMoment . subtract ( 1 , "day" ) } } } return nextMoment . toDate ( ) }
For scheduling daemon jobs and reports, given a timeInterval, timeUnit, timeOption, timezone and
 optional lastDate, compute and return the nextDate that the process should run.
For scheduling daemon jobs and reports given a timeInterval timeUnit timeOption timezone and optional lastDate compute and return the nextDate that the process should run .
//OLog.debug("util.js computeNextDate lastMoment="+lastMoment.format()+" using "+timeInterval+" "+timeUnit+" "+timeOption+" "+timezone)