Skip to content

Commit

Permalink
Complete DateOnlyExtensions and TimeOnlyExtensions,recode DateTimeExt…
Browse files Browse the repository at this point in the history
…ensions adn DateTimeOffsetExtensions.
  • Loading branch information
XiaoFeiDu committed Dec 27, 2021
1 parent 06592f9 commit 28ce1f5
Show file tree
Hide file tree
Showing 9 changed files with 353 additions and 200 deletions.
18 changes: 18 additions & 0 deletions src/Zaabee.Extensions/Zaabee.Extensions.DateOnly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#if !NETSTANDARD2_0 && !NETCOREAPP3_1 && !NET5_0
namespace Zaabee.Extensions;

public static partial class ZaabeeExtension
{
public static IEnumerable<DateOnly> EachDayTo(this DateOnly dateFrom, DateOnly dateTo)
{
for (var date = dateFrom; date <= dateTo; date = date.AddDays(1))
yield return date;
}

public static IEnumerable<DateOnly> EachMonthTo(this DateOnly dateFrom, DateOnly dateTo)
{
for (var date = new DateOnly(dateFrom.Year, dateFrom.Month, 1); date <= dateTo; date = date.AddMonths(1))
yield return date;
}
}
#endif
57 changes: 25 additions & 32 deletions src/Zaabee.Extensions/Zaabee.Extensions.DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,43 @@ namespace Zaabee.Extensions;

public static partial class ZaabeeExtension
{
public static IEnumerable<DateTime> EachSecondTo(this DateTime timeFrom, DateTime timeTo)
public static IEnumerable<DateTime> EachSecondTo(this DateTime from, DateTime to)
{
var dateTime = new DateTime(timeTo.Year, timeTo.Month, timeTo.Day,
timeTo.Hour, timeTo.Minute, timeTo.Second);
for (var time = new DateTime(timeFrom.Year, timeFrom.Month, timeFrom.Day,
timeFrom.Hour, timeFrom.Minute, timeFrom.Second);
time <= dateTime;
time = time.AddSeconds(1))
yield return time;
for (var dateTime = new DateTime(from.Year, from.Month, from.Day, from.Hour, from.Minute, from.Second);
dateTime <= to;
dateTime = dateTime.AddSeconds(1))
yield return dateTime;
}

public static IEnumerable<DateTime> EachMinuteTo(this DateTime timeFrom, DateTime timeTo)
public static IEnumerable<DateTime> EachMinuteTo(this DateTime from, DateTime to)
{
var dateTime = new DateTime(timeTo.Year, timeTo.Month, timeTo.Day,
timeTo.Hour, timeTo.Minute, 0);
for (var time = new DateTime(timeFrom.Year, timeFrom.Month, timeFrom.Day,
timeFrom.Hour, timeFrom.Minute, 0);
time <= dateTime;
time = time.AddMinutes(1))
yield return time;
for (var dateTime = new DateTime(from.Year, from.Month, from.Day, from.Hour, from.Minute, 0);
dateTime <= to;
dateTime = dateTime.AddMinutes(1))
yield return dateTime;
}

public static IEnumerable<DateTime> EachHourTo(this DateTime timeFrom, DateTime timeTo)
public static IEnumerable<DateTime> EachHourTo(this DateTime from, DateTime to)
{
var dateTime = new DateTime(timeTo.Year, timeTo.Month, timeTo.Day,
timeTo.Hour, 0, 0);
for (var time = new DateTime(timeFrom.Year, timeFrom.Month, timeFrom.Day,
timeFrom.Hour, 0, 0);
time <= dateTime;
time = time.AddHours(1))
yield return time;
for (var dateTime = new DateTime(from.Year, from.Month, from.Day, from.Hour, 0, 0);
dateTime <= to;
dateTime = dateTime.AddHours(1))
yield return dateTime;
}

public static IEnumerable<DateTime> EachDayTo(this DateTime dateFrom, DateTime dateTo)
public static IEnumerable<DateTime> EachDayTo(this DateTime from, DateTime to)
{
for (var date = dateFrom.Date; date.Date <= dateTo.Date; date = date.AddDays(1))
yield return date;
for (var dateTime = new DateTime(from.Year, from.Month, from.Day);
dateTime <= to;
dateTime = dateTime.AddDays(1))
yield return dateTime;
}

public static IEnumerable<DateTime> EachMonthTo(this DateTime dateFrom, DateTime dateTo)
public static IEnumerable<DateTime> EachMonthTo(this DateTime from, DateTime to)
{
for (var date = dateFrom.Date;
date.Date <= dateTo.Date || date.Month == dateTo.Month;
date = date.AddMonths(1))
yield return date;
for (var dateTime = new DateTime(from.Year, from.Month, 1);
dateTime <= to;
dateTime = dateTime.AddMonths(1))
yield return dateTime;
}
}
62 changes: 30 additions & 32 deletions src/Zaabee.Extensions/Zaabee.Extensions.DateTimeOffset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,48 @@ namespace Zaabee.Extensions;

