A newer version of this page is available. Switch to the current version.

FreeTimeCalculator.FindFreeTimeInterval(TimeInterval, TimeSpan, Boolean) Method

Performs a search for the nearest available time slot with the specified duration within the specified interval.

Namespace: DevExpress.XtraScheduler.Tools

Assembly: DevExpress.XtraScheduler.v18.2.Core.dll

Declaration

public TimeInterval FindFreeTimeInterval(
    TimeInterval interval,
    TimeSpan duration,
    bool forward
)
Public Function FindFreeTimeInterval(
    interval As TimeInterval,
    duration As TimeSpan,
    forward As Boolean
) As TimeInterval

Parameters

Name Type Description
interval TimeInterval

A TimeInterval object, representing the time period in which the search is performed.

duration TimeSpan

A TimeSpan structure, which represents the duration of a time slot to find.

forward Boolean

If true, the search starts at the interval.Start and continues forward in time. If false, it starts at interval.End, and continues backwards.

Returns

Type Description
TimeInterval

A TimeInterval object, representing the interval which meets the conditions, or the TimeInterval.Empty value, if an interval is not found.

Remarks

The FindFreeTimeInterval method searches for the nearest free interval with a specified minimum duration within the specified period. Note that FindFreeTimeInterval method requires the time interval converted to server time zone, i.e. you should use the TimeZoneHelper.FromClientTime method to adjust the time interval passed as a parameter.

The following code sample illustrates the implementation of the method used to find a free time slot which has a specified duration, or is longer than that, before the end of the current work week.

The following code sample illustrates the implementation of the method used to find a free time slot which has a specified duration, or is longer than that, before the end of the current work week.

using DevExpress.XtraScheduler;
using DevExpress.XtraScheduler.Tools;
using System;
using System.Windows.Forms;

namespace FreeTimeIntervals
{
    public partial class Form1 : Form {
        TimeZoneHelper timeZoneHelper;
        // Specify non-working time interval.
        TimeOfDayInterval nonWorkingTime = new TimeOfDayInterval(TimeSpan.FromHours(18), TimeSpan.FromDays(1) + TimeSpan.FromHours(9));

        internal TimeZoneHelper TimeZoneHelper { get { return timeZoneHelper; } }
        internal TimeOfDayInterval NonWorkingTime { get { return nonWorkingTime; } set { nonWorkingTime = value; } }

        public Form1() {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e) {
            // TODO: This line of code loads data into the 'xtraSchedulingDataSet1.Resources' table. You can move, or remove it, as needed.
            this.resourcesTableAdapter.Fill(this.xtraSchedulingDataSet1.Resources);
            // TODO: This line of code loads data into the 'xtraSchedulingDataSet.Appointments' table. You can move, or remove it, as needed.
            this.appointmentsTableAdapter.Fill(this.xtraSchedulingDataSet.Appointments);

            timeZoneHelper = new TimeZoneHelper(this.schedulerControl1.OptionsBehavior.ClientTimeZoneId);
        }

        private void button1_Click(object sender, EventArgs e) {
            TimeSpan duration = ((DateTime)slotDuration.EditValue).TimeOfDay;
            DateTime start = schedulerControl1.ActiveView.SelectedInterval.End;
            DateTime startOfWeek = DevExpress.XtraScheduler.Native.DateTimeHelper.GetStartOfWeek(start);
            TimeInterval interval = new TimeInterval(start, startOfWeek.AddDays(7));
            TimeInterval freeTime = FindInterval(interval, duration);
            string text;
            if (TimeInterval.Equals(freeTime, TimeInterval.Empty))
                text = "Not found";
            else {
                TimeInterval clientFreeInterval = TimeZoneHelper.ToClientTime(freeTime);
                text = "Free time interval with duration " + clientFreeInterval.Duration.ToString() +
                    " is found! \n\r It starts on " + clientFreeInterval.Start.Date.ToShortDateString() +
                    " at " + clientFreeInterval.Start.TimeOfDay.ToString() + ".";
                schedulerControl1.ActiveView.SetSelection(clientFreeInterval, ResourceEmpty.Resource);
            }
            MessageBox.Show(text, "Search Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private TimeInterval FindInterval(TimeInterval clientInterval, TimeSpan duration) {
            FreeTimeCalculator calculator = new FreeTimeCalculator(schedulerControl1.Storage);
            // Set a handler for the IntervalFound event.
            calculator.IntervalFound += new IntervalFoundEventHandler(OnIntervalFound);
            TimeInterval interval = TimeZoneHelper.FromClientTime(clientInterval);
            // Call the method which raises the event.
            return calculator.FindFreeTimeInterval(interval, duration, true);
        }
        private void OnIntervalFound(object sender, IntervalFoundEventArgs args) {
            TimeIntervalCollectionEx freeIntervals = args.FreeIntervals;
            DateTime start = freeIntervals.Start.Date.AddDays(-1);
            DateTime end = freeIntervals.End;
            while (start < end) {
                RemoveNonWorkingTime(freeIntervals, start);
                RemoveNonWorkingDay(freeIntervals, start);
                start += TimeSpan.FromDays(1);
            }
        }
        private void RemoveNonWorkingTime(TimeIntervalCollectionEx freeIntervals, DateTime date) {
            DateTime clientDate = TimeZoneHelper.ToClientTime(date).Date;        

            TimeInterval clientNonWorkingTime = new TimeInterval(clientDate + NonWorkingTime.Start, clientDate + NonWorkingTime.End);
            freeIntervals.Remove(TimeZoneHelper.FromClientTime(clientNonWorkingTime));
        }
        private void RemoveNonWorkingDay(TimeIntervalCollectionEx freeIntervals, DateTime date) {
            DateTime clientDate = TimeZoneHelper.ToClientTime(date).Date;
            bool isWorkDay = schedulerControl1.WorkDays.IsWorkDay(clientDate);
            if (!isWorkDay) {
                TimeInterval clientInterval = new TimeInterval(clientDate, TimeSpan.FromDays(1));
                freeIntervals.Remove(TimeZoneHelper.FromClientTime(clientInterval));
            }
        }
        private void OnApptChangedInsertedDeleted(object sender, PersistentObjectsEventArgs e) {
            appointmentsTableAdapter.Update(xtraSchedulingDataSet);
            xtraSchedulingDataSet.AcceptChanges();
        }
    }
}
See Also