Exemple #1
0
            public override double yearFraction(Date d1, Date d2, Date refPeriodStart, Date refPeriodEnd)
            {
                if (d1 == d2)
                {
                    return(0);
                }
                if (d1 > d2)
                {
                    return(-yearFraction(d2, d1, null, null));
                }

                Date   newD2 = d2, temp = d2;
                double sum = 0;

                while (temp > d1)
                {
                    temp = newD2 - TimeUnit.Years;
                    if (temp.Day == 28 && temp.Month == 2 && Date.IsLeapYear(temp.Year))
                    {
                        temp += 1;
                    }
                    if (temp >= d1)
                    {
                        sum  += 1;
                        newD2 = temp;
                    }
                }

                double den = 365;

                if (Date.IsLeapYear(newD2.Year))
                {
                    temp = new Date(29, Month.February, newD2.Year);
                    if (newD2 > temp && d1 <= temp)
                    {
                        den += 1;
                    }
                }
                else if (Date.IsLeapYear(d1.Year))
                {
                    temp = new Date(29, Month.February, d1.Year);
                    if (newD2 > temp && d1 <= temp)
                    {
                        den += 1;
                    }
                }

                return(sum + dayCount(d1, newD2) / den);
            }
Exemple #2
0
            public override double yearFraction(Date d1, Date d2, Date refPeriodStart, Date refPeriodEnd)
            {
                if (d1 == d2)
                {
                    return(0);
                }
                if (d1 > d2)
                {
                    return(-yearFraction(d2, d1, null, null));
                }

                int    y1 = d1.Year, y2 = d2.Year;
                double dib1 = (Date.IsLeapYear(y1) ? 366 : 365),
                       dib2 = (Date.IsLeapYear(y2) ? 366 : 365);

                double sum = y2 - y1 - 1;

                sum += dayCount(d1, new Date(1, Month.January, y1 + 1)) / dib1;
                sum += dayCount(new Date(1, Month.January, y2), d2) / dib2;
                return(sum);
            }