Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
geoperez committed Oct 10, 2024
1 parent 18242c5 commit ce7079b
Show file tree
Hide file tree
Showing 20 changed files with 89 additions and 82 deletions.
24 changes: 24 additions & 0 deletions src/Unosquare.DateTimeExt/Date-Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,28 @@ public static DateTime OrNow(this DateTime date) =>

public static DateTime OrToday(this DateTime date) =>
date > DateTime.Today ? DateTime.Today : date;

public static DateTime GetNextBusinessDay(this DateTime currentDate)
{
do
{
currentDate = currentDate.AddDays(1);
} while (currentDate.IsWeekend());

return currentDate;
}

public static DateTime GetDateFromWeekDay(this DateTime currentDate, int weekday)
{
var daysAdded = 0;

while (daysAdded < weekday)
{
currentDate = currentDate.AddDays(1);
if (currentDate.DayOfWeek != DayOfWeek.Saturday && currentDate.DayOfWeek != DayOfWeek.Sunday)
daysAdded++;
}

return currentDate;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
namespace Unosquare.DateTimeExt.Interfaces;

public interface IReadOnlyDateOnlyRange : IHasReadOnlyStartDateOnly, IHasReadOnlyEndDateOnly
{
}
public interface IReadOnlyDateOnlyRange : IHasReadOnlyStartDateOnly, IHasReadOnlyEndDateOnly;
4 changes: 1 addition & 3 deletions src/Unosquare.DateTimeExt/Interfaces/IReadOnlyDateRange.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
namespace Unosquare.DateTimeExt.Interfaces;

public interface IReadOnlyDateRange : IHasReadOnlyStartDate, IHasReadOnlyEndDate
{
}
public interface IReadOnlyDateRange : IHasReadOnlyStartDate, IHasReadOnlyEndDate;
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
namespace Unosquare.DateTimeExt.Interfaces;

public interface IReadOnlyOpenDateRange : IHasReadOnlyStartDate, ICanHaveReadOnlyEndDate
{
}
public interface IReadOnlyOpenDateRange : IHasReadOnlyStartDate, ICanHaveReadOnlyEndDate;
4 changes: 1 addition & 3 deletions src/Unosquare.DateTimeExt/Interfaces/IYearMonth.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
namespace Unosquare.DateTimeExt.Interfaces;

public interface IYearMonth : IHasReadOnlyYear, IHasReadOnlyMonth
{
}
public interface IYearMonth : IHasReadOnlyYear, IHasReadOnlyMonth;
4 changes: 1 addition & 3 deletions src/Unosquare.DateTimeExt/Interfaces/IYearMonthDateRange.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
namespace Unosquare.DateTimeExt.Interfaces;

public interface IYearMonthDateRange : IYearMonth, IReadOnlyDateRange
{
}
public interface IYearMonthDateRange : IYearMonth, IReadOnlyDateRange;
4 changes: 1 addition & 3 deletions src/Unosquare.DateTimeExt/Interfaces/IYearQuarter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
namespace Unosquare.DateTimeExt.Interfaces;

public interface IYearQuarter : IHasReadOnlyYear, IHasReadOnlyQuarter
{
}
public interface IYearQuarter : IHasReadOnlyYear, IHasReadOnlyQuarter;
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
namespace Unosquare.DateTimeExt.Interfaces;

public interface IYearQuarterDateRange : IYearQuarter, IReadOnlyDateRange
{
}
public interface IYearQuarterDateRange : IYearQuarter, IReadOnlyDateRange;
4 changes: 1 addition & 3 deletions src/Unosquare.DateTimeExt/Interfaces/IYearWeek.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
namespace Unosquare.DateTimeExt.Interfaces;

public interface IYearWeek : IHasReadOnlyYear, IHasReadOnlyWeek
{
}
public interface IYearWeek : IHasReadOnlyYear, IHasReadOnlyWeek;
4 changes: 1 addition & 3 deletions src/Unosquare.DateTimeExt/Interfaces/IYearWeekDateRange.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
namespace Unosquare.DateTimeExt.Interfaces;

public interface IYearWeekDateRange : IYearWeek, IReadOnlyDateRange
{
}
public interface IYearWeekDateRange : IYearWeek, IReadOnlyDateRange;
13 changes: 4 additions & 9 deletions src/Unosquare.DateTimeExt/RangeBase.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
namespace Unosquare.DateTimeExt;

public abstract class RangeBase<TStart, TEnd> where TStart : struct
public abstract class RangeBase<TStart, TEnd>(TStart startDate, TEnd endDate)
where TStart : struct
{
protected RangeBase(TStart startDate, TEnd endDate)
{
StartDate = startDate;
EndDate = endDate;
}

public TStart StartDate { get; }
public TStart StartDate { get; } = startDate;

public TEnd EndDate { get; }
public TEnd EndDate { get; } = endDate;

public IEnumerable<TK> Select<TK>(Func<TStart, TK> selector)
{
Expand Down
1 change: 0 additions & 1 deletion src/Unosquare.DateTimeExt/Unosquare.DateTimeExt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
<AnalysisLevel>latest</AnalysisLevel>
<CodeAnalysisRuleSet>..\..\StyleCop.Analyzers.ruleset</CodeAnalysisRuleSet>
<Version>1.4.1</Version>
Expand Down
8 changes: 2 additions & 6 deletions src/Unosquare.DateTimeExt/YearEntity.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
namespace Unosquare.DateTimeExt;

public sealed class YearEntity : YearAbstract
public sealed class YearEntity(int? year = null) : YearAbstract(new(year ?? DateTime.UtcNow.Year, 1, 1),
new(year ?? DateTime.UtcNow.Year, 12, 31))
{
public YearEntity(int? year = null)
: base(new(year ?? DateTime.UtcNow.Year, 1, 1), new(year ?? DateTime.UtcNow.Year, 12, 31))
{
}

public YearEntity(DateTime? dateTime)
: this((dateTime ?? DateTime.UtcNow).Year)
{
Expand Down
9 changes: 3 additions & 6 deletions src/Unosquare.DateTimeExt/YearMonth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
namespace Unosquare.DateTimeExt;

[DebuggerDisplay("{ToString()} ({ToDateRangeString()})")]
public class YearMonth : YearAbstract, IYearMonthDateRange, IComparable<YearMonth>
public class YearMonth(int? month = null, int? year = null)
: YearAbstract(GetStartDate(month, year), GetStartDate(month, year).GetLastDayOfMonth()), IYearMonthDateRange,
IComparable<YearMonth>
{
public YearMonth(int? month = null, int? year = null)
: base(GetStartDate(month, year), GetStartDate(month, year).GetLastDayOfMonth())
{
}

public YearMonth(IYearMonth yearMonth)
: this(yearMonth.Month, yearMonth.Year)
{
Expand Down
8 changes: 2 additions & 6 deletions src/Unosquare.DateTimeExt/YearWeekIso.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
namespace Unosquare.DateTimeExt;

[DebuggerDisplay("ISO Week {Year}-W{Week} ({ToDateRangeString()})")]
public sealed class YearWeekIso : YearWeekBase, IComparable<YearWeekIso>
public sealed class YearWeekIso(int week, int year) : YearWeekBase(ISOWeek.ToDateTime(year, week, DayOfWeek.Monday),
ISOWeek.ToDateTime(year, week, DayOfWeek.Sunday).ToMidnight()), IComparable<YearWeekIso>
{
public YearWeekIso(int week, int year)
: base(ISOWeek.ToDateTime(year, week, DayOfWeek.Monday), ISOWeek.ToDateTime(year, week, DayOfWeek.Sunday).ToMidnight())
{
}

public YearWeekIso(IYearWeek yearWeek)
: this(yearWeek.Week, yearWeek.Year)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ namespace Unosquare.DateTimeExt.Test;
public class DateExtensionsTests
{
public static readonly object[][] BusinessAndWeekendDates =
{
new object[] { new DateTime(2022, 10, 3), new DateTime(2022, 10, 8)},
new object[] { new DateTime(2022, 10, 10), new DateTime(2022, 10, 16)},
new object[] { new DateTime(2022, 10, 24), new DateTime(2022, 10, 30)}
};
[
[new DateTime(2022, 10, 3), new DateTime(2022, 10, 8)],
[new DateTime(2022, 10, 10), new DateTime(2022, 10, 16)],
[new DateTime(2022, 10, 24), new DateTime(2022, 10, 30)],
];

public static readonly object[][] MonthsRange =
{
new object[] { new DateTime(2022, 01, 01), new DateTime(2022, 12, 01)},
new object[] { new DateTime(2022, 01, 01), new DateTime(2022, 2, 01)},
new object[] { new DateTime(2022, 01, 01), new DateTime(2022, 6, 01)}
};
[
[new DateTime(2022, 01, 01), new DateTime(2022, 12, 01)],
[new DateTime(2022, 01, 01), new DateTime(2022, 2, 01)],
[new DateTime(2022, 01, 01), new DateTime(2022, 6, 01)],
];

public static readonly object[][] NormalAndLeapYear =
{
new object[] { new DateTime(2022, 02, 01), new DateTime(2020, 02, 01)},
};
[
[new DateTime(2022, 02, 01), new DateTime(2020, 02, 01)],
];

[Fact]
public void WithDate_ToFormattedString()
Expand Down Expand Up @@ -342,4 +342,22 @@ public void OrToday_ReturnsToday()

Assert.True(orToday <= today);
}

[Fact]
public void GetNextBusinessDay_ReturnsNextBusinessDay()
{
var date = new DateTime(2022, 1, 1);
var nextBusinessDay = date.GetNextBusinessDay();

Assert.Equal(new(2022, 1, 3), nextBusinessDay);
}

[Fact]
public void GetDateFromWeekDay_ReturnsDateFromWeekDay()
{
var date = new DateTime(2022, 1, 1);
var dateFromWeekDay = date.GetDateFromWeekDay(1);

Assert.Equal(new(2022, 1, 3), dateFromWeekDay);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ namespace Unosquare.DateTimeExt.Test;

public class YearMonthGrouperTests
{
private readonly List<YearMonthRecordWithData> _data = new()
{
new() { Year = 2022, Month = 1, Total = 5 },
new() { Year = 2022, Month = 2, Total = 6 },
new() { Year = 2022, Month = 2, Total = 1 },
new() { Year = 2022, Month = 1, Total = 1 },
new() { Year = 2022, Month = 2, Total = 1 },
new() { Year = 2022, Month = 1, Total = 1 }
};
private readonly List<YearMonthRecordWithData> _data =
[
new() {Year = 2022, Month = 1, Total = 5},
new() {Year = 2022, Month = 2, Total = 6},
new() {Year = 2022, Month = 2, Total = 1},
new() {Year = 2022, Month = 1, Total = 1},
new() {Year = 2022, Month = 2, Total = 1},
new() {Year = 2022, Month = 1, Total = 1},
];

[Fact]
public void GroupByDateRange_ShouldGroupByYearMonth()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void WithYearQuarter_ReturnsFormattedString()
[Fact]
public void WithYearQuarter_ReturnsMonths()
{
Assert.Equal(new[] { 10, 11, 12 }, new YearQuarter(year: 2022, quarter: 4).Months);
Assert.Equal([10, 11, 12], new YearQuarter(year: 2022, quarter: 4).Months);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void WithYearToDateOnPast_ReturnsYear()
[Fact]
public void WithYearToDateOnPast_ReturnsAllMonths()
{
Assert.Equal(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }, new YearToDate(2020).Months);
Assert.Equal([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], new YearToDate(2020).Months);
}

[Fact]
Expand Down

0 comments on commit ce7079b

Please sign in to comment.