Esempio n. 1
0
    public void Should_respect_custom_non_working_day_definition()
    {
        var someDate = new DateOnly(2022, 3, 17); // THU
        var nwd      = new NonWorkingDays {
            CustomNonWorkingsDays = new List <DateOnly> {
                someDate
            }
        };
        var isWorkingDayBefore  = someDate.IsWorkingDay();
        var isNoWorkingDayAfter = !someDate.IsWorkingDay(nwd);
        var respectsCustomNonWorkingDayDefinition = isWorkingDayBefore && isNoWorkingDayAfter;

        Assert.True(respectsCustomNonWorkingDayDefinition);
    }
Esempio n. 2
0
    public static DateOnly GetXthWorkingDay(this DateOnly thisDate, int x = 1, bool startFromEnd = false, NonWorkingDays?nonWorkingDays = null)
    {
        var runningTotalOfWorkingDays = 0;
        var DAY = 0;

        if (startFromEnd)
        {
            DateOnly testDate;
            var      endOfMonth = thisDate.lastDay();
            DAY = endOfMonth.Day;
            while (runningTotalOfWorkingDays < x)
            {
                try
                {
                    testDate = new DateOnly(thisDate.Year, thisDate.Month, DAY);
                }
                catch (ArgumentOutOfRangeException)
                {
                    throw new ArgumentOutOfRangeException(nameof(DAY), $"There aren't {DAY} days in {thisDate.Year}/{thisDate.Month}");
                }
                var isWorkingDay = testDate.IsWorkingDay(nonWorkingDays);
                if (isWorkingDay)
                {
                    runningTotalOfWorkingDays++;
                }
                if (runningTotalOfWorkingDays < x)
                {
                    DAY--;
                }
            }
        }
        else
        {
            DateOnly testDate;
            do
            {
                DAY++;
                try
                {
                    testDate = new DateOnly(thisDate.Year, thisDate.Month, DAY);
                }
                catch (ArgumentOutOfRangeException)
                {
                    throw new ArgumentOutOfRangeException(nameof(DAY), $"There aren't {DAY} days in {thisDate.Year}/{thisDate.Month}");
                }

                var isWorkingDay = testDate.IsWorkingDay(nonWorkingDays);
                if (isWorkingDay)
                {
                    runningTotalOfWorkingDays++;
                }
            } while (runningTotalOfWorkingDays != x);
        }
        return(new DateOnly(thisDate.Year, thisDate.Month, DAY));
    }