Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="holidayCalendar"></param>
        /// <param name="referenceDate"></param>
        /// <param name="leadTimeInWorkdays">If positive the return date will be before the reference date</param>
        /// <returns></returns>
        public virtual DateTime FindAbsoluteWorkday(TimeSchedule holidayCalendar, DateTime referenceDate, int leadTimeInWorkdays)
        {
            if (leadTimeInWorkdays == 0)
            {
                return(referenceDate);
            }

            int dayStep;
            int dayCount;

            if (leadTimeInWorkdays < 0)
            {
                dayStep  = 1;
                dayCount = -leadTimeInWorkdays;
            }
            else
            {
                dayStep  = -1;
                dayCount = leadTimeInWorkdays;
            }

            DateTime date = referenceDate;

            for (int i = 0; dayCount > 0; --dayCount)
            {
                do
                {
                    i   += dayStep;
                    date = referenceDate.AddDays(i);
                } while (!this.IsScheduledDayAndNotAHoliday(date, holidayCalendar));
            }

            return(date);
        }
Пример #2
0
 public virtual bool IsScheduledDayAndNotAHoliday(DateTime givenDate, TimeSchedule nonworkSchedule)
 {
     if (IsScheduledDay(givenDate))
     {
         if (null == nonworkSchedule)
         {
             return(true);
         }
         if (!nonworkSchedule.IsScheduledDay(givenDate))
         {
             return(true);
         }
     }
     return(false);
 }