public static partial class ZaabeeExtension
{
public static IEnumerable<DateTimeOffset> EachSecondTo(this DateTimeOffset timeFrom, DateTimeOffset timeTo)
public static IEnumerable<DateTimeOffset> EachSecondTo(this DateTimeOffset from, DateTimeOffset to)
{
var dateTime = new DateTimeOffset(timeTo.Year, timeTo.Month, timeTo.Day,
timeTo.Hour, timeTo.Minute, timeTo.Second, timeTo.Offset);
for (var time = new DateTimeOffset(timeFrom.Year, timeFrom.Month, timeFrom.Day,
timeFrom.Hour, timeFrom.Minute, timeFrom.Second, timeFrom.Offset);
time <= dateTime;
time = time.AddSeconds(1))
yield return time;
for (var dateTime =
new DateTimeOffset(from.Year, from.Month, from.Day, from.Hour, from.Minute, from.Second, from.Offset);
dateTime <= to;
dateTime = dateTime.AddSeconds(1))
yield return dateTime;
}

public static IEnumerable<DateTimeOffset> EachMinuteTo(this DateTimeOffset timeFrom, DateTimeOffset timeTo)
public static IEnumerable<DateTimeOffset> EachMinuteTo(this DateTimeOffset from, DateTimeOffset to)
{
var dateTime = new DateTimeOffset(timeTo.Year, timeTo.Month, timeTo.Day,
timeTo.Hour, timeTo.Minute, 0, timeTo.Offset);
for (var time = new DateTimeOffset(timeFrom.Year, timeFrom.Month, timeFrom.Day,
timeFrom.Hour, timeFrom.Minute, 0, timeFrom.Offset);
time <= dateTime;
time = time.AddMinutes(1))
yield return time;
for (var dateTime =
new DateTimeOffset(from.Year, from.Month, from.Day, from.Hour, from.Minute, 0, from.Offset);
dateTime <= to;
dateTime = dateTime.AddMinutes(1))
yield return dateTime;
}

public static IEnumerable<DateTimeOffset> EachHourTo(this DateTimeOffset timeFrom, DateTimeOffset timeTo)
public static IEnumerable<DateTimeOffset> EachHourTo(this DateTimeOffset from, DateTimeOffset to)
{
var dateTime = new DateTimeOffset(timeTo.Year, timeTo.Month, timeTo.Day,
timeTo.Hour, 0, 0, timeTo.Offset);
for (var time = new DateTimeOffset(timeFrom.Year, timeFrom.Month, timeFrom.Day,
timeFrom.Hour, 0, 0, timeFrom.Offset);
time <= dateTime;
time = time.AddHours(1))
yield return time;
for (var dateTime =
new DateTimeOffset(from.Year, from.Month, from.Day, from.Hour, 0, 0, from.Offset);
dateTime <= to;
dateTime = dateTime.AddHours(1))
yield return dateTime;
}

public static IEnumerable<DateTimeOffset> EachDayTo(this DateTimeOffset dateFrom, DateTimeOffset dateTo)
public static IEnumerable<DateTimeOffset> EachDayTo(this DateTimeOffset from, DateTimeOffset to)
{
for (var date = dateFrom.Date; date <= dateTo.Date; date = date.AddDays(1))
yield return date;
for (var dateTime =
new DateTimeOffset(from.Year, from.Month, from.Day, 0, 0, 0, from.Offset);
dateTime <= to;
dateTime = dateTime.AddDays(1))
yield return dateTime;
}

public static IEnumerable<DateTimeOffset> EachMonthTo(this DateTimeOffset dateFrom, DateTimeOffset dateTo)
public static IEnumerable<DateTimeOffset> EachMonthTo(this DateTimeOffset from, DateTimeOffset to)
{
for (var date = dateFrom.Date;
date <= dateTo.Date || date.Month == dateTo.Month;
date = date.AddMonths(1))
yield return date;
for (var dateTime =
new DateTimeOffset(from.Year, from.Month, 1, 0, 0, 0, from.Offset);
dateTime <= to;
dateTime = dateTime.AddMonths(1))
yield return dateTime;
}
}
39 changes: 39 additions & 0 deletions src/Zaabee.Extensions/Zaabee.Extensions.TimeOnly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#if !NETSTANDARD2_0 && !NETCOREAPP3_1 && !NET5_0
namespace Zaabee.Extensions;

