Created
June 13, 2016 06:50
-
-
Save tohagan/814a65380d9c8a7f13538584ff4aad19 to your computer and use it in GitHub Desktop.
Efficiently compute WorkdaysDiff() and AddWorkdays() with unit tests.
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
public static class DateTimeExtensions | |
{ | |
#region Add/Diff Weekdays | |
// Transformed for SQL which numbers Mon=0 and Sun=6 | |
// 0123444401233334012222340111123400012345001234550 | |
private static readonly int[,] _addOffset = | |
{ | |
// 0 1 2 3 4 | |
{0, 1, 2, 3, 4}, // Su 0 | |
{0, 1, 2, 3, 4}, // M 1 | |
{0, 1, 2, 3, 6}, // Tu 2 | |
{0, 1, 4, 5, 6}, // W 3 | |
{0, 1, 4, 5, 6}, // Th 4 | |
{0, 3, 4, 5, 6}, // F 5 | |
{0, 2, 3, 4, 5}, // Sa 6 | |
}; | |
public static DateTime AddWeekdays(this DateTime date, int weekdays) | |
{ | |
int extraDays = weekdays % 5; | |
int addDays = weekdays >= 0 | |
? (weekdays / 5) * 7 + _addOffset[(int)date.DayOfWeek, extraDays] | |
: (weekdays / 5) * 7 - _addOffset[6 - (int)date.DayOfWeek, -extraDays]; | |
return date.AddDays(addDays); | |
} | |
static readonly int[,] _diffOffset = | |
{ | |
// Su M Tu W Th F Sa | |
{0, 1, 2, 3, 4, 5, 5}, // Su | |
{4, 0, 1, 2, 3, 4, 4}, // M | |
{3, 4, 0, 1, 2, 3, 3}, // Tu | |
{2, 3, 4, 0, 1, 2, 2}, // W | |
{1, 2, 3, 4, 0, 1, 1}, // Th | |
{0, 1, 2, 3, 4, 0, 0}, // F | |
{0, 1, 2, 3, 4, 5, 0}, // Sa | |
}; | |
public static int GetWeekdaysDiff(this DateTime dtStart, DateTime dtEnd) | |
{ | |
int daysDiff = (int)(dtEnd - dtStart).TotalDays; | |
return daysDiff >= 0 | |
? 5 * (daysDiff / 7) + _diffOffset[(int) dtStart.DayOfWeek, (int) dtEnd.DayOfWeek] | |
: 5 * (daysDiff / 7) - _diffOffset[6 - (int) dtStart.DayOfWeek, 6 - (int) dtEnd.DayOfWeek]; | |
} | |
#endregion // Weekdays | |
} |
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using NUnit.Framework; | |
using ShouldBe; | |
[TestFixture] | |
public class DateTimeExtensionsTests | |
{ | |
/// <summary> | |
/// Exclude start date, Include end date | |
/// </summary> | |
/// <param name="dtStart"></param> | |
/// <param name="dtEnd"></param> | |
/// <returns></returns> | |
private IEnumerable<DateTime> GetDateRange(DateTime dtStart, DateTime dtEnd) | |
{ | |
Console.WriteLine(@"dtStart={0:yy-MMM-dd ddd}, dtEnd={1:yy-MMM-dd ddd}", dtStart, dtEnd); | |
TimeSpan diff = dtEnd - dtStart; | |
Console.WriteLine(diff); | |
if (dtStart <= dtEnd) | |
{ | |
for (DateTime dt = dtStart.AddDays(1); dt <= dtEnd; dt = dt.AddDays(1)) | |
{ | |
Console.WriteLine(@"dt={0:yy-MMM-dd ddd}", dt); | |
yield return dt; | |
} | |
} | |
else | |
{ | |
for (DateTime dt = dtStart.AddDays(-1); dt >= dtEnd; dt = dt.AddDays(-1)) | |
{ | |
Console.WriteLine(@"dt={0:yy-MMM-dd ddd}", dt); | |
yield return dt; | |
} | |
} | |
} | |
[Test, Combinatorial] | |
public void TestGetWeekdaysDiff( | |
[Values(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 20, 30)] | |
int startDay, | |
[Values(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 20, 30)] | |
int endDay, | |
[Values(7)] | |
int startMonth, | |
[Values(7)] | |
int endMonth) | |
{ | |
// Arrange | |
DateTime dtStart = new DateTime(2016, startMonth, startDay); | |
DateTime dtEnd = new DateTime(2016, endMonth, endDay); | |
int nDays = GetDateRange(dtStart, dtEnd) | |
.Count(dt => dt.DayOfWeek != DayOfWeek.Saturday && dt.DayOfWeek != DayOfWeek.Sunday); | |
if (dtEnd < dtStart) nDays = -nDays; | |
Console.WriteLine(@"countBusDays={0}", nDays); | |
// Act / Assert | |
dtStart.GetWeekdaysDiff(dtEnd).ShouldBe(nDays); | |
} | |
[Test, Combinatorial] | |
public void TestAddWeekdays( | |
[Values(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 20, 30)] | |
int startDay, | |
[Values(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 20, 30)] | |
int weekdays) | |
{ | |
DateTime dtStart = new DateTime(2016, 7, startDay); | |
DateTime dtEnd1 = dtStart.AddWeekdays(weekdays); | |
dtStart.GetWeekdaysDiff(dtEnd1).ShouldBe(weekdays); // ADD | |
DateTime dtEnd2 = dtStart.AddWeekdays(-weekdays); | |
dtStart.GetWeekdaysDiff(dtEnd2).ShouldBe(-weekdays); // SUBTRACT | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment