diff --git a/src/Zaabee.Extensions/Zaabee.Extensions.DateOnly.cs b/src/Zaabee.Extensions/Zaabee.Extensions.DateOnly.cs new file mode 100644 index 0000000..0a31c6e --- /dev/null +++ b/src/Zaabee.Extensions/Zaabee.Extensions.DateOnly.cs @@ -0,0 +1,18 @@ +#if !NETSTANDARD2_0 && !NETCOREAPP3_1 && !NET5_0 +namespace Zaabee.Extensions; + +public static partial class ZaabeeExtension +{ + public static IEnumerable EachDayTo(this DateOnly dateFrom, DateOnly dateTo) + { + for (var date = dateFrom; date <= dateTo; date = date.AddDays(1)) + yield return date; + } + + public static IEnumerable 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 \ No newline at end of file diff --git a/src/Zaabee.Extensions/Zaabee.Extensions.DateTime.cs b/src/Zaabee.Extensions/Zaabee.Extensions.DateTime.cs index 2aed452..cb48079 100644 --- a/src/Zaabee.Extensions/Zaabee.Extensions.DateTime.cs +++ b/src/Zaabee.Extensions/Zaabee.Extensions.DateTime.cs @@ -2,50 +2,43 @@ namespace Zaabee.Extensions; public static partial class ZaabeeExtension { - public static IEnumerable EachSecondTo(this DateTime timeFrom, DateTime timeTo) + public static IEnumerable 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 EachMinuteTo(this DateTime timeFrom, DateTime timeTo) + public static IEnumerable 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 EachHourTo(this DateTime timeFrom, DateTime timeTo) + public static IEnumerable 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 EachDayTo(this DateTime dateFrom, DateTime dateTo) + public static IEnumerable 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 EachMonthTo(this DateTime dateFrom, DateTime dateTo) + public static IEnumerable 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; } } \ No newline at end of file diff --git a/src/Zaabee.Extensions/Zaabee.Extensions.DateTimeOffset.cs b/src/Zaabee.Extensions/Zaabee.Extensions.DateTimeOffset.cs index d28d0a8..fbaf69a 100644 --- a/src/Zaabee.Extensions/Zaabee.Extensions.DateTimeOffset.cs +++ b/src/Zaabee.Extensions/Zaabee.Extensions.DateTimeOffset.cs @@ -2,50 +2,48 @@ namespace Zaabee.Extensions; public static partial class ZaabeeExtension { - public static IEnumerable EachSecondTo(this DateTimeOffset timeFrom, DateTimeOffset timeTo) + public static IEnumerable 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 EachMinuteTo(this DateTimeOffset timeFrom, DateTimeOffset timeTo) + public static IEnumerable 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 EachHourTo(this DateTimeOffset timeFrom, DateTimeOffset timeTo) + public static IEnumerable 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 EachDayTo(this DateTimeOffset dateFrom, DateTimeOffset dateTo) + public static IEnumerable 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 EachMonthTo(this DateTimeOffset dateFrom, DateTimeOffset dateTo) + public static IEnumerable 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; } } \ No newline at end of file diff --git a/src/Zaabee.Extensions/Zaabee.Extensions.TimeOnly.cs b/src/Zaabee.Extensions/Zaabee.Extensions.TimeOnly.cs new file mode 100644 index 0000000..073286b --- /dev/null +++ b/src/Zaabee.Extensions/Zaabee.Extensions.TimeOnly.cs @@ -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 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 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 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 \ No newline at end of file diff --git a/src/Zaabee.Extensions/Zaabee.Extensions.csproj b/src/Zaabee.Extensions/Zaabee.Extensions.csproj index 34dfb73..e7e7ae1 100644 --- a/src/Zaabee.Extensions/Zaabee.Extensions.csproj +++ b/src/Zaabee.Extensions/Zaabee.Extensions.csproj @@ -3,16 +3,16 @@ netstandard2.0;netcoreapp3.1;net5.0;net6.0 true - 2022.2.0 - 2022.2.0 + 2022.4.0 + 2022.4.0 Mutuduxf Mutuduxf Zaabee;Extensions Exteinsions for C# https://github.com/Mutuduxf/Zaabee.Extensions MIT - enable latest + enable diff --git a/tests/Zaabee.Extensions.UnitTest/DateOnlyExtensionsTest.cs b/tests/Zaabee.Extensions.UnitTest/DateOnlyExtensionsTest.cs new file mode 100644 index 0000000..414e6be --- /dev/null +++ b/tests/Zaabee.Extensions.UnitTest/DateOnlyExtensionsTest.cs @@ -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 \ No newline at end of file diff --git a/tests/Zaabee.Extensions.UnitTest/DateTimeExtensionTest.cs b/tests/Zaabee.Extensions.UnitTest/DateTimeExtensionTest.cs index 4f47d41..1970d1a 100644 --- a/tests/Zaabee.Extensions.UnitTest/DateTimeExtensionTest.cs +++ b/tests/Zaabee.Extensions.UnitTest/DateTimeExtensionTest.cs @@ -2,100 +2,103 @@ namespace Zaabee.Extensions.UnitTest; public class DateTimeExtensionTest { - [Fact] - public void EachSecondToTest() + [Theory] + [InlineData(-1024)] + [InlineData(0)] + [InlineData(1024)] + public void EachSecondToTest(int seconds) { - var dateFrom = DateTime.Now; - var compareTime = new DateTime(dateFrom.Year, dateFrom.Month, dateFrom.Day, dateFrom.Hour, dateFrom.Minute, - dateFrom.Second); - var dateTo = DateTime.Now.AddDays(1).AddMinutes(-1); - var results = dateFrom.EachSecondTo(dateTo); - foreach (var result in results) + var dateFrom = new DateTime(1900, 1, 1); + var dateTo = dateFrom.AddSeconds(seconds); + var dates = dateFrom.EachSecondTo(dateTo).ToList(); + if (seconds < 0) + Assert.Empty(dates); + else + Assert.Equal(seconds + 1, dates.Count); + foreach (var result in dates) { - Assert.Equal(compareTime, result); - compareTime = compareTime.AddSeconds(1); + Assert.Equal(dateFrom, result); + dateFrom = dateFrom.AddSeconds(1); } } - [Fact] - public void EachMinuteToTest() + [Theory] + [InlineData(-1024)] + [InlineData(0)] + [InlineData(1024)] + public void EachMinuteToTest(int minutes) { - var dateFrom = DateTime.Now; - var compareTime = new DateTime(dateFrom.Year, dateFrom.Month, dateFrom.Day, dateFrom.Hour, dateFrom.Minute, - 0); - var dateTo = DateTime.Now.AddDays(1).AddMinutes(-1); - var results = dateFrom.EachMinuteTo(dateTo); - foreach (var result in results) + var dateFrom = new DateTime(1900, 1, 1); + var dateTo = dateFrom.AddMinutes(minutes); + var dates = dateFrom.EachMinuteTo(dateTo).ToList(); + if (minutes < 0) + Assert.Empty(dates); + else + Assert.Equal(minutes + 1, dates.Count); + foreach (var result in dates) { - Assert.Equal(compareTime, result); - compareTime = compareTime.AddMinutes(1); + Assert.Equal(dateFrom, result); + dateFrom = dateFrom.AddMinutes(1); } } - [Fact] - public void EachHourToTest() + [Theory] + [InlineData(-1024)] + [InlineData(0)] + [InlineData(1024)] + public void EachHourToTest(int hours) { - var dateFrom = DateTime.Now; - var compareTime = new DateTime(dateFrom.Year, dateFrom.Month, dateFrom.Day, dateFrom.Hour, 0, 0); - var dateTo = DateTime.Now.AddDays(1).AddMinutes(-1); - var results = dateFrom.EachHourTo(dateTo); - foreach (var result in results) + var dateFrom = new DateTime(1900, 1, 1); + var dateTo = dateFrom.AddHours(hours); + var dates = dateFrom.EachHourTo(dateTo).ToList(); + if (hours < 0) + Assert.Empty(dates); + else + Assert.Equal(hours + 1, dates.Count); + foreach (var result in dates) { - Assert.Equal(compareTime, result); - compareTime = compareTime.AddHours(1); + Assert.Equal(dateFrom, result); + dateFrom = dateFrom.AddHours(1); } } - [Fact] - public void EachDayToTest() + [Theory] + [InlineData(-1024)] + [InlineData(0)] + [InlineData(1024)] + public void EachDayToTest(int days) { - var dateFrom = DateTime.Now; - var dateTo = DateTime.Now.AddDays(7).AddHours(-1); - var results = dateFrom.EachDayTo(dateTo); - foreach (var result in results) + var dateFrom = new DateTime(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.Date, result); + Assert.Equal(dateFrom, result); dateFrom = dateFrom.AddDays(1); } } [Theory] - [InlineData(-1)] - [InlineData(-7)] [InlineData(-1024)] - public void EachDayDateToLessThanDateFromTest(int days) - { - var dateFrom = DateTime.Now; - var dateTo = DateTime.Now.AddDays(days); - var results = dateFrom.EachDayTo(dateTo); - Assert.Empty(results); - } - - [Theory] - [InlineData(1)] - [InlineData(7)] + [InlineData(0)] [InlineData(1024)] public void EachMonthToTest(int months) { - var dateFrom = DateTime.Now; - var dateTo = DateTime.Now.AddMonths(months).AddDays(-1); - var results = dateFrom.EachMonthTo(dateTo); - foreach (var result in results) + var dateFrom = new DateTime(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.Date, result); + Assert.Equal(dateFrom, result); dateFrom = dateFrom.AddMonths(1); } } - - [Theory] - [InlineData(-1)] - [InlineData(-7)] - [InlineData(-1024)] - public void EachMonthDateToLessThanDateFromTest(int months) - { - var dateFrom = DateTime.Now; - var dateTo = DateTime.Now.AddMonths(months); - var results = dateFrom.EachMonthTo(dateTo); - Assert.Empty(results); - } } \ No newline at end of file diff --git a/tests/Zaabee.Extensions.UnitTest/DateTimeOffsetExtensionTest.cs b/tests/Zaabee.Extensions.UnitTest/DateTimeOffsetExtensionTest.cs index 8ccfd45..ce6ed91 100644 --- a/tests/Zaabee.Extensions.UnitTest/DateTimeOffsetExtensionTest.cs +++ b/tests/Zaabee.Extensions.UnitTest/DateTimeOffsetExtensionTest.cs @@ -2,101 +2,103 @@ namespace Zaabee.Extensions.UnitTest; public class DateTimeOffsetExtensionTest { - [Fact] - public void EachSecondToTest() + [Theory] + [InlineData(-1024)] + [InlineData(0)] + [InlineData(1024)] + public void EachSecondToTest(int seconds) { - var dateFrom = DateTimeOffset.Now; - var compareTime = new DateTimeOffset(dateFrom.Year, dateFrom.Month, dateFrom.Day, - dateFrom.Hour, dateFrom.Minute, dateFrom.Second, DateTimeOffset.Now.Offset); - var dateTo = DateTimeOffset.Now.AddDays(1).AddMinutes(-1); - var results = dateFrom.EachSecondTo(dateTo); - foreach (var result in results) + var dateFrom = new DateTimeOffset(new DateTime(1900, 1, 1)); + var dateTo = dateFrom.AddSeconds(seconds); + var dates = dateFrom.EachSecondTo(dateTo).ToList(); + if (seconds < 0) + Assert.Empty(dates); + else + Assert.Equal(seconds + 1, dates.Count); + foreach (var result in dates) { - Assert.Equal(compareTime, result); - compareTime = compareTime.AddSeconds(1); + Assert.Equal(dateFrom, result); + dateFrom = dateFrom.AddSeconds(1); } } - [Fact] - public void EachMinuteToTest() + [Theory] + [InlineData(-1024)] + [InlineData(0)] + [InlineData(1024)] + public void EachMinuteToTest(int minutes) { - var dateFrom = DateTimeOffset.Now; - var compareTime = new DateTimeOffset(dateFrom.Year, dateFrom.Month, dateFrom.Day, - dateFrom.Hour, dateFrom.Minute, 0, DateTimeOffset.Now.Offset); - var dateTo = DateTimeOffset.Now.AddDays(1).AddMinutes(-1); - var results = dateFrom.EachMinuteTo(dateTo); - foreach (var result in results) + var dateFrom = new DateTimeOffset(new DateTime(1900, 1, 1)); + var dateTo = dateFrom.AddMinutes(minutes); + var dates = dateFrom.EachMinuteTo(dateTo).ToList(); + if (minutes < 0) + Assert.Empty(dates); + else + Assert.Equal(minutes + 1, dates.Count); + foreach (var result in dates) { - Assert.Equal(compareTime, result); - compareTime = compareTime.AddMinutes(1); + Assert.Equal(dateFrom, result); + dateFrom = dateFrom.AddMinutes(1); } } - [Fact] - public void EachHourToTest() + [Theory] + [InlineData(-1024)] + [InlineData(0)] + [InlineData(1024)] + public void EachHourToTest(int hours) { - var dateFrom = DateTimeOffset.Now; - var compareTime = new DateTimeOffset(dateFrom.Year, dateFrom.Month, dateFrom.Day, - dateFrom.Hour, 0, 0, DateTimeOffset.Now.Offset); - var dateTo = DateTimeOffset.Now.AddDays(1).AddMinutes(-1); - var results = dateFrom.EachHourTo(dateTo); - foreach (var result in results) + var dateFrom = new DateTimeOffset(new DateTime(1900, 1, 1)); + var dateTo = dateFrom.AddHours(hours); + var dates = dateFrom.EachHourTo(dateTo).ToList(); + if (hours < 0) + Assert.Empty(dates); + else + Assert.Equal(hours + 1, dates.Count); + foreach (var result in dates) { - Assert.Equal(compareTime, result); - compareTime = compareTime.AddHours(1); + Assert.Equal(dateFrom, result); + dateFrom = dateFrom.AddHours(1); } } - [Fact] - public void EachDayToTest() + [Theory] + [InlineData(-1024)] + [InlineData(0)] + [InlineData(1024)] + public void EachDayToTest(int days) { - var dateFrom = DateTimeOffset.Now; - var dateTo = DateTimeOffset.Now.AddDays(7).AddHours(-1); - var results = dateFrom.EachDayTo(dateTo); - foreach (var result in results) + var dateFrom = new DateTimeOffset(new DateTime(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.Date, result); + Assert.Equal(dateFrom, result); dateFrom = dateFrom.AddDays(1); } } [Theory] - [InlineData(-1)] - [InlineData(-7)] [InlineData(-1024)] - public void EachDayDateToLessThanDateFromTest(int days) - { - var dateFrom = DateTimeOffset.Now; - var dateTo = DateTimeOffset.Now.AddDays(days); - var results = dateFrom.EachDayTo(dateTo); - Assert.Empty(results); - } - - [Theory] - [InlineData(1)] - [InlineData(7)] + [InlineData(0)] [InlineData(1024)] public void EachMonthToTest(int months) { - var dateFrom = DateTimeOffset.Now; - var dateTo = DateTimeOffset.Now.AddMonths(months).AddDays(-1); - var results = dateFrom.EachMonthTo(dateTo); - foreach (var result in results) + var dateFrom = new DateTimeOffset(new DateTime(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.Date, result); + Assert.Equal(dateFrom, result); dateFrom = dateFrom.AddMonths(1); } } - - [Theory] - [InlineData(-1)] - [InlineData(-7)] - [InlineData(-1024)] - public void EachMonthDateToLessThanDateFromTest(int months) - { - var dateFrom = DateTimeOffset.Now; - var dateTo = DateTimeOffset.Now.AddMonths(months); - var results = dateFrom.EachMonthTo(dateTo); - Assert.Empty(results); - } } \ No newline at end of file diff --git a/tests/Zaabee.Extensions.UnitTest/TimeOnlyExtensionsTest.cs b/tests/Zaabee.Extensions.UnitTest/TimeOnlyExtensionsTest.cs new file mode 100644 index 0000000..783e3f7 --- /dev/null +++ b/tests/Zaabee.Extensions.UnitTest/TimeOnlyExtensionsTest.cs @@ -0,0 +1,54 @@ +#if !NET48 && !NETCOREAPP3_1 && !NET5_0 +namespace Zaabee.Extensions.UnitTest; + +public class TimeOnlyExtensionsTest +{ + [Theory] + [InlineData(-1024)] + [InlineData(0)] + [InlineData(1024)] + public void EachSecondToTest(int seconds) + { + var timeFrom = new TimeOnly(12, 0, 0); + var timeTo = timeFrom.AddSeconds(seconds); + var times = timeFrom.EachSecondTo(timeTo).ToList(); + foreach (var result in times) + { + Assert.Equal(timeFrom, result); + timeFrom = timeFrom.AddSeconds(1); + } + } + + [Theory] + [InlineData(-1024)] + [InlineData(0)] + [InlineData(1024)] + public void EachMinuteToTest(int minutes) + { + var timeFrom = new TimeOnly(12, 0, 0); + var timeTo = timeFrom.AddMinutes(minutes); + var times = timeFrom.EachMinuteTo(timeTo).ToList(); + foreach (var result in times) + { + Assert.Equal(timeFrom, result); + timeFrom = timeFrom.AddMinutes(1); + } + } + + [Theory] + [InlineData(-11)] + [InlineData(0)] + [InlineData(11)] + public void EachHourToTest(int hours) + { + var timeFrom = new TimeOnly(12, 0, 0); + var timeTo = timeFrom.AddHours(hours); + var times = timeFrom.EachHourTo(timeTo).ToList(); + foreach (var result in times) + { + Assert.Equal(timeFrom, result); + timeFrom = timeFrom.AddHours(1); + } + } +} +#endif \ No newline at end of file