// JulianCalendar.cs // // (C) Ulrich Kunitz 2002 // namespace System.Globalization { using System; /// /// This is the Julian calendar. /// /// /// The Julian calendar supports only the Common Era from /// January 1, 1 (Gregorian) to December 31, 9999 (Gregorian). /// /// The implementation uses the /// namespace. /// /// [Serializable] public class JulianCalendar : Calendar { /// /// Default constructor. /// public JulianCalendar() { M_AbbrEraNames = new string[] {"C.E."}; M_EraNames = new string[] {"Common Era"}; if (M_TwoDigitYearMax == 99) M_TwoDigitYearMax = 2029; } /// /// The era number for the Common Era (C.E.) or Anno Domini (A.D.) /// respective. /// public const int JulianEra = 1; /// Overridden. Gives the eras supported by the Julian /// calendar as an array of integers. /// public override int[] Eras { get { return new int[] { JulianEra }; } } /// /// A protected method checking the era number. /// /// The era number. /// /// The exception is thrown if the era is not equal /// . /// protected void M_CheckEra(ref int era) { if (era == CurrentEra) era = JulianEra; if (era != JulianEra) throw new ArgumentException("Era value was not valid."); } /// /// A protected method checking calendar year and the era number. /// /// An integer representing the calendar year. /// /// The era number. /// /// The exception is thrown if the era is not equal /// . /// /// /// The exception is thrown if the calendar year is outside of /// the allowed range. /// protected override void M_CheckYE(int year, ref int era) { M_CheckEra(ref era); M_ArgumentInRange("year", year, 1, 9999); } /// /// A protected method checking the calendar year, month, and /// era number. /// /// An integer representing the calendar year. /// /// An integer giving the calendar month. /// /// The era number. /// /// The exception is thrown if the era is not equal /// . /// /// /// The exception is thrown if the calendar year or month is /// outside of the allowed range. /// protected void M_CheckYME(int year, int month, ref int era) { M_CheckYE(year, ref era); if (month < 1 || month > 12) throw new ArgumentOutOfRangeException("month", "Month must be between one and twelve."); } /// /// A protected method checking the calendar day, month, and year /// and the era number. /// /// An integer representing the calendar year. /// /// An integer giving the calendar month. /// /// An integer giving the calendar day. /// /// The era number. /// /// The exception is thrown if the era is not equal /// . /// /// /// The exception is thrown if the calendar year, month, or day is /// outside of the allowed range. /// protected void M_CheckYMDE(int year, int month, int day, ref int era) { M_CheckYME(year, month, ref era); M_ArgumentInRange("day", day, 1, GetDaysInMonth(year, month, era)); if (year == 9999 && ((month == 10 && day > 19) || month > 10)) throw new ArgumentOutOfRangeException( "The maximum Julian date is 19. 10. 9999."); } /// /// Overridden. Adds months to a given date. /// /// The /// to which to add /// months. /// /// The number of months to add. /// A new value, that /// results from adding to the specified /// DateTime. public override DateTime AddMonths(DateTime time, int months) { int rd = CCFixed.FromDateTime(time); int day, month, year; CCJulianCalendar.dmy_from_fixed( out day, out month, out year, rd); month += months; rd = CCJulianCalendar.fixed_from_dmy(day, month, year); DateTime t = CCFixed.ToDateTime(rd); return t.Add(time.TimeOfDay); } /// /// Overridden. Adds years to a given date. /// /// The /// to which to add /// years. /// /// The number of years to add. /// A new value, that /// results from adding to the specified /// DateTime. public override DateTime AddYears(DateTime time, int years) { int rd = CCFixed.FromDateTime(time); int day, month, year; CCJulianCalendar.dmy_from_fixed( out day, out month, out year, rd); year += years; rd = CCJulianCalendar.fixed_from_dmy(day, month, year); DateTime t = CCFixed.ToDateTime(rd); return t.Add(time.TimeOfDay); } /// /// Overridden. Gets the day of the month from /// . /// /// The /// that specifies a /// date. /// /// An integer giving the day of months, starting with 1. /// public override int GetDayOfMonth(DateTime time) { int rd = CCFixed.FromDateTime(time); return CCJulianCalendar.day_from_fixed(rd); } /// /// Overridden. Gets the day of the week from the specified date. /// /// The /// that specifies a /// date. /// /// An integer giving the day of months, starting with 1. /// public override DayOfWeek GetDayOfWeek(DateTime time) { int rd = CCFixed.FromDateTime(time); return (DayOfWeek)CCFixed.day_of_week(rd); } /// /// Overridden. Gives the number of the day in the year. /// /// The /// that specifies a /// date. /// /// An integer representing the day of the year, /// starting with 1. public override int GetDayOfYear(DateTime time) { int rd = CCFixed.FromDateTime(time); int year = CCJulianCalendar.year_from_fixed(rd); int rd1_1 = CCJulianCalendar.fixed_from_dmy(1, 1, year); return rd - rd1_1 + 1; } /// /// Overridden. Gives the number of days in the specified month /// of the given year and era. /// /// An integer that gives the year. /// /// An integer that gives the month, starting /// with 1. /// An intger that gives the era of the specified /// year. /// An integer that gives the number of days of the /// specified month. /// /// The exception is thrown, if , /// ,or is outside /// the allowed range. /// public override int GetDaysInMonth(int year, int month, int era) { M_CheckYME(year, month, ref era); int rd1 = CCJulianCalendar.fixed_from_dmy(1, month, year); int rd2 = CCJulianCalendar.fixed_from_dmy(1, month+1, year); return rd2 - rd1; } /// /// Overridden. Gives the number of days of the specified /// year of the given era. /// /// An integer that specifies the year. /// /// An ineger that specifies the era. /// /// An integer that gives the number of days of the /// specified year. /// /// The exception is thrown, if /// is outside the allowed range. /// public override int GetDaysInYear(int year, int era) { M_CheckYE(year, ref era); int rd1 = CCJulianCalendar.fixed_from_dmy(1, 1, year); int rd2 = CCJulianCalendar.fixed_from_dmy(1, 1, year+1); return rd2 - rd1; } /// /// Overridden. Gives the era of the specified date. /// /// The /// that specifies a /// date. /// /// An integer representing the era of the calendar. /// public override int GetEra(DateTime time) { // should change, if more than one era is supported return JulianEra; } /// /// Overridden. Gives the number of the month of the specified /// date. /// /// The /// that specifies a /// date. /// /// An integer representing the month, /// starting with 1. public override int GetMonth(DateTime time) { int rd = CCFixed.FromDateTime(time); return CCJulianCalendar.month_from_fixed(rd); } /// /// Overridden. Gives the number of months in the specified year /// and era. /// /// An integer that specifies the year. /// /// An integer that specifies the era. /// /// An integer that gives the number of the months in the /// specified year. /// /// The exception is thrown, if the year or the era are not valid. /// public override int GetMonthsInYear(int year, int era) { M_CheckYE(year, ref era); return 12; } /// /// Overridden. Gives the number of the year of the specified /// date. /// /// The /// that specifies a /// date. /// /// An integer representing the year, /// starting with 1. public override int GetYear(DateTime time) { int rd = CCFixed.FromDateTime(time); return CCJulianCalendar.year_from_fixed(rd); } /// /// Overridden. Tells whether the given day /// is a leap day. /// /// An integer that specifies the year in the /// given era. /// /// An integer that specifies the month. /// /// An integer that specifies the day. /// /// An integer that specifies the era. /// /// A boolean that tells whether the given day is a leap /// day. /// /// /// The exception is thrown, if the year, month, day, or era is not /// valid. /// public override bool IsLeapDay(int year, int month, int day, int era) { M_CheckYMDE(year, month, day, ref era); return IsLeapYear(year) && month == 2 && day == 29; } /// /// Overridden. Tells whether the given month /// is a leap month. /// /// An integer that specifies the year in the /// given era. /// /// An integer that specifies the month. /// /// An integer that specifies the era. /// /// A boolean that tells whether the given month is a leap /// month. /// /// /// The exception is thrown, if the year, month, or era is not /// valid. /// public override bool IsLeapMonth(int year, int month, int era) { M_CheckYME(year, month, ref era); return false; } /// /// Overridden. Tells whether the given year /// is a leap year. /// /// An integer that specifies the year in the /// given era. /// /// An integer that specifies the era. /// /// A boolean that tells whether the given year is a leap /// year. /// /// /// The exception is thrown, if the year or era is not /// valid. /// public override bool IsLeapYear(int year, int era) { M_CheckYE(year, ref era); return CCJulianCalendar.is_leap_year(year); } /// /// Overridden. Creates the /// from the parameters. /// /// An integer that gives the year in the /// . /// /// An integer that specifies the month. /// /// An integer that specifies the day. /// /// An integer that specifies the hour. /// /// An integer that specifies the minute. /// /// An integer that gives the second. /// /// An integer that gives the /// milliseconds. /// /// An integer that specifies the era. /// /// /// representig the date and time. /// /// /// The exception is thrown, if at least one of the parameters /// is out of range. /// public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int milliseconds, int era) { M_CheckYMDE(year, month, day, ref era); M_CheckHMSM(hour, minute, second, milliseconds); int rd = CCJulianCalendar.fixed_from_dmy(day, month, year); return CCFixed.ToDateTime(rd, hour, minute, second, milliseconds); } } // class JulianCalendar } // namespace System.Globalization