3 // (C) Ulrich Kunitz 2002
6 namespace System.Globalization {
11 /// This is the Julian calendar.
14 /// <para>The Julian calendar supports only the Common Era from
15 /// January 1, 1 (Gregorian) to December 31, 9999 (Gregorian).
17 /// <para>The implementation uses the
18 /// <see cref="N:CalendricalCalculations"/> namespace.
22 public class JulianCalendar : Calendar {
24 /// Default constructor.
26 public JulianCalendar() {
27 M_AbbrEraNames = new string[] {"C.E."};
28 M_EraNames = new string[] {"Common Era"};
29 if (M_TwoDigitYearMax == 99)
30 M_TwoDigitYearMax = 2029;
34 /// The era number for the Common Era (C.E.) or Anno Domini (A.D.)
37 public static readonly int JulianEra = 1;
39 /// <value>Overridden. Gives the eras supported by the Julian
40 /// calendar as an array of integers.
42 public override int[] Eras {
44 return new int[] { JulianEra };
49 public override int TwoDigitYearMax
52 throw new NotImplementedException();
55 throw new NotImplementedException();
60 /// A protected method checking the era number.
62 /// <param name="era">The era number.</param>
63 /// <exception name="T:System.ArgumentException">
64 /// The exception is thrown if the era is not equal
65 /// <see cref="M:JulianEra"/>.
67 internal void M_CheckEra(ref int era) {
68 if (era == CurrentEra)
71 throw new ArgumentException("Era value was not valid.");
75 /// A protected method checking calendar year and the era number.
77 /// <param name="year">An integer representing the calendar year.
79 /// <param name="era">The era number.</param>
80 /// <exception cref="T:System.ArgumentException">
81 /// The exception is thrown if the era is not equal
82 /// <see cref="M:JulianEra"/>.
84 /// <exception cref="T:System.ArgumentOutOfRangeException">
85 /// The exception is thrown if the calendar year is outside of
86 /// the allowed range.
88 internal override void M_CheckYE(int year, ref int era) {
90 M_ArgumentInRange("year", year, 1, 9999);
94 /// A protected method checking the calendar year, month, and
97 /// <param name="year">An integer representing the calendar year.
99 /// <param name="month">An integer giving the calendar month.
101 /// <param name="era">The era number.</param>
102 /// <exception cref="T:System.ArgumentException">
103 /// The exception is thrown if the era is not equal
104 /// <see cref="M:JulianEra"/>.
106 /// <exception cref="T:System.ArgumentOutOfRangeException">
107 /// The exception is thrown if the calendar year or month is
108 /// outside of the allowed range.
110 internal void M_CheckYME(int year, int month, ref int era) {
111 M_CheckYE(year, ref era);
112 if (month < 1 || month > 12)
113 throw new ArgumentOutOfRangeException("month",
114 "Month must be between one and twelve.");
118 /// A protected method checking the calendar day, month, and year
119 /// and the era number.
121 /// <param name="year">An integer representing the calendar year.
123 /// <param name="month">An integer giving the calendar month.
125 /// <param name="day">An integer giving the calendar day.
127 /// <param name="era">The era number.</param>
128 /// <exception cref="T:System.ArgumentException">
129 /// The exception is thrown if the era is not equal
130 /// <see cref="M:JulianEra"/>.
132 /// <exception cref="T:System.ArgumentOutOfRangeException">
133 /// The exception is thrown if the calendar year, month, or day is
134 /// outside of the allowed range.
136 internal void M_CheckYMDE(int year, int month, int day, ref int era)
138 M_CheckYME(year, month, ref era);
139 M_ArgumentInRange("day", day, 1,
140 GetDaysInMonth(year, month, era));
141 if (year == 9999 && ((month == 10 && day > 19) || month > 10))
142 throw new ArgumentOutOfRangeException(
143 "The maximum Julian date is 19. 10. 9999.");
147 /// Overridden. Adds months to a given date.
149 /// <param name="time">The
150 /// <see cref="T:System.DateTime"/> to which to add
153 /// <param name="months">The number of months to add.</param>
154 /// <returns>A new <see cref="T:System.DateTime"/> value, that
155 /// results from adding <paramref name="months"/> to the specified
156 /// DateTime.</returns>
157 public override DateTime AddMonths(DateTime time, int months) {
158 int rd = CCFixed.FromDateTime(time);
159 int day, month, year;
160 CCJulianCalendar.dmy_from_fixed(
161 out day, out month, out year, rd);
163 rd = CCJulianCalendar.fixed_from_dmy(day, month, year);
164 DateTime t = CCFixed.ToDateTime(rd);
165 return t.Add(time.TimeOfDay);
169 /// Overridden. Adds years to a given date.
171 /// <param name="time">The
172 /// <see cref="T:System.DateTime"/> to which to add
175 /// <param name="years">The number of years to add.</param>
176 /// <returns>A new <see cref="T:System.DateTime"/> value, that
177 /// results from adding <paramref name="years"/> to the specified
178 /// DateTime.</returns>
179 public override DateTime AddYears(DateTime time, int years) {
180 int rd = CCFixed.FromDateTime(time);
181 int day, month, year;
182 CCJulianCalendar.dmy_from_fixed(
183 out day, out month, out year, rd);
185 rd = CCJulianCalendar.fixed_from_dmy(day, month, year);
186 DateTime t = CCFixed.ToDateTime(rd);
187 return t.Add(time.TimeOfDay);
191 /// Overridden. Gets the day of the month from
192 /// <paramref name="time"/>.
194 /// <param name="time">The
195 /// <see cref="T:System.DateTime"/> that specifies a
198 /// <returns>An integer giving the day of months, starting with 1.
200 public override int GetDayOfMonth(DateTime time) {
201 int rd = CCFixed.FromDateTime(time);
202 return CCJulianCalendar.day_from_fixed(rd);
206 /// Overridden. Gets the day of the week from the specified date.
208 /// <param name="time">The
209 /// <see cref="T:System.DateTime"/> that specifies a
212 /// <returns>An integer giving the day of months, starting with 1.
214 public override DayOfWeek GetDayOfWeek(DateTime time) {
215 int rd = CCFixed.FromDateTime(time);
216 return (DayOfWeek)CCFixed.day_of_week(rd);
220 /// Overridden. Gives the number of the day in the year.
222 /// <param name="time">The
223 /// <see cref="T:System.DateTime"/> that specifies a
226 /// <returns>An integer representing the day of the year,
227 /// starting with 1.</returns>
228 public override int GetDayOfYear(DateTime time) {
229 int rd = CCFixed.FromDateTime(time);
230 int year = CCJulianCalendar.year_from_fixed(rd);
231 int rd1_1 = CCJulianCalendar.fixed_from_dmy(1, 1, year);
232 return rd - rd1_1 + 1;
236 /// Overridden. Gives the number of days in the specified month
237 /// of the given year and era.
239 /// <param name="year">An integer that gives the year.
241 /// <param name="month">An integer that gives the month, starting
243 /// <param name="era">An intger that gives the era of the specified
245 /// <returns>An integer that gives the number of days of the
246 /// specified month.</returns>
247 /// <exception cref="T:System.ArgumentOutOfRangeException">
248 /// The exception is thrown, if <paramref name="month"/>,
249 /// <paramref name="year"/> ,or <paramref name="era"/> is outside
250 /// the allowed range.
252 public override int GetDaysInMonth(int year, int month, int era) {
253 M_CheckYME(year, month, ref era);
254 int rd1 = CCJulianCalendar.fixed_from_dmy(1, month, year);
255 int rd2 = CCJulianCalendar.fixed_from_dmy(1, month+1, year);
260 /// Overridden. Gives the number of days of the specified
261 /// year of the given era.
263 /// <param name="year">An integer that specifies the year.
265 /// <param name="era">An ineger that specifies the era.
267 /// <returns>An integer that gives the number of days of the
268 /// specified year.</returns>
269 /// <exception cref="T:System.ArgumentOutOfRangeExceiption">
270 /// The exception is thrown, if
271 /// <paramref name="year"/> is outside the allowed range.
273 public override int GetDaysInYear(int year, int era) {
274 M_CheckYE(year, ref era);
275 int rd1 = CCJulianCalendar.fixed_from_dmy(1, 1, year);
276 int rd2 = CCJulianCalendar.fixed_from_dmy(1, 1, year+1);
282 /// Overridden. Gives the era of the specified date.
284 /// <param name="time">The
285 /// <see cref="T:System.DateTime"/> that specifies a
288 /// <returns>An integer representing the era of the calendar.
290 public override int GetEra(DateTime time) {
291 // should change, if more than one era is supported
296 /// Overridden. Gives the number of the month of the specified
299 /// <param name="time">The
300 /// <see cref="T:System.DateTime"/> that specifies a
303 /// <returns>An integer representing the month,
304 /// starting with 1.</returns>
305 public override int GetMonth(DateTime time) {
306 int rd = CCFixed.FromDateTime(time);
307 return CCJulianCalendar.month_from_fixed(rd);
311 /// Overridden. Gives the number of months in the specified year
314 /// <param name="year">An integer that specifies the year.
316 /// <param name="era">An integer that specifies the era.
318 /// <returns>An integer that gives the number of the months in the
319 /// specified year.</returns>
320 /// <exception cref="T:System.ArgumentOutOfRangeException">
321 /// The exception is thrown, if the year or the era are not valid.
323 public override int GetMonthsInYear(int year, int era) {
324 M_CheckYE(year, ref era);
329 /// Overridden. Gives the number of the year of the specified
332 /// <param name="time">The
333 /// <see cref="T:System.DateTime"/> that specifies a
336 /// <returns>An integer representing the year,
337 /// starting with 1.</returns>
338 public override int GetYear(DateTime time) {
339 int rd = CCFixed.FromDateTime(time);
340 return CCJulianCalendar.year_from_fixed(rd);
344 /// Overridden. Tells whether the given day
347 /// <param name="year">An integer that specifies the year in the
350 /// <param name="month">An integer that specifies the month.
352 /// <param name="day">An integer that specifies the day.
354 /// <param name="era">An integer that specifies the era.
356 /// <returns>A boolean that tells whether the given day is a leap
359 /// <exception cref="T:System.ArgumentOutOfRangeException">
360 /// The exception is thrown, if the year, month, day, or era is not
363 public override bool IsLeapDay(int year, int month, int day, int era)
365 M_CheckYMDE(year, month, day, ref era);
366 return IsLeapYear(year) && month == 2 && day == 29;
370 /// Overridden. Tells whether the given month
373 /// <param name="year">An integer that specifies the year in the
376 /// <param name="month">An integer that specifies the month.
378 /// <param name="era">An integer that specifies the era.
380 /// <returns>A boolean that tells whether the given month is a leap
383 /// <exception cref="T:System.ArgumentOutOfRangeException">
384 /// The exception is thrown, if the year, month, or era is not
387 public override bool IsLeapMonth(int year, int month, int era) {
388 M_CheckYME(year, month, ref era);
393 /// Overridden. Tells whether the given year
396 /// <param name="year">An integer that specifies the year in the
399 /// <param name="era">An integer that specifies the era.
401 /// <returns>A boolean that tells whether the given year is a leap
404 /// <exception cref="T:System.ArgumentOutOfRangeException">
405 /// The exception is thrown, if the year or era is not
408 public override bool IsLeapYear(int year, int era) {
409 M_CheckYE(year, ref era);
410 return CCJulianCalendar.is_leap_year(year);
414 /// Overridden. Creates the
415 /// <see cref="T:System.DateTime"/> from the parameters.
417 /// <param name="year">An integer that gives the year in the
418 /// <paramref name="era"/>.
420 /// <param name="month">An integer that specifies the month.
422 /// <param name="day">An integer that specifies the day.
424 /// <param name="hour">An integer that specifies the hour.
426 /// <param name="minute">An integer that specifies the minute.
428 /// <param name="second">An integer that gives the second.
430 /// <param name="milliseconds">An integer that gives the
433 /// <param name="era">An integer that specifies the era.
436 /// <see cref="T:system.DateTime"/> representig the date and time.
438 /// <exception cref="T:System.ArgumentOutOfRangeException">
439 /// The exception is thrown, if at least one of the parameters
442 public override DateTime ToDateTime(int year, int month, int day,
443 int hour, int minute, int second, int milliseconds,
446 M_CheckYMDE(year, month, day, ref era);
447 M_CheckHMSM(hour, minute, second, milliseconds);
448 int rd = CCJulianCalendar.fixed_from_dmy(day, month, year);
449 return CCFixed.ToDateTime(rd,
450 hour, minute, second, milliseconds);
454 public override int ToFourDigitYear(int year)
456 throw new NotImplementedException();
459 } // class JulianCalendar
461 } // namespace System.Globalization