Skip to content

Commit

Permalink
Fix FindDayBetween bug with short range
Browse files Browse the repository at this point in the history
  • Loading branch information
tinohager committed Aug 29, 2022
1 parent af0925e commit 9c0ad04
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
41 changes: 33 additions & 8 deletions src/Nager.Date.UnitTest/Common/LogicTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,44 @@ public void CheckFindDayBefore()
}

[TestMethod]
public void CheckFindDayBetween()
public void CheckFindDayBetween1()
{
var result = DateSystem.FindDayBetween(2019, 7, 1, 2019, 7, 7, DayOfWeek.Tuesday);
Assert.AreEqual(new DateTime(2019, 7, 2), result);
Assert.IsNotNull(result);
Assert.AreEqual(new DateTime(2019, 7, 2), result.Value);
}


result = DateSystem.FindDayBetween(2019, 7, 1, 2019, 7, 7, DayOfWeek.Wednesday);
Assert.AreEqual(new DateTime(2019, 7, 3), result);
[TestMethod]
public void CheckFindDayBetween2()
{
var result = DateSystem.FindDayBetween(2019, 7, 1, 2019, 7, 7, DayOfWeek.Wednesday);
Assert.IsNotNull(result);
Assert.AreEqual(new DateTime(2019, 7, 3), result.Value);
}

result = DateSystem.FindDayBetween(2019, 7, 1, 2019, 7, 7, DayOfWeek.Friday);
Assert.AreEqual(new DateTime(2019, 7, 5), result);

result = DateSystem.FindDayBetween(2019, 7, 1, 2019, 7, 7, DayOfWeek.Saturday);
Assert.AreEqual(new DateTime(2019, 7, 6), result);
[TestMethod]
public void CheckFindDayBetween3()
{
var result = DateSystem.FindDayBetween(2019, 7, 1, 2019, 7, 7, DayOfWeek.Friday);
Assert.IsNotNull(result);
Assert.AreEqual(new DateTime(2019, 7, 5), result.Value);
}

[TestMethod]
public void CheckFindDayBetween4()
{
var result = DateSystem.FindDayBetween(2019, 7, 1, 2019, 7, 7, DayOfWeek.Saturday);
Assert.IsNotNull(result);
Assert.AreEqual(new DateTime(2019, 7, 6), result.Value);
}

[TestMethod]
public void CheckFindDayBetween5()
{
var result = DateSystem.FindDayBetween(2022, 08, 25, 2022, 08, 28, DayOfWeek.Tuesday);
Assert.IsNull(result);
}

[TestMethod]
Expand Down
12 changes: 9 additions & 3 deletions src/Nager.Date/DateSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ public static DateTime FindDay(DateTime date, DayOfWeek dayOfWeek)
/// <param name="dayEnd">The end day</param>
/// <param name="dayOfWeek">The name of the day</param>
/// <returns>Date of day found</returns>
public static DateTime FindDayBetween(int yearStart, int monthStart, int dayStart, int yearEnd, int monthEnd, int dayEnd, DayOfWeek dayOfWeek)
public static DateTime? FindDayBetween(int yearStart, int monthStart, int dayStart, int yearEnd, int monthEnd, int dayEnd, DayOfWeek dayOfWeek)
{
var startDay = new DateTime(yearStart, monthStart, dayStart);
var endDay = new DateTime(yearEnd, monthEnd, dayEnd);
Expand All @@ -582,7 +582,13 @@ public static DateTime FindDayBetween(int yearStart, int monthStart, int dayStar
}

}
return startDay;

if (startDay.DayOfWeek == dayOfWeek)
{
return startDay;
}

return null;
}

/// <summary>
Expand All @@ -592,7 +598,7 @@ public static DateTime FindDayBetween(int yearStart, int monthStart, int dayStar
/// <param name="endDate">The end date</param>
/// <param name="dayOfWeek">The name of the day</param>
/// <returns>Date of day found</returns>
public static DateTime FindDayBetween(DateTime startDate, DateTime endDate, DayOfWeek dayOfWeek)
public static DateTime? FindDayBetween(DateTime startDate, DateTime endDate, DayOfWeek dayOfWeek)
{
return FindDayBetween(startDate.Year, startDate.Month, startDate.Day, endDate.Year, endDate.Month, endDate.Day, dayOfWeek);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Nager.Date/PublicHolidays/FinlandProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public IEnumerable<PublicHoliday> Get(int year)
items.Add(this._catholicProvider.Pentecost("Helluntaipäivä", year, countryCode));
items.Add(new PublicHoliday(midsummerEve, "Juhannusaatto", "Midsummer Eve", countryCode));
items.Add(new PublicHoliday(midsummerDay, "Juhannuspäivä", "Midsummer Day", countryCode));
items.Add(new PublicHoliday(allSaintsDay, "Pyhäinpäivä", "All Saints' Day", countryCode));
items.Add(new PublicHoliday(allSaintsDay.Value, "Pyhäinpäivä", "All Saints' Day", countryCode));
items.Add(new PublicHoliday(year, 12, 6, "Itsenäisyyspäivä", "Independence Day", countryCode));
items.Add(new PublicHoliday(year, 12, 24, "Jouluaatto", "Christmas Eve", countryCode));
items.Add(new PublicHoliday(year, 12, 25, "Joulupäivä", "Christmas Day", countryCode));
Expand Down

0 comments on commit 9c0ad04

Please sign in to comment.