Click here to Skip to main content
15,892,161 members
Articles / Programming Languages / C#

Time scheduler in C#

Rate me:
Please Sign up or sign in to vote.
4.67/5 (17 votes)
16 May 2007GPL34 min read 135.9K   2.5K   82  
This library allows iterating through a sequence of events or time ranges based on a time schedule.
using System;
using System.Collections.Generic;
using System.Text;

namespace Idaligo.Time
{
	public class OverAreaSequence : ISequence
	{
		private ISequence sequence;
		private IArea area;

		public OverAreaSequence(ISequence sequence, IArea area)
		{
			if (sequence == null) throw new ArgumentNullException("sequence");
			if (area == null) throw new ArgumentNullException("area");
			this.sequence = sequence;
			this.area = area;
		}

		public bool AtPoint(DateTime at)
		{
			return this.sequence.AtPoint(at) && this.area.Hit(at);
		}

		public IEnumerable<DateTime> Walk(DateTime from)
		{
			IEnumerator<TimeRange> iteratorForArea = this.area.Walk(from).GetEnumerator();
			if (iteratorForArea.MoveNext())
			{
				TimeRange area = iteratorForArea.Current;

				DateTime start = (iteratorForArea.Current.From < from) ? from : iteratorForArea.Current.From;
				IEnumerator<DateTime> iteratorForSequence = this.sequence.Walk(start).GetEnumerator();
				while (iteratorForSequence.MoveNext())
				{
					DateTime at = iteratorForSequence.Current;
					if (at < area.Till)
					{
						yield return at;
					}
					else
					{
						break;
					}
				}


				while (iteratorForArea.MoveNext())
				{
					area = iteratorForArea.Current;
					iteratorForSequence = this.sequence.Walk(area.From).GetEnumerator();
					while (iteratorForSequence.MoveNext())
					{
						DateTime at = iteratorForSequence.Current;
						if (at < area.Till)
						{
							yield return at;
						}
						else
						{
							break;
						}
					}
				}
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
United States United States
C# .NET developer since 2003

Comments and Discussions