using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
namespace UserDefinedDataEXP
{
public class Date
{
private DateTime dt;
private int _diffYear,_diffMonth,_diffDay;
#region 属性Year、Month、Day、SerialValue
//0.1 属性Year
public int Year
{
get { return dt.Year; }
}
//0.2 属性Month
public int Month
{
get { return dt.Month; }
}
//0.3 属性Day
public int Day
{
get { return dt.Day; }
}
//0.4 属性SerialValue
public int SerialValue
{
get { return dt.Subtract(new DateTime(1900, 1, 1)).Days + 2; }
}
//0.5 属性DateValue
public DateTime DateValue
{
get { return dt; }
}
#endregion
#region 改写运算符
//1. Operator +
public static Date operator +(Date D1,Date D2)
{
Date result = new Date(D1.Year+D2.Year,D1.Month+D2.Month,D1.Day+D2.Day);
return result;
}
//2. Operator -
public static Date operator -(Date D1, Date D2)
{
Date result = new Date(D1.Year - D2.Year, D1.Month- D2.Month, D1.Day - D2.Day);
return result;
}
//3. Operator ==
public static bool operator ==(Date D1, Date D2)
{
if (D1.Year == D2.Year && D1.Month == D2.Month && D1.Day == D2.Day)
{
return true;
}
else
{
return false;
}
}
//4. Operator !=
public static bool operator !=(Date D1, Date D2)
{
if (D1.Year != D2.Year || D1.Month != D2.Month || D1.Day != D2.Day)
{
return true;
}
else
{
return false;
}
}
//5. Operator >
public static bool operator >(Date D1, Date D2)
{
if (D1.SerialValue>D2.SerialValue)
{
return true;
}
else
{
return false;
}
}
//6. Operator >
public static bool operator <(Date D1, Date D2)
{
if (D1.SerialValue < D2.SerialValue)
{
return true;
}
else
{
return false;
}
}
#endregion
#region 构造器
//1.1 构造器
public Date()
{
dt = new DateTime();
}
//1.2 构造器
public Date(int Year, int Month, int Day)
{
dt = new DateTime(Year,Month,Day);
}
//1.3 构造器
public Date(DateTime date)
{
dt = new DateTime(date.Year,date.Month,date.Day);
}
//1.4 构造器
public Date(int ExcelSerialNum)
{
dt = new DateTime(1900,1,1).AddDays(ExcelSerialNum-2);
}
//1.5 构造器
public Date(Date dt2)
{
dt = new DateTime(dt2.dt.Year,dt2.dt.Month,dt2.dt.Day);
}
#endregion
#region YearFactor
//3.1 Act/Act
public double YF_AA(Date endDate)
{
return D_MM(endDate) / (DateTime.IsLeapYear(this.Year) ? 366 : 365);
}
//3.2 Act/360
public double YF_MM(Date endDate)
{
return (endDate.SerialValue - this.SerialValue) / 360.0;
}
public double YF_30_360(Date endDate)
{
return D_30_360(endDate) / 360.0;
}
public double YF_30_360E(Date endDate)
{
return D_30_360E(endDate) / 360.0;
}
public double YF_30_360E_ISDA(Date endDate)
{
return D_30_360E_ISDA(endDate) / 360.0;
}
public double YF_30_365(Date endDate)
{
return D_30_360(endDate) / 365.0;
}
//3.4 【改】YF_365 = Act/365
public double YF_365(Date endDate)
{
return D_MM(endDate) / 365.0;
}
//3.5 【改】YF_365_25 = Act/365.25
public double YF_365_25(Date endDate)
{
return D_MM(endDate)/ 365.25;
}
#endregion
函数End Of Month
//public int EOM(Date date)
//{
// int _eom=30;
// switch (date.Month)
// {
// case 2:
// if (DateTime.IsLeapYear(date.Year)) _eom = 29;
// else _eom = 28;
// break;
// case 1:
// case 3:
// case 5:
// case 7:
// case 8:
// case 10:
// case 12:
// _eom=31;
// break;
// default:
// break;
// }
// return _eom;
//}
//4.1 Day Count
#region Day Count
public int D_MM(Date endDate)
{
return endDate.SerialValue - this.SerialValue;
}
public int D_30_360(Date endDate)
{
_diffYear = endDate.Year - this.Year;
_diffMonth = endDate.Month - this.Month;
_diffDay = endDate.Day - dt.Day;
return 360*_diffYear + 30 * _diffMonth + _diffDay;
}
public int D_30_360E(Date endDate)
{
_diffYear = endDate.Year - this.Year;
_diffMonth = endDate.Month - this.Month;
if (endDate.Day == 31) _diffDay = 30;
else _diffDay = endDate.Day;
if (this.Day == 31) _diffDay -= 30;
else _diffDay -= this.Day;
return 360*_diffYear + 30 * _diffMonth + _diffDay;
}
public int D_30_360E_ISDA(Date endDate)
{
_diffYear = endDate.Year - this.Year;
_diffMonth = endDate.Month - this.Month;
if (endDate.Day ==DateTime.DaysInMonth(endDate.Year,endDate.Month)) _diffDay = 30;
else _diffDay = endDate.Day;
if (this.Day == 31) _diffDay -= 30;
else _diffDay -= this.Day;
return 360*_diffYear + 30 * _diffMonth + _diffDay;
}
public double D_EFF(Date Date2)
{
return Date2.SerialValue - this.SerialValue;
}
#endregion
//5.1 add_workdays()
public Date add_workdays(int workdays)
{
DateTime dt1 = dt;
for (int i = 0; i < workdays; i++)
{
switch (dt.DayOfWeek)
{
case DayOfWeek.Saturday:
dt1 = dt.AddDays(2);
break;
case DayOfWeek.Friday:
dt1 = dt.AddDays(3);
break;
default:
dt1 = dt1.AddDays(1);
break;
}
}
return new Date(dt1);
}
//8. 函数add_period_string
public Date add_period_string(string addPeriod, int n)
{
DateTime dt1 = dt;
switch (addPeriod.Substring(1,1))
{
case "d":
dt1 = dt1.AddDays(double.Parse(addPeriod.Substring(0, 1)));
break;
case "m":
dt1 = dt1.AddMonths(int.Parse(addPeriod.Substring(0,1)));
break;
case "y":
dt1 = dt1.AddYears(int.Parse(addPeriod.Substring(0,1)));
break;
}
if (dt1.DayOfWeek==DayOfWeek.Sunday||dt1.DayOfWeek==DayOfWeek.Saturday)
{
return new Date(dt1).add_workdays(n);
}
else
{
return new Date(dt1);
}
}
//9. 属性FormatedDate
public void PrintFormatedDate()
{
Console.Write(dt.ToString("ddd_dd_MMM_yyyy", DateTimeFormatInfo.InvariantInfo));
}
//10. 函数AddMonth
public Date AddMonths(int month)
{
return new Date(dt.AddMonths(month));
}
//【更新】2015/3/11
//给出指定年月的第三个周三的日期
public static Date IMMDate(int Month, int Year)
{
// Start with last day of previous month
DateTime outPut = new DateTime(Year, Month, 1).AddDays(-1);
int NoOfWednesday = 0; // Wednesday counter
// Loop to find third Wednesday
while (NoOfWednesday < 3)
{
outPut = outPut.AddDays(1);
if (outPut.DayOfWeek == DayOfWeek.Wednesday)
{
NoOfWednesday++;
}
}
return new Date(outPut);
}
public Date IMMDate()
{
return IMMDate(dt.Month, dt.Year);
}
//给出距离Today第NthStir近的一个IMM Date
public static Date IMM_Date_Nth(Date Today, int NthStir)
{
DateTime Today_ = Today.DateValue; // Casting to DateTime
int Month = Today_.Month; // Month int
int Year = Today_.Year; // Year int
int Rem; // The remainder
int result = Math.DivRem(Month, 3, out Rem);
int Quarter = Month + (3 - Rem) * Math.Sign(Rem);
if (Today.SerialValue > (IMMDate(Quarter, Year).SerialValue - 2))
{
Rem = Quarter + 3 * NthStir;
}
else
{
Rem = Quarter + 3 * (NthStir - 1);
}
return IMMDate(Rem - 12 * (int)((Rem - 0.5) / 12), Year + (int)((Rem - 0.5) / 12));
}
public Date IMM_Date_Nth(int NthStir)
{
return IMM_Date_Nth(this, NthStir);
}
//打印函数
public override string ToString()
{
return dt.ToString("ddd_dd_MMM_yyyy", DateTimeFormatInfo.InvariantInfo);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Globalization;
namespace UserDefinedDataEXP
{
class test
{
static void Main(string[] args)
{
Example3();
Console.Read();
}
public static void Example3()
{
Date today = new Date(2011, 6, 16); // My today
int minIndex = 0; // Min index of myDateVect
int nOfDates = 6; // # element of myDateVect
Vector<Date> myDateVect = new Vector<Date>(nOfDates, minIndex);
string myDateFormat = "ddd dd MMM yyyy"; // Data format for output
Console.WriteLine("Reference Date Third Wednesday of Ref Date");
for (int i = minIndex; i < nOfDates; i++)
{
myDateVect[i] = today.AddMonths(3 * i);
Console.WriteLine("{0} \t{1}",
myDateVect[i].DateValue.ToString(myDateFormat),
myDateVect[i].IMMDate().DateValue.ToString(myDateFormat));
}
Console.WriteLine("\n{0} {1}",
"IMM Dates for futures strip [Mar,Jun,Sep,Dec]. Today is",
today.DateValue.ToString(myDateFormat));
// We show the IMM Date of futures strip of type Mar,Jun,Sep,Dec
for (int i = minIndex + 1; i < nOfDates; i++)
{
Console.WriteLine("#{0} stir \t{1}", i, today.IMM_Date_Nth(i).DateValue.ToString(myDateFormat));
}
}
}
}
【C#】16. IMM (Date class 更新)
最新推荐文章于 2022-11-28 14:32:26 发布