forked from marsop/ephemeral
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntervalExtensions.cs
73 lines (55 loc) · 2.86 KB
/
IntervalExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using Optional;
using System;
namespace Marsop.Ephemeral
{
public static class IntervalExtensions
{
public static bool Covers(this IInterval interval, DateTimeOffset timestamp)
{
if (timestamp < interval.Start)
return false;
if (interval.End < timestamp)
return false;
if (timestamp == interval.Start && !interval.StartIncluded)
return false;
if (timestamp == interval.End && !interval.EndIncluded)
return false;
return true;
}
public static bool Covers(this IInterval interval, IInterval other) =>
interval.Intersect(other).Match(x => x.ToInterval().Equals(other), () => false);
public static TimeSpan DurationUntilNow(this IInterval interval) =>
DateTimeOffset.UtcNow < interval.End ? interval.End - DateTimeOffset.UtcNow : interval.Duration;
/// <summary>
/// Creates an interval based on the information of this object
/// </summary>
/// <returns> new interval </returns>
public static Interval ToInterval(this IInterval interval) =>
new Interval(interval.Start, interval.End, interval.StartIncluded, interval.EndIncluded);
/// <summary>
/// Generates a new Interval, which is the intersection of the two.
/// WARN: Can be null
/// </summary>
/// <param name="interval"> first interval </param>
/// <param name="other"> second interval </param>
/// <returns> new interval </returns>
public static Option<IInterval> Intersect(this IInterval interval, IInterval other) =>
Interval.Intersect(interval, other).Map(x => (IInterval)x);
public static IDisjointIntervalSet Union(this IInterval i, IInterval j) => new DisjointIntervalSet(i, j);
public static TimeSpan DurationOfIntersect(this IInterval i, IInterval j) =>
i.Intersect(j).Match(x => x.Duration, () => TimeSpan.Zero);
public static bool Intersects(this IInterval i, IInterval j) => i.Intersect(j).HasValue;
/// <summary>
/// Returns true if both intervals join with each other seamlessly and without overlap
/// </summary>
/// <param name="i">earlier interval</param>
/// <param name="o">later interval</param>
/// <returns></returns>
public static bool IsContiguouslyFollowedBy(this IInterval i, IInterval o) =>
i.End == o.Start && (i.EndIncluded != o.StartIncluded);
public static bool IsContiguouslyPreceededBy(this IInterval i, IInterval o) =>
o.IsContiguouslyFollowedBy(i);
public static bool StartsBefore(this IInterval interval, IInterval other) =>
(interval.Start < other.Start || (interval.Start == other.Start && interval.StartIncluded && !other.StartIncluded));
}
}