void Snippets2() { #region SubtractCollection var nz = TimeSpan.FromHours(12); var availability = new TimeRange(new DateTimeOffset(2013, 8, 13, 9, 0, 0, nz), TimeSpan.FromHours(3)); // 0900 - 1200 var busy = new TimeRange[] { new TimeRange(new DateTimeOffset(2013, 8, 13, 9, 0, 0, nz), TimeSpan.FromMinutes(30)), // 0900 - 0930 new TimeRange(new DateTimeOffset(2013, 8, 13, 9, 0, 0, nz), TimeSpan.FromHours(1)), // 0900 - 1000 new TimeRange(new DateTimeOffset(2013, 8, 13, 11, 15, 0, nz), TimeSpan.FromMinutes(15)), // 1115 - 1130 }; var free = availability.Subtract(busy); foreach (var t in free) Console.WriteLine(t); // Produces the following free time // 13 Aug 2013 10:00:00 a.m. +12:00 for 01:15:00 // 13 Aug 2013 11:30:00 a.m. +12:00 for 00:30:00 #endregion }
public void SubtractingAgain() { var nz = TimeSpan.FromHours(12); var duration = TimeSpan.FromMinutes(1); var a = new DateTimeOffset(2013, 8, 13, 9, 0, 0, nz); var b = a + duration; var c = b + duration; var d = c + duration; var e = d + duration; var f = e + duration; var g = f + duration; var h = g + duration; var availability = new TimeRange(c, f); var free = availability.Subtract(new TimeRange(a, b)).ToArray(); Assert.AreEqual(1, free.Length); Assert.AreEqual(availability, free[0]); free = availability.Subtract(new TimeRange(b, c)).ToArray(); Assert.AreEqual(1, free.Length); Assert.AreEqual(availability, free[0]); free = availability.Subtract(new TimeRange(f, g)).ToArray(); Assert.AreEqual(1, free.Length); Assert.AreEqual(availability, free[0]); free = availability.Subtract(new TimeRange(g, h)).ToArray(); Assert.AreEqual(1, free.Length); Assert.AreEqual(availability, free[0]); free = availability.Subtract(new TimeRange(b, g)).ToArray(); Assert.AreEqual(0, free.Length); free = availability.Subtract(new TimeRange(d, e)).ToArray(); Assert.AreEqual(2, free.Length); Assert.AreEqual(new TimeRange(c, d), free[0]); Assert.AreEqual(new TimeRange(e, f), free[1]); free = availability.Subtract(new TimeRange(b, d)).ToArray(); // fails Assert.AreEqual(1, free.Length); Assert.AreEqual(new TimeRange(d, f), free[0]); free = availability.Subtract(new TimeRange(e, g)).ToArray(); Assert.AreEqual(1, free.Length); Assert.AreEqual(new TimeRange(c, e), free[0]); free = availability.Subtract(availability).ToArray(); Assert.AreEqual(0, free.Length); free = availability.Subtract(new TimeRange(c, d)).ToArray(); Assert.AreEqual(1, free.Length); Assert.AreEqual(new TimeRange(d, f), free[0]); free = availability.Subtract(new TimeRange(e, f)).ToArray(); Assert.AreEqual(1, free.Length); Assert.AreEqual(new TimeRange(c, e), free[0]); }
public void Subtracting() { var now = new DateTimeOffset(2013, 8, 13, 13, 0, 0, TimeSpan.FromHours(12)); var tick = new TimeSpan(1); var time = new TimeRange(now, now.AddHours(1)); var before = new TimeRange(now - TimeSpan.FromHours(1), time.StartsOn); var after = new TimeRange(time.EndsOn, TimeSpan.FromHours(1)); var mid = new TimeRange(now.AddMinutes(15), TimeSpan.FromMinutes(30)); // No intersection Assert.AreEqual(1, time.Subtract(before).Count()); Assert.AreEqual(time, time.Subtract(before).First()); Assert.AreEqual(1, time.Subtract(after).Count()); Assert.AreEqual(time, time.Subtract(after).First()); // Identity Assert.AreEqual(0, time.Subtract(time).Count()); // Subsumes Assert.AreEqual(0, time.Subtract(new TimeRange(before.StartsOn, after.EndsOn)).Count()); // Middle Assert.AreEqual(2, time.Subtract(mid).Count()); Assert.IsTrue(time.Subtract(mid).Any(t => t.StartsOn == now && t.Duration == TimeSpan.FromMinutes(15))); Assert.IsTrue(time.Subtract(mid).Any(t => t.StartsOn == now.AddMinutes(45) && t.Duration == TimeSpan.FromMinutes(15))); }