-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
408 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Unosquare.DateTimeExt.Interfaces; | ||
|
||
namespace Unosquare.DateTimeExt; | ||
|
||
public abstract class BaseGrouper<T, TConcrete> : IGrouper<T, TConcrete> | ||
{ | ||
public abstract IEnumerable<IGrouping<TConcrete, T>> GroupByDateRange(); | ||
|
||
public IDictionary<string, List<T>> GroupByLabel() => | ||
GroupByDateRange() | ||
.ToDictionary(x => x.Key!.ToString()!, x => x.ToList()); | ||
|
||
public IDictionary<string, List<TResult>> GroupByLabel<TResult>(Func<T, TResult> selector) => | ||
GroupByDateRange() | ||
.ToDictionary(x => x.Key!.ToString()!, x => x.Select(selector).ToList()); | ||
|
||
public IDictionary<string, int> GroupCount() => | ||
GroupByDateRange() | ||
.ToDictionary(x => x.Key!.ToString()!, x => x.Count()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/Unosquare.DateTimeExt/Interfaces/ICanHaveReadOnlyEndDate.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Unosquare.DateTimeExt.Interfaces; | ||
|
||
public interface ICanHaveReadOnlyEndDate | ||
{ | ||
DateTime? EndDate { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Unosquare.DateTimeExt.Interfaces; | ||
|
||
public interface IGrouper<T, out TConcrete> | ||
{ | ||
IEnumerable<IGrouping<TConcrete, T>> GroupByDateRange(); | ||
|
||
IDictionary<string, List<T>> GroupByLabel(); | ||
|
||
IDictionary<string, List<TResult>> GroupByLabel<TResult>(Func<T, TResult> selector); | ||
|
||
IDictionary<string, int> GroupCount(); | ||
} |
5 changes: 5 additions & 0 deletions
5
src/Unosquare.DateTimeExt/Interfaces/IReadOnlyOpenDateRange.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace Unosquare.DateTimeExt.Interfaces; | ||
|
||
public interface IReadOnlyOpenDateRange : IHasReadOnlyStartDate, ICanHaveReadOnlyEndDate | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System.Collections; | ||
using Unosquare.DateTimeExt.Interfaces; | ||
|
||
namespace Unosquare.DateTimeExt; | ||
|
||
public class OpenDateRange(DateTime startDate, DateTime? endDate = null) : RangeBase<DateTime, DateTime?>(startDate, endDate), IReadOnlyOpenDateRange, IComparable<OpenDateRange>, IEnumerable<DateTime>, IHasBusinessDays | ||
Check warning on line 6 in src/Unosquare.DateTimeExt/OpenDateRange.cs GitHub Actions / Build
Check warning on line 6 in src/Unosquare.DateTimeExt/OpenDateRange.cs GitHub Actions / Build
Check warning on line 6 in src/Unosquare.DateTimeExt/OpenDateRange.cs GitHub Actions / Build
|
||
{ | ||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
|
||
public override IEnumerator<DateTime> GetEnumerator() | ||
{ | ||
if (EndDate is not null) | ||
{ | ||
var daysInBetween = (EndDate.Value - StartDate).Days; | ||
for (var i = 0; i <= daysInBetween; i++) | ||
yield return StartDate.AddDays(i); | ||
} | ||
else | ||
{ | ||
var i = 0; | ||
|
||
while (DateTime.MaxValue != StartDate.AddDays(i)) | ||
yield return StartDate.AddDays(++i); | ||
} | ||
} | ||
|
||
public int CompareTo(OpenDateRange? other) | ||
{ | ||
if (ReferenceEquals(this, other)) | ||
return 0; | ||
|
||
if (other is null) | ||
return 1; | ||
|
||
var startDateComparison = StartDate.CompareTo(other.StartDate); | ||
|
||
return startDateComparison != 0 ? startDateComparison : (EndDate?.CompareTo(other.EndDate) ?? 0); | ||
} | ||
|
||
public override string ToString() => $"{StartDate.ToShortDateString()} - {EndDate?.ToShortDateString()}"; | ||
|
||
public override int GetHashCode() => StartDate.GetHashCode() + EndDate.GetHashCode(); | ||
|
||
public DateTime FirstBusinessDay => StartDate.GetFirstBusinessDayOfMonth(); | ||
public DateTime LastBusinessDay => (EndDate ?? DateTime.Now).GetLastBusinessDayOfMonth(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Unosquare.DateTimeExt.Interfaces; | ||
|
||
namespace Unosquare.DateTimeExt; | ||
|
||
public class YearMonthGrouper<T>(IEnumerable<T> query) : BaseGrouper<T, YearMonth> where T : IYearMonth | ||
{ | ||
public override IEnumerable<IGrouping<YearMonth, T>> GroupByDateRange() => query.GroupBy(x => new YearMonth(x.Month, x.Year)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Unosquare.DateTimeExt.Interfaces; | ||
|
||
namespace Unosquare.DateTimeExt; | ||
|
||
public class YearMonthRecordGrouper<T>(IEnumerable<T> query) : BaseGrouper<T, YearMonthRecord> where T : IYearMonth | ||
{ | ||
public override IEnumerable<IGrouping<YearMonthRecord, T>> GroupByDateRange() => query.GroupBy(x => new YearMonthRecord { Month = x.Month, Year = x.Year }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Unosquare.DateTimeExt.Interfaces; | ||
|
||
namespace Unosquare.DateTimeExt; | ||
|
||
public class YearQuarterGrouper<T>(IEnumerable<T> query) : BaseGrouper<T, YearQuarter> where T : IYearQuarter | ||
{ | ||
public override IEnumerable<IGrouping<YearQuarter, T>> GroupByDateRange() => query.GroupBy(x => new YearQuarter(x.Quarter, x.Year)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Unosquare.DateTimeExt.Interfaces; | ||
|
||
namespace Unosquare.DateTimeExt; | ||
|
||
public class YearQuarterRecordGrouper<T>(IEnumerable<T> query) : BaseGrouper<T, YearQuarterRecord> where T : IYearQuarter | ||
{ | ||
public override IEnumerable<IGrouping<YearQuarterRecord, T>> GroupByDateRange() => query.GroupBy(x => new YearQuarterRecord { Year = x.Year, Quarter = x.Quarter }); | ||
} |
Oops, something went wrong.