Skip to content

Commit 70dfc5c

Browse files
committed
DateOnlyInterval
1 parent b80b108 commit 70dfc5c

File tree

4 files changed

+87
-5
lines changed

4 files changed

+87
-5
lines changed

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ namespace Foundation.Core
77
/// <summary>
88
/// https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/DateOnly.cs
99
/// </summary>
10-
public readonly struct DateOnly
10+
[DebuggerDisplay("{DebuggerDisplay,nq}")]
11+
public readonly struct DateOnly : IComparable<DateOnly>
1112
{
1213
private readonly int _dayNumber;
1314

15+
private const int MinDayNumber = 0;
1416
private const int MaxDayNumber = 3_652_058;
1517

1618
private static int DayNumberFromDateTime(DateTime dt) => (int) (dt.Ticks / TimeSpan.TicksPerDay);
@@ -39,11 +41,36 @@ public DateOnly AddDays(int value)
3941
static void ThrowOutOfRange() => throw new ArgumentOutOfRangeException(nameof(value));
4042
}
4143

44+
[Pure]
45+
public DateTime ToDateTime() => ToDateTime(_dayNumber);
46+
47+
[Pure]
48+
private static DateTime ToDateTime(int dayNumber) => new DateTime(dayNumber * TimeSpan.TicksPerDay);
49+
4250
public static bool operator ==(DateOnly left, DateOnly right) => left._dayNumber == right._dayNumber;
4351
public static bool operator !=(DateOnly left, DateOnly right) => left._dayNumber != right._dayNumber;
4452
public static bool operator >(DateOnly left, DateOnly right) => left._dayNumber > right._dayNumber;
4553
public static bool operator >=(DateOnly left, DateOnly right) => left._dayNumber >= right._dayNumber;
4654
public static bool operator <(DateOnly left, DateOnly right) => left._dayNumber < right._dayNumber;
4755
public static bool operator <=(DateOnly left, DateOnly right) => left._dayNumber <= right._dayNumber;
56+
57+
public int CompareTo(DateOnly other) => _dayNumber.CompareTo(other._dayNumber);
58+
59+
internal string DebuggerDisplay
60+
{
61+
get
62+
{
63+
string debuggerDisplay;
64+
65+
if (_dayNumber == MinDayNumber)
66+
debuggerDisplay = $"{ToDateTime():d}(min)";
67+
else if (_dayNumber == MaxDayNumber)
68+
debuggerDisplay = $"{ToDateTime():d}(max)";
69+
else
70+
debuggerDisplay = $"{ToDateTime():d}";
71+
72+
return debuggerDisplay;
73+
}
74+
}
4875
}
4976
}
Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
namespace Foundation.Core
1+
using System.Collections.Generic;
2+
using System.Diagnostics.Contracts;
3+
using Foundation.Assertions;
4+
5+
namespace Foundation.Core
26
{
37
public readonly struct DateOnlyInterval
48
{
@@ -7,8 +11,59 @@ public readonly struct DateOnlyInterval
711

812
public DateOnlyInterval(DateOnly start, DateOnly end)
913
{
14+
Assert.IsTrue(start <= end);
1015
Start = start;
1116
End = end;
1217
}
18+
19+
[Pure]
20+
public bool Contains(DateOnly date)
21+
{
22+
var contains = Start <= date && date <= End;
23+
return contains;
24+
}
25+
26+
[Pure]
27+
public bool Contains(DateOnlyInterval other)
28+
{
29+
var contains = Start <= other.Start && other.End <= End;
30+
return contains;
31+
}
32+
33+
[Pure]
34+
public DateOnlyInterval? Intersect(DateOnlyInterval other)
35+
{
36+
var start = ElementPair.Max(Start, other.Start);
37+
var end = ElementPair.Min(End, other.End);
38+
var intersects = start <= end;
39+
return intersects
40+
? new DateOnlyInterval(start, end)
41+
: (DateOnlyInterval?) null;
42+
}
43+
44+
[Pure]
45+
public bool Intersects(DateOnlyInterval other)
46+
{
47+
var start = ElementPair.Max(Start, other.Start);
48+
var end = ElementPair.Min(End, other.End);
49+
var intersects = start <= end;
50+
return intersects;
51+
}
52+
53+
//[Pure]
54+
//public int GetLength()
55+
//{
56+
// var length = End - Start + 1;
57+
// return length;
58+
//}
59+
60+
[Pure]
61+
public IEnumerable<DateOnly> GetDates()
62+
{
63+
for (var date = Start; date <= End; date = date.AddDays(1))
64+
yield return date;
65+
}
66+
67+
private string DebuggerDisplay => $"{Start.DebuggerDisplay}-{End.DebuggerDisplay}";
1368
}
1469
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public SmallDate(int year, int month, int day)
3232
_value = ToSmallDateValue(dateTime);
3333
}
3434

35-
public ushort Value => _value;
35+
//public ushort Value => _value;
3636
public int Year => ToDateTime(_value).Year;
3737
public int Month => ToDateTime(_value).Month;
3838
public int Day => ToDateTime(_value).Day;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ public SmallDateInterval(SmallDate start, SmallDate end)
2121
[Pure]
2222
public bool Contains(SmallDate date)
2323
{
24-
var contains = Start.Value <= date.Value && date.Value <= End.Value;
24+
var contains = Start <= date && date <= End;
2525
return contains;
2626
}
2727

2828
[Pure]
2929
public bool Contains(SmallDateInterval other)
3030
{
31-
var contains = Start.Value <= other.Start.Value && other.End.Value <= End.Value;
31+
var contains = Start <= other.Start && other.End <= End;
3232
return contains;
3333
}
3434

0 commit comments

Comments
 (0)