コード例 #1
0
    public static DateTime CalculateDeadline(DateTime start, TimeSpan workhours)
    {
        DateTime current = new DateTime(start.Year, start.Month, start.Day, start.Hour, start.Minute, 0);

        while (workhours.TotalMinutes > 0)
        {
            DayOfWeek dayOfWeek = current.DayOfWeek;
            Workday   workday   = Workday.GetWorkday(dayOfWeek);
            if (workday == null)
            {
                DayOfWeek original = dayOfWeek;
                while (workday == null)
                {
                    current   = current.AddDays(1);
                    dayOfWeek = current.DayOfWeek;
                    workday   = Workday.GetWorkday(dayOfWeek);
                    if (dayOfWeek == original)
                    {
                        throw new InvalidOperationException("no work days");
                    }
                }
                current = current.AddHours(workday.startTime.Hour - current.Hour);
                current = current.AddMinutes(workday.startTime.Minute - current.Minute);
            }

            TimeSpan worked = Workday.WorkHours(workday, current);
            if (workhours > worked)
            {
                workhours = workhours - worked;
                // Add one day and reset hour/minutes
                current = current.Add(new TimeSpan(1, current.Hour * -1, current.Minute * -1, 0));
            }
            else
            {
                current.Add(workhours);
                return(current);
            }
        }
        return(DateTime.MinValue);
    }