获取时间是约束之间 [英] Getting time that is between constraints
问题描述
我有这3个字符串(使用#作为分隔符):
I have these 3 strings (using # as a delimiter):
Name#startTime#endTime#room
Meeting#19:00:00#20:30:00#Conference
Hist 2368#19:00:00#20:30:00#Large Conference Room
Hist 2368#09:00:00#10:30:00#Large Conference Room
我想知道我将如何产生这个
I want to know how would I generate this
Conference 9:00:00 19:00:00
Large Conference Room 10:30:00 20:30:00
Large Conference Room 20:30:00 22:00:00
所以这是什么正在生成是是自由房间的倍。在顶部的3串可以看出,会议从19:00:00占用20时30分○○秒所以空闲时间是9:00:00到19:00:00(一天开始于9:00:00在22:00:00)结束。
So what this is generating are the times of a room that are free. In the top 3 strings we can see that Conference is occupied from 19:00:00 to 20:30:00 so the free time is 9:00:00 to 19:00:00 (A day starts at 9:00:00 and ends at 22:00:00).
推荐答案
因此,为了使这项工作相对容易,你需要定义理解的类。如何分割给定的时间可能重叠期的一段时间
So to make this task relatively easy you need to define a class that understands how to split a period of time given a potentially overlapping period of time.
下面的那个类:
private sealed class Period : IEquatable<Period>
{
public DateTime StartTime { get; private set; }
public DateTime EndTime { get; private set; }
public Period(DateTime startTime, DateTime endTime)
{
this.StartTime = startTime;
this.EndTime = endTime;
}
public override bool Equals(object obj)
{
if (obj is Period)
return Equals((Period)obj);
return false;
}
public bool Equals(Period obj)
{
if (obj == null)
return false;
if (!EqualityComparer<DateTime>.Default.Equals(
this.StartTime, obj.StartTime))
return false;
if (!EqualityComparer<DateTime>.Default.Equals(
this.EndTime, obj.EndTime))
return false;
return true;
}
public override int GetHashCode()
{
int hash = 0;
hash ^= EqualityComparer<DateTime>.Default
.GetHashCode(this.StartTime);
hash ^= EqualityComparer<DateTime>.Default
.GetHashCode(this.EndTime);
return hash;
}
public override string ToString()
{
return String.Format("{{ StartTime = {0}, EndTime = {1} }}",
this.StartTime, this.EndTime);
}
public IEnumerable<Period> Split(Period period)
{
if (period.StartTime <= this.StartTime)
{
if (period.EndTime <= this.StartTime)
yield return this;
else if (period.EndTime >= this.EndTime)
yield break;
else
yield return new Period(period.EndTime, this.EndTime);
}
else if (period.StartTime < this.EndTime)
{
yield return new Period(this.StartTime, period.StartTime);
if (period.EndTime < this.EndTime)
{
yield return new Period(period.EndTime, this.EndTime);
}
}
else
yield return this;
}
}
这里最重要的代码是的IEnumerable<期及GT;斯普利特(期期)
方法。比较两个时间段和返回可以拆分后左零,一到两个周期时,它通过每一个可能的情况下。
The important code here is the IEnumerable<Period> Split(Period period)
method. It goes through each possible case when comparing two time periods and returns zero, one or two periods that can be left after the split.
因此,鉴于输入数据像这样的:
So, given your input data is like this:
var lines = new []
{
"Meeting#19:00:00#20:30:00#Conference",
"Hist 2368#19:00:00#20:30:00#Large Conference Room",
"Hist 2368#09:00:00#10:30:00#Large Conference Room",
};
var full_day =
new Period(
DateTime.Parse("09:00"),
DateTime.Parse("22:00"));
然后我就可以运行该代码来确定空闲时间:
I can then run this code to determine the free times:
var free_times =
from line in lines
let parts = line.Split('#')
let Start = DateTime.Parse(parts[1])
let End = DateTime.Parse(parts[2])
orderby Start, End
group new Period(Start, End) by parts[3] into groups
select new
{
Room = groups.Key,
FreePeriods =
groups.Aggregate(new [] { full_day },
(ys, x) => ys.SelectMany(y => y.Split(x)).ToArray()),
};
结果我得到的是:
The result I get is:
注:您的例子的结果是问题不匹配您的数据。我假设你的数据是正确的,忽略了你的结果示例。
NB: Your example results in the question do not match your data. I have assumed that your data is correct and ignored your example results.
这篇关于获取时间是约束之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!