-
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.
Complete DateOnlyExtensions and TimeOnlyExtensions,recode DateTimeExt…
…ensions adn DateTimeOffsetExtensions.
- Loading branch information
XiaoFeiDu
committed
Dec 27, 2021
1 parent
06592f9
commit 28ce1f5
Showing
9 changed files
with
353 additions
and
200 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,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 |
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,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 |
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
46 changes: 46 additions & 0 deletions
46
tests/Zaabee.Extensions.UnitTest/DateOnlyExtensionsTest.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,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 |
Oops, something went wrong.