public static partial class ZaabeeExtension
{
public static TimeOnly AddSeconds(this TimeOnly time, double value) =>
time.Add(TimeSpan.FromSeconds(value));

public static IEnumerable<TimeOnly> EachSecondTo(this TimeOnly from, TimeOnly to)
{
var seconds = (to - from).Seconds;
for (var i = 0; i <= seconds; i++)
{
var result = from.AddSeconds(i);
yield return new TimeOnly(result.Hour, result.Minute, result.Second);
}
}

public static IEnumerable<TimeOnly> EachMinuteTo(this TimeOnly from, TimeOnly to)
{
var minutes = (to - from).Minutes;
for (var i = 0; i <= minutes; i++)
{
var result = from.AddMinutes(i);
yield return new TimeOnly(result.Hour, result.Minute, 0);
}
}

public static IEnumerable<TimeOnly> EachHourTo(this TimeOnly from, TimeOnly to)
{
var hours = (to - from).Hours;
for (var i = 0; i <= hours; i++)
{
var result = from.AddHours(i);
yield return new TimeOnly(result.Hour, 0, 0);
}
}
}
#endif
6 changes: 3 additions & 3 deletions src/Zaabee.Extensions/Zaabee.Extensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>2022.2.0</PackageVersion>
<Version>2022.2.0</Version>
<PackageVersion>2022.4.0</PackageVersion>
<Version>2022.4.0</Version>
<Authors>Mutuduxf</Authors>
<Company>Mutuduxf</Company>
<PackageTags>Zaabee;Extensions</PackageTags>
<Description>Exteinsions for C#</Description>
<PackageProjectUrl>https://github.com/Mutuduxf/Zaabee.Extensions</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
46 changes: 46 additions & 0 deletions tests/Zaabee.Extensions.UnitTest/DateOnlyExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#if !NET48 && !NETCOREAPP3_1 && !NET5_0
namespace Zaabee.Extensions.UnitTest;

public class DateOnlyExtensionsTest
{
[Theory]
[InlineData(-1024)]
[InlineData(0)]
[InlineData(1024)]
public void EachDayToTest(int days)
{
var dateFrom = new DateOnly(1900, 1, 1);
var dateTo = dateFrom.AddDays(days);
var dates = dateFrom.EachDayTo(dateTo).ToList();
if (days < 0)
Assert.Empty(dates);
else
Assert.Equal(days + 1, dates.Count);
foreach (var result in dates)
{
Assert.Equal(dateFrom, result);
dateFrom = dateFrom.AddDays(1);
}
}

[Theory]
[InlineData(-1024)]
[InlineData(0)]
[InlineData(1024)]
public void EachMonthToTest(int months)
{
var dateFrom = new DateOnly(1900, 1, 1);
var dateTo = dateFrom.AddMonths(months);
var dates = dateFrom.EachMonthTo(dateTo).ToList();
if (months < 0)
Assert.Empty(dates);
else
Assert.Equal(months + 1, dates.Count);
foreach (var result in dates)
{
Assert.Equal(dateFrom, result);
dateFrom = dateFrom.AddMonths(1);
}
}
}
#endif
Loading

0 comments on commit 28ce1f5

Please sign in to comment.