Пример #1
0
 public static DateTime GetPreviousWorkingDay(DateTime date)
 {
     do
     {
         date = date.AddDays(-1);
     }while (!CalendarCache.IsWorkingDay(date));
     return(date);
 }
Пример #2
0
        public static List <DateTime> GenerateDateList(DateTime first, DateTime endDate,
                                                       int timeSpan, TimeSpanUnit timeSpanUnit, TemplateTimeType dateType, bool isForward, bool ignoreReduplicateDays)
        {
            if (timeSpan < 1)
            {
                throw new ApplicationException("Time span can't be 0");
            }

            var dateList = new List <DateTime>();

            DateTime date = first;

            for (int i = 0; ; ++i)
            {
                //避免每个月日期数不同导致的的向下取整问题
                //e.g.
                //(20160131).AddMonths(1).AddMonths(1) = 20160329
                //(20160131).AddMonths(2) = 20160331
                var interval = timeSpan * i;
                switch (timeSpanUnit)
                {
                case TimeSpanUnit.Year:
                    date = first.AddYears(interval);
                    break;

                case TimeSpanUnit.Month:
                    date = first.AddMonths(interval);
                    break;

                case TimeSpanUnit.Day:
                    date = first.AddDays(interval);
                    break;
                }

                if (dateType == TemplateTimeType.TradingDay && !CalendarCache.IsTradingDay(date))
                {
                    date = isForward ? GetPreviousTradingDay(date) : GetNextTradingDay(date);
                }
                else if (dateType == TemplateTimeType.WorkingDay && !CalendarCache.IsWorkingDay(date))
                {
                    date = isForward ? GetPreviousWorkingDay(date) : GetNextWorkingDay(date);
                }

                if (date > endDate)
                {
                    break;
                }

                bool isIgnore = false;
                isIgnore = ignoreReduplicateDays && dateList.Contains(date);
                if (!isIgnore)
                {
                    dateList.Add(date);
                }
            }

            return(dateList);
        }
Пример #3
0
        private static DateTime AddWorkingDays(this DateTime date, int nDays)
        {
            bool isAdd = nDays > 0;
            bool isSub = nDays < 0;
            var  begin = date;
            var  end   = date;

            int nWorkingDay = 0;

            do
            {
                int nDeltaDay = Math.Abs(nDays) - nWorkingDay;
                if (isAdd)
                {
                    end = end.AddDays(nDeltaDay);
                }
                else if (isSub)
                {
                    begin = begin.AddDays(0 - nDeltaDay);
                }

                // For example: t-1 -> (endDate - beginData).workdays = 2
                var n = CalendarCache.NumOfWorkingDay(begin, end);
                if (isAdd)
                {
                    n -= CalendarCache.IsWorkingDay(begin) ? 1 : 0;
                }
                else if (isSub)
                {
                    n -= CalendarCache.IsWorkingDay(end) ? 1 : 0;
                }
                nWorkingDay = n;
            }while (nWorkingDay < Math.Abs(nDays));

            return(nDays > 0 ? end : begin);
        }