我想要編寫以下功能,將n
工作日數添加到DateTime
。但是,我希望工作日可以配置:將營業日添加到日期時間營業日可配置
public DateTime AddBusinessDays(DateTime dateTime, int n, IEnumerable<DayOfWeek> businessDays)
{
// ?
}
例如,週三可能是假的,所以我想只跳過這一天:
DateTime.Today.AddBusinessDays(
7,
new DayOfWeek[]
{
DayOfWeek.Monday,
DayOfWeek.Tuesday,
DayOfWeek.Thursday,
DayOfWeek.Friday,
DayOfWeek.Saturday,
DayOfWeek.Sunday,
});
我意識到,我可以寫一個天真的實現過目每一天,但是我正在尋找一個更好的實現。
UPDATE
沒有工作的例子,到目前爲止,這裏有一些xUnit的測試,以幫助測試不同的方案:
[Theory]
// Zero Values
[InlineData("01/01/2016", 0, "Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday", "01/01/2016")]
[InlineData("01/01/2016", 0, "Saturday", "01/01/2016")]
// Positive Days
[InlineData("01/01/2016", 1, "Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday", "02/01/2016")]
[InlineData("01/01/2016", 30, "Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday", "31/01/2016")]
[InlineData("01/01/2016", 1, "Saturday", "02/01/2016")]
[InlineData("01/01/2016", 2, "Saturday", "09/01/2016")]
[InlineData("01/01/2016", 3, "Saturday", "16/01/2016")]
[InlineData("01/01/2016", 7, "Monday,Tuesday,Wednesday,Thursday,Friday,Saturday", "08/01/2016")]
// Negative Days
[InlineData("01/01/2016", -1, "Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday", "31/12/2015")]
[InlineData("01/01/2016", -31, "Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday", "01/12/2015")]
[InlineData("01/01/2016", -1, "Saturday", "26/12/2015")]
[InlineData("01/01/2016", -2, "Saturday", "19/12/2015")]
[InlineData("01/01/2016", -3, "Saturday", "12/12/2015")]
public void AddBusinessDays(string start, int days, string businessDays, string expectedEnd)
{
var daysOfWeek = businessDays.Split(',').Select(x => (DayOfWeek)Enum.Parse(typeof(DayOfWeek), x));
var actualEnd = DateTime.Parse(start).AddBusinessDays(days, daysOfWeek);
Assert.Equal(DateTime.Parse(expectedEnd), actualEnd);
}
你會更好,把一週的天數爲某種形式的HashSet的,這樣你就不會遍歷所有元素 - 所有可能會有人經過要添加10,000天到一個日期,這將得到sloooow – Jamiec
我剛剛測試添加10 000天,它在8毫秒。所以這很慢。 ;) –
它像2線更換容易'var bdLookup = new HashSet(businessDays)'和'bdLookup.Contains(tmpDate.DayOfWeek)' – Jamiec