donderdag 5 augustus 2010

WorkCalendarDate::findDate

A new useful method on Table WorkCalendarDate:

\Data Dictionary\Tables\WorkCalendarDate\Methods\findDate


/// <summary>
/// Searches the nth day of type _workTimeControl, forward or backwards from _startDate
/// </summary>
/// <param name="_calendarId">
/// The calendar to use to look for open days
/// </param>
/// <param name="_lookAheadDays">
/// Search for the nth day (default 0)
/// positive value = search forward
/// negative value = search backwards
/// </param>
/// <param name="_startDate">
/// Start looking from this date (default: system date)
/// </param>
/// <param name="_forceReturnDate">
/// What should be returned if no date was found?
/// true  = _startdate is returned
/// false = no date is returned (default)
/// </param>
/// <param name="_workTimeControl">
/// Look for which type of days?
/// Open (default)
/// Closed
/// UseBasic
/// </param>
/// <returns>
/// The nth day (_lookAheadDays) of type _workTimeControl, starting from _startDate
/// </returns>
/// <remarks>
/// location = \Data Dictionary\Tables\WorkCalendarDate\Methods\findDate
/// </remarks>
static TransDate findDate(
    CalendarId          _calendarId,
    Counter             _lookAheadDays   = 0,
    TransDate           _startDate          = systemDateGet(),
    boolean             _forceReturnDate    = false,
    WorkTimeControl     _workTimeControl    = WorkTimeControl::Open)
{
    WorkCalendarDate workCalendarDate;
    Counter          counter = 0;
    ;
    
    if (_lookAheadDays >= 0) //search forward from _startDate
    {
        while
        select workCalendarDate
            order by TransDate
        where workCalendarDate.CalendarId      == _calendarId
           && workCalendarDate.TransDate       >= _startDate
           && workCalendarDate.WorkTimeControl == _workTimeControl
        {
            if (counter >= _lookAheadDays)
                return workCalendarDate.TransDate;
            counter++;
        }
    }
    else                        //search backward from _startDate
    {
        while
        select workCalendarDate
            order by TransDate DESC
        where workCalendarDate.CalendarId      == _calendarId
           && workCalendarDate.TransDate       <= _startDate
           && workCalendarDate.WorkTimeControl == _workTimeControl
        {
            if (counter <= _lookAheadDays)
                return workCalendarDate.TransDate;
            counter--;
        }
    }

    //no date found
    if (_forceReturnDate)
        return _startDate;
    else
        return dateNull();
}


Example Job:

static void WorkCalendarDate_findDate(Args _args)
{
    CalendarId          calendarId;
    TransDate           startDate;
    TransDate           nextDate;
    Counter             offsetDays;
    Name                startDateName;
    Name                nextDateName;
    boolean             forceReturnDate;
    WorkTimeControl     workTimeControl;
    ;
    //Example 1:
    //Search 3 days forward in calendar STD, starting from 05 AUG 2010 (thursday), looking for Open days
    calendarId      = "STD";
    startDate       = str2date("05/08/2010",123);
    offsetDays      = 3;
    forceReturnDate = false;
    workTimeControl = WorkTimeControl::Open;
    
    nextDate        = WorkCalendarDate::findDate(
                        calendarId, 
                        offsetDays,
                        startDate, 
                        forceReturnDate, 
                        workTimeControl);
                        
    startDateName   = dayname(dayofwk(startDate));
    nextDateName    = dayname(dayofwk(nextDate));
    global::enum2int(
    info(strfmt("Search %1 days, starting from %2(%3), with an offset of %4 days = %5(%6)",
            workTimeControl,
            startDate,
            startDateName,
            offsetDays,
            nextDate,
            nextDateName)));
    //output:
    //Search Openen days, starting from 5/08/2010(thursday), with an offset of 3 days = 10/08/2010(tuesday)

    //Example 2:
    //Search 3 days backwards in calendar STD, starting from 11 AUG 2010 (wednesday), looking for Open days
    calendarId      = "STD";
    startDate       = str2date("11/08/2010",123);
    offsetDays      = -3;
    forceReturnDate = false;
    workTimeControl = WorkTimeControl::Open;
    
    nextDate        = WorkCalendarDate::findDate(
                        calendarId, 
                        offsetDays,
                        startDate, 
                        forceReturnDate, 
                        workTimeControl);
                        
    startDateName   = dayname(dayofwk(startDate));
    nextDateName    = dayname(dayofwk(nextDate));
    global::enum2int(
    info(strfmt("Search %1 days, starting from %2(%3), with an offset of %4 days = %5(%6)",
            workTimeControl,
            startDate,
            startDateName,
            offsetDays,
            nextDate,
            nextDateName)));
    //output:
    //Search Openen days, starting from 11/08/2010(wednesday), with an offset of -3 days = 6/08/2010(friday)
}

Geen opmerkingen:

Een reactie posten