Skip to content

Commit 4bdb600

Browse files
committed
DateOnlyInterval
1 parent 7c04a34 commit 4bdb600

File tree

5 files changed

+121
-87
lines changed

5 files changed

+121
-87
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Diagnostics.Contracts;
4+
5+
namespace Foundation.Core
6+
{
7+
/// <summary>
8+
/// https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/DateOnly.cs
9+
/// </summary>
10+
public readonly struct DateOnly
11+
{
12+
private readonly int _dayNumber;
13+
14+
private const int MaxDayNumber = 3_652_058;
15+
16+
private static int DayNumberFromDateTime(DateTime dt) => (int) (dt.Ticks / TimeSpan.TicksPerDay);
17+
18+
private DateOnly(int dayNumber)
19+
{
20+
Debug.Assert((uint) dayNumber <= MaxDayNumber);
21+
_dayNumber = dayNumber;
22+
}
23+
24+
public DateOnly(int year, int month, int day) => _dayNumber = DayNumberFromDateTime(new DateTime(year, month, day));
25+
26+
[Pure]
27+
public DateOnly Next => AddDays(1);
28+
29+
public DateOnly AddDays(int value)
30+
{
31+
int newDayNumber = _dayNumber + value;
32+
if ((uint) newDayNumber > MaxDayNumber)
33+
{
34+
ThrowOutOfRange();
35+
}
36+
37+
return new DateOnly(newDayNumber);
38+
39+
static void ThrowOutOfRange() => throw new ArgumentOutOfRangeException(nameof(value));
40+
}
41+
42+
public static bool operator ==(DateOnly left, DateOnly right) => left._dayNumber == right._dayNumber;
43+
public static bool operator !=(DateOnly left, DateOnly right) => left._dayNumber != right._dayNumber;
44+
public static bool operator >(DateOnly left, DateOnly right) => left._dayNumber > right._dayNumber;
45+
public static bool operator >=(DateOnly left, DateOnly right) => left._dayNumber >= right._dayNumber;
46+
public static bool operator <(DateOnly left, DateOnly right) => left._dayNumber < right._dayNumber;
47+
public static bool operator <=(DateOnly left, DateOnly right) => left._dayNumber <= right._dayNumber;
48+
}
49+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Foundation.Core
2+
{
3+
public readonly struct DateOnlyInterval
4+
{
5+
public readonly DateOnly Start;
6+
public readonly DateOnly End;
7+
8+
public DateOnlyInterval(DateOnly start, DateOnly end)
9+
{
10+
Start = start;
11+
End = end;
12+
}
13+
}
14+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
3+
namespace Foundation.Core
4+
{
5+
public static class DateOnlyIntervalRelations
6+
{
7+
public static bool Precedes(this DateOnlyInterval x, DateOnlyInterval y) => x.End.Next < y.Start;
8+
public static bool Meets(this DateOnlyInterval x, DateOnlyInterval y) => x.End.Next == y.Start;
9+
public static bool OverlapsWith(this DateOnlyInterval x, DateOnlyInterval y) => x.Start < y.Start && y.Start <= x.End && x.End < y.End;
10+
public static bool Starts(this DateOnlyInterval x, DateOnlyInterval y) => x.Start == y.Start && x.End < y.End;
11+
public static bool During(this DateOnlyInterval x, DateOnlyInterval y) => y.Start < x.Start && x.End < y.End;
12+
public static bool Finishes(this DateOnlyInterval x, DateOnlyInterval y) => y.Start < x.Start && x.End == y.End;
13+
public static bool IsPrecededBy(this DateOnlyInterval x, DateOnlyInterval y) => Precedes(y, x);
14+
public static bool IsMetBy(this DateOnlyInterval x, DateOnlyInterval y) => Meets(y, x);
15+
public static bool IsOverlappedBy(this DateOnlyInterval x, DateOnlyInterval y) => OverlapsWith(y, x);
16+
public static bool IsStartedBy(this DateOnlyInterval x, DateOnlyInterval y) => Starts(y, x);
17+
public static bool Contains(this DateOnlyInterval x, DateOnlyInterval y) => During(y, x);
18+
public static bool IsFinishedBy(this DateOnlyInterval x, DateOnlyInterval y) => Finishes(y, x);
19+
public static bool IsEqualTo(this DateOnlyInterval x, DateOnlyInterval y) => x.Start == y.Start && x.End == y.End;
20+
21+
public static TemporalIntervalRelation GetTemporalIntervalRelation(this DateOnlyInterval x, DateOnlyInterval y)
22+
{
23+
TemporalIntervalRelation relation;
24+
25+
if (Precedes(x, y))
26+
relation = TemporalIntervalRelation.Precedes;
27+
else if (Meets(x, y))
28+
relation = TemporalIntervalRelation.Meets;
29+
else if (OverlapsWith(x, y))
30+
relation = TemporalIntervalRelation.OverlapsWith;
31+
else if (Starts(x, y))
32+
relation = TemporalIntervalRelation.Starts;
33+
else if (During(x, y))
34+
relation = TemporalIntervalRelation.During;
35+
else if (Finishes(x, y))
36+
relation = TemporalIntervalRelation.Finishes;
37+
else if (IsPrecededBy(x, y))
38+
relation = TemporalIntervalRelation.IsPrecededBy;
39+
else if (IsMetBy(x, y))
40+
relation = TemporalIntervalRelation.IsMetBy;
41+
else if (IsOverlappedBy(x, y))
42+
relation = TemporalIntervalRelation.IsOverlappedBy;
43+
else if (IsStartedBy(x, y))
44+
relation = TemporalIntervalRelation.IsStartedBy;
45+
else if (Contains(x, y))
46+
relation = TemporalIntervalRelation.Contains;
47+
else if (IsFinishedBy(x, y))
48+
relation = TemporalIntervalRelation.IsFinishedBy;
49+
else if (IsEqualTo(x, y))
50+
relation = TemporalIntervalRelation.IsEqualTo;
51+
else
52+
throw new InvalidOperationException();
53+
54+
return relation;
55+
}
56+
}
57+
}

Foundation/.Net-5.0/Core/TemporalIntervalRelations.cs

Lines changed: 0 additions & 87 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:String x:Key="/Default/CodeInspection/Highlighting/AnalysisEnabled/@EntryValue">VISIBLE_FILES</s:String>
23
<s:String x:Key="/Default/Environment/Hierarchy/Build/SolBuilderDuo/UseMsbuildSolutionBuilder/@EntryValue">NewVersion</s:String></wpf:ResourceDictionary>

0 commit comments

Comments
 (0)