Пример #3
0
        /// <summary>
        /// Check whether the given date is a scheduled date or coincides with a rescheduled date
        /// (when work schedule and holiday schedule are taken into account).
        /// this.WorkCalendar and this.WorkCalendar.HolidayCalendar should be set for a rescheduled date
        /// </summary>
        /// <param name="givenDate"></param>
        /// <param name="daysToSearch">No. of days to </param>
        /// <param name="scheduledHours"></param>
        /// <returns></returns>
        public virtual bool IsScheduledDayWithin(DateTime fromDate, DateTime toDate, TimeSchedule nonworkSchedule)
        {
            TimeInterval searchInterval;

            searchInterval = this.EffectivePeriod.Intersect(fromDate, toDate);
            if (!searchInterval.IsEmpty)
            {
                for (DateTime d = searchInterval.EffectiveDate; d >= searchInterval.ExpiryDate; d = d.AddDays(1))
                {
                    if (IsScheduledDayAndNotAHoliday(d, nonworkSchedule))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Пример #4
0
        public static TimeSchedule Find(Context context, long scheduleId)
        {
            TimeSchedule schedule = context.PersistenceSession.Get <TimeSchedule>(scheduleId);

            return(schedule);
        }
Пример #5
0
        /// <summary>
        /// Return DateTime.MinValue or the latest non-holiday scheduled date within the range [fromDate, toTimestamp] of the time schedule
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public virtual DateTime FindLatestScheduledTimestampWithin(DateTime fromDate, DateTime toTimestamp, TimeSchedule holidayCalendar)
        {
            TimeInterval searchInterval = this.EffectivePeriod.Intersect(fromDate, toTimestamp);

            if (searchInterval.IsEmpty)
            {
                return(DateTime.MinValue);
            }

            TimeInterval scheduledHourInterval = null;

            for (DateTime d = searchInterval.ExpiryDate; d >= searchInterval.EffectiveDate; d = d.AddDays(-1))
            {
                ScheduleDetail sdItem;
                if (GetFirstMatchedScheduleDetailOnDate(d, out sdItem, out scheduledHourInterval) &&
                    IsNotAHoliday(holidayCalendar, d))
                {
                    break;
                }
            }
            if (null == scheduledHourInterval)
            {
                return(DateTime.MinValue);
            }
            else
            {
                return(scheduledHourInterval.EffectiveDate);
            }
        }
Пример #6
0
        ///// <summary>
        ///// Find the closest non-holiday scheduled date after thisDate
        ///// by looking forward within the number of days in daysToLookBack.
        ///// </summary>
        ///// <param name="thisDate"></param>
        ///// <param name="withinDays">The number of days to look back, must be greater 0</param>
        ///// <param name="holidayCalendar"></param>
        ///// <returns>A scheduled non-holiday date with the same time part as thisDate if found.  Otherwise TimeInterval.MinDate</returns>
        //public virtual DateTime FindClosetScheduledDateAfter(DateTime thisDate, int withinDays, TimeSchedule holidayCalendar)
        //{
        //    TimeInterval scheduledHourInterval = null;
        //    DateTime date = thisDate;
        //    for (int i = 1; i <= withinDays; ++i)
        //    {
        //        date = date.AddDays(i);
        //        ScheduleDetail sdItem;
        //        if (GetFirstMatchedScheduleDetailOnDate(date, out sdItem, out scheduledHourInterval)
        //               && IsNotAHoliday(holidayCalendar, date))
        //            break;
        //    }
        //    if (null == scheduledHourInterval)
        //        return TimeInterval.MinDate;
        //    else
        //        return scheduledHourInterval.From;
        //}

        /// <summary>
        /// Find the closest scheduled non-holiday date before (if withinDays is negative) <br/>or after (if withinDays is positive) within withinDays of thisDate.
        /// </summary>
        /// <param name="ofDate">The date from which to find the closest date</param>
        /// <param name="withinDays">The number of days (non-zero) before (if negative), or after (if positive) thisDate</param>
        /// <param name="holidayCalendar">Define the holidays to be skipped.</param>
        /// <returns>A scheduled working date with the same time part as thisDate if found.  Otherwise TimeInterval.MinDate</returns>
        public virtual DateTime FindClosetScheduledDate(DateTime ofDate, int withinDays, TimeSchedule holidayCalendar)
        {
            if (withinDays == 0)
            {
                throw new Exception("The number of days is zero.");
            }

            TimeInterval   scheduledHourInterval = null;
            DateTime       date = ofDate;
            ScheduleDetail sdItem;

            if (withinDays < 0)
            {
                for (int i = -1; i >= withinDays; --i)
                {
                    date = date.AddDays(i);
                    if (GetFirstMatchedScheduleDetailOnDate(date, out sdItem, out scheduledHourInterval) &&
                        IsNotAHoliday(holidayCalendar, date))
                    {
                        break;
                    }
                }
            }
            else
            {
                for (int i = 1; i <= withinDays; ++i)
                {
                    date = date.AddDays(i);
                    if (GetFirstMatchedScheduleDetailOnDate(date, out sdItem, out scheduledHourInterval) &&
                        IsNotAHoliday(holidayCalendar, date))
                    {
                        break;
                    }
                }
            }

            if (null == scheduledHourInterval)
            {
                return(TimeInterval.MinDate);
            }
            else
            {
                return(scheduledHourInterval.EffectiveDate);
            }
        }
Пример #7
0
 private static bool IsNotAHoliday(TimeSchedule nonworkSchedule, DateTime d)
 {
     return(null == nonworkSchedule || null == nonworkSchedule.GetScheduledHoursOn(d));
 }
Пример #8
0
 public TimeSchedule(TimeSchedule original)
 {
     this.Code        = original.Code;
     this.Title       = original.Title.Clone();
     this.Description = original.Description.Clone();
 }
Пример #9
0
        /// <summary>
        /// Find the earliest non-holiday date-time within the range [fromDate, toDate] of the time schedule
        /// starting from fromTimestamp.
        /// Otherwise returns DateTime.MinValue
        /// </summary>
        /// <param name="fromDate">fromTimestamp must be less than or equal to toDate</param>
        /// <param name="toDate">toDate must be equal to or greater than fromTimestamp</param>
        /// <param name="nonworkSchedule">Holiday schedule</param>
        /// <returns></returns>
        //

        /// <summary>
        /// fromTimestamp &lt;= toDate
        /// </summary>
        /// <param name="fromTimestamp"></param>
        /// <param name="toDate"></param>
        /// <param name="cutoffTime">cutoff time must be between workhours of the calendar</param>
        /// <param name="nonworkSchedule"></param>
        /// <returns></returns>
        public virtual DateTime FindEarliestScheduledTimestampWithin(DateTime fromTimestamp, DateTime toDate, DateTime cutoffTime, TimeSchedule nonworkSchedule)
        {
            TimeInterval searchInterval;
            DateTime     fromDate = fromTimestamp;

            if (fromTimestamp.TimeOfDay > cutoffTime.TimeOfDay)
            {
                DateTime nextDay = fromTimestamp.AddDays(1);
                fromDate = new DateTime(nextDay.Year, nextDay.Month, nextDay.Day, 0, 0, 0);
            }
            searchInterval = this.EffectivePeriod.Intersect(fromDate, toDate);

            if (searchInterval.IsEmpty)
            {
                return(DateTime.MinValue);
            }

            DateTime     scheduledTimestamp;
            TimeInterval scheduledHourInterval = null;

            for (DateTime d = searchInterval.EffectiveDate; d <= searchInterval.ExpiryDate; d = d.AddDays(1))
            {
                ScheduleDetail sdItem;
                if (this.GetFirstMatchedScheduleDetailOnDate(d, out sdItem, out scheduledHourInterval) &&
                    IsNotAHoliday(nonworkSchedule, d))
                {
                    break;
                }
            }

            if (null == scheduledHourInterval)
            {
                scheduledTimestamp = DateTime.MinValue;
            }
            else
            {
                if (fromTimestamp.Date == scheduledHourInterval.EffectiveDate.Date)
                {
                    scheduledTimestamp = fromTimestamp;
                }
                else
                {
                    scheduledTimestamp = scheduledHourInterval.EffectiveDate;
                }
            }
            return(scheduledTimestamp);
        }
Пример #10
0
        public virtual TimeInterval FindAbsoluteTimeIntervalWithRespectTo(DateTime referenceDate, TimeSchedule workCalendar, TimeSchedule holidayCalendar)
        {
            DateTime d;

            if (this.LeadDayType == DayType.BusinessDay && null != workCalendar)
            {
                d = workCalendar.FindAbsoluteWorkday(holidayCalendar, referenceDate, this.LeadDays);
            }
            else
            {
                d = referenceDate.AddDays(LeadDays);
            }

            if (this.LeadDays < 0)
            {
                //Happen after the reference date
                DateTime f = new DateTime(d.Year, d.Month, d.Day, this.From.Hour, this.From.Minute, this.From.Second);
                if (this.From.TimeOfDay > this.To.TimeOfDay)
                {
                    d = d.AddDays(1);
                }
                DateTime t = new DateTime(d.Year, d.Month, d.Day, this.To.Hour, this.To.Minute, this.To.Second);
                return(new TimeInterval(f, t));
            }
            else
            {
                //Happen earlier than the reference date
                DateTime t = new DateTime(d.Year, d.Month, d.Day, this.To.Hour, this.To.Minute, this.To.Second);
                if (this.From.TimeOfDay > this.To.TimeOfDay)
                {
                    d = d.AddDays(-1);
                }
                DateTime f = new DateTime(d.Year, d.Month, d.Day, this.From.Hour, this.From.Minute, this.From.Second);
                return(new TimeInterval(f, t));
            }
        }