131 lines
5.5 KiB
C#
131 lines
5.5 KiB
C#
using Qdp.Foundation.Implementations;
|
|
using Qdp.Pricing.Base.Implementations;
|
|
|
|
namespace YLErp.QdpModule
|
|
{
|
|
[TestClass]
|
|
public class CalendarTest
|
|
{
|
|
[TestMethod]
|
|
public void TestBizDaysBetweenDates()
|
|
{
|
|
var holiday = new CalendarHoliday
|
|
{
|
|
HolidayList = new List<string> { "2022,01,01", "2022,01,02", "2022,01,03", "2022,01,22", "2023,01,01" }
|
|
};
|
|
|
|
CalendarImpl.InitializeCalendarData(new Dictionary<string, CalendarHoliday> {
|
|
{"chn", holiday}
|
|
});
|
|
|
|
Assert.IsTrue(QdpCalendarHelper.IsHoliday(new DateTime(2022, 1, 1)));
|
|
Assert.IsTrue(QdpCalendarHelper.IsHoliday(new DateTime(2022, 1, 22)));
|
|
Assert.IsFalse(QdpCalendarHelper.IsHoliday(new DateTime(2022, 1, 9)));
|
|
Assert.IsFalse(QdpCalendarHelper.IsHoliday(new DateTime(2022, 1, 23)));
|
|
|
|
var qd1 = QdpCalendarHelper.GetNonHoliday(new DateTime(2022, 1, 1));
|
|
var qd2 = QdpCalendarHelper.GetNonHoliday(new DateTime(2022, 1, 31));
|
|
|
|
var calendar = CalendarImpl.Get("chn");
|
|
|
|
//BizDaysBetweenDates这个方法返回包含起始日不包含结束日的所有交易日列表
|
|
var observationDates = calendar.BizDaysBetweenDates(qd1, qd2);
|
|
Assert.IsTrue(observationDates.First() == (Date)new DateTime(2022, 1, 4));
|
|
Assert.IsTrue(observationDates.Contains(new DateTime(2022, 1, 9)));
|
|
Assert.IsFalse(observationDates.Contains(new DateTime(2022, 1, 22)));
|
|
Assert.IsFalse(observationDates.Contains(new DateTime(2022, 1, 31)));
|
|
|
|
//BizDaysBetweenDatesInclEndDay这个方法返回包含起始日包含结束日的所有交易日列表
|
|
observationDates = calendar.BizDaysBetweenDatesInclEndDay(qd1, qd2);
|
|
Assert.IsTrue(observationDates.First() == (Date)new DateTime(2022, 1, 4));
|
|
Assert.IsTrue(observationDates.Contains(new DateTime(2022, 1, 9)));
|
|
Assert.IsFalse(observationDates.Contains(new DateTime(2022, 1, 22)));
|
|
Assert.IsTrue(observationDates.Contains(new DateTime(2022, 1, 31)));
|
|
|
|
//BizDaysBetweenDatesExcluStartDay这个方法返回不包含起始日包含结束日的所有交易日列表
|
|
observationDates = calendar.BizDaysBetweenDatesExcluStartDay(qd1, qd2);
|
|
Assert.IsTrue(observationDates.First() == (Date)new DateTime(2022, 1, 5));
|
|
Assert.IsTrue(observationDates.Contains(new DateTime(2022, 1, 9)));
|
|
Assert.IsFalse(observationDates.Contains(new DateTime(2022, 1, 22)));
|
|
Assert.IsTrue(observationDates.Contains(new DateTime(2022, 1, 31)));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void TestGetWorkingDayCount()
|
|
{
|
|
var holiday = new CalendarHoliday
|
|
{
|
|
HolidayList = new List<string> { "2022,01,01", "2022,01,02", "2022,01,03", "2022,01,22",
|
|
"2022,09,03", "2022,09,04", "2022,09,10", "2022,09,11", "2022,09,12","2022,09,17", "2022,09,18","2023,01,01" }
|
|
};
|
|
|
|
CalendarImpl.InitializeCalendarData(new Dictionary<string, CalendarHoliday> {
|
|
{"chn", holiday}
|
|
});
|
|
|
|
var (d1, sd1, ed1) = GetWorkingDayCount(new DateTime(2022, 9, 1), new DateTime(2022, 9, 13));
|
|
Assert.AreEqual(d1, 8);
|
|
Assert.AreEqual(sd1, new DateTime(2022, 9, 1));
|
|
Assert.AreEqual(ed1, new DateTime(2022, 9, 13));
|
|
|
|
var (d2, sd2, ed2) = GetWorkingDayCount(new DateTime(2022, 9, 11), new DateTime(2022, 9, 13));
|
|
Assert.AreEqual(d2, 1);
|
|
Assert.AreEqual(sd2, new DateTime(2022, 9, 13));
|
|
Assert.AreEqual(ed2, new DateTime(2022, 9, 13));
|
|
|
|
var (d3, sd3, ed3) = GetWorkingDayCount(new DateTime(2022, 9, 3), new DateTime(2022, 9, 13));
|
|
Assert.AreEqual(d3, 6);
|
|
Assert.AreEqual(sd3, new DateTime(2022, 9, 5));
|
|
Assert.AreEqual(ed3, new DateTime(2022, 9, 13));
|
|
|
|
var (d4, sd4, ed4) = GetWorkingDayCount(new DateTime(2022, 9, 3), new DateTime(2022, 9, 4));
|
|
Assert.AreEqual(d4, 0);
|
|
Assert.AreEqual(sd4, null);
|
|
Assert.AreEqual(ed4, null);
|
|
}
|
|
|
|
private (int daycount, DateTime? sdate, DateTime? edate) GetWorkingDayCount(DateTime reqStartDate, DateTime reqEndDate, string reqCalendarName = null)
|
|
{
|
|
var daycount = 0;
|
|
var calendar = CalendarImpl.Get(reqCalendarName.TrimToNull() ?? "chn");
|
|
|
|
DateTime? startDate = null, endDate = null;
|
|
|
|
for (var date = reqStartDate; date <= reqEndDate; date = date.AddDays(1))
|
|
{
|
|
if (!calendar.IsHoliday(date))
|
|
{
|
|
startDate = date;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (startDate.HasValue)
|
|
{
|
|
daycount = 1;
|
|
|
|
endDate = startDate;
|
|
|
|
for (var date = reqEndDate; date > startDate.Value; date = date.AddDays(-1))
|
|
{
|
|
if (!calendar.IsHoliday(date))
|
|
{
|
|
endDate = date;
|
|
break;
|
|
}
|
|
}
|
|
|
|
for (var date = startDate.Value; date < endDate.Value; date = date.AddDays(1))
|
|
{
|
|
if (!calendar.IsHoliday(date))
|
|
{
|
|
daycount++;
|
|
}
|
|
}
|
|
}
|
|
|
|
return (daycount, startDate, endDate);
|
|
}
|
|
}
|
|
}
|