Sounds like another philosophical question but no, you can relax. This is another developmental (yes almost mental) posting.
Say you need to create a piece of functionality that must only operate within a predefined time period of a day – call it a ‘service window’. It could be that you may even need several of these ‘windows’ per day – i.e. if there is a pause period inside the day and work must be processed before (up until) and after the pause.
For this kind of functionality I created a ‘ServiceWindows’ class. It basically host a list of ‘from’ and ‘to’ times plus methods that makes it possible to find out if a specified time is somewhere ‘inside’ or ‘outside’ any ‘service window’. Additionally the class can be ‘populated’ from an xml config string so it can be used in applications that operates from xml config files.
So without much further chit-chatting here is the guts of the class (not for sensitive readers 😉
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
public class ServiceWindow
{
public ServiceWindow(DateTime from, DateTime to)
{
From = from;
To = to;
}
public DateTime From { get; set; }
public DateTime To { get; set; }
}
public class ServiceWindows
{
private List<ServiceWindow> times = new List<ServiceWindow>();
public List<ServiceWindow> Times { get { return times; } }
public void AddTimeWindow(DateTime fromTime, DateTime toTime)
{
times.Add(new ServiceWindow(fromTime, toTime));
}
public bool IsInTimeWindow()
{
return IsInTimeWindow(DateTime.Now);
}
public bool IsInTimeWindow(DateTime nowTime)
{
if (times.Count == 0)
return true;
else
return (from t in times
where t.From.TimeOfDay <= nowTime.TimeOfDay &&
t.To.TimeOfDay >= nowTime.TimeOfDay
select t).Count() > 0;
}
public void CreateFromConfig(string config)
{
if (config.Length > 0)
{
times.Clear();
XmlDocument configXml = new XmlDocument();
configXml.LoadXml(config);
XmlElement root = configXml.DocumentElement;
foreach (XmlNode windowItem in root.SelectNodes("window"))
{
try
{
DateTime fromTime = DateTime.Parse(windowItem.ReadXmlElementAttr("from", "00:00:01"));
DateTime toTime = DateTime.Parse(windowItem.ReadXmlElementAttr("to", "23:59:59"));
times.Add(new ServiceWindow(fromTime, toTime));
}
catch { }
}
}
}
public string ToConfig()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("<serviceWindows>");
if (times.Count > 0)
foreach (ServiceWindow timeWindow in times)
sb.AppendLine(string.Format("<window from=\"{0}\" to=\"{1}\" />",
timeWindow.From.ToString("HH:mm:ss"),
timeWindow.To.ToString("HH:mm:ss")));
sb.AppendLine("</serviceWindows>");
return sb.ToString();
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
if (times.Count > 0)
foreach (ServiceWindow timeWindow in times)
sb.Append(string.Format("{0} to {1} and ",
timeWindow.From.ToString("HH:mm:ss"),
timeWindow.To.ToString("HH:mm:ss")));
else
sb.Append("None");
return sb.ToString().Trim(' ', 'a', 'n', 'd');
}
}
I noticed that I included an XmlNode extension method (ReadXmlElementAttr) that I defined elsewhere but all it does is to read an xml element attribute or provide a default value if not found. I’ll list that class here on my blog too soon.