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 };
48 int twoDigitYearMax = 2029;
50 public override int TwoDigitYearMax
53 return twoDigitYearMax;
56 M_ArgumentInRange ("value", value, 100, M_MaxYear);
58 twoDigitYearMax = value;
63 /// A protected method checking the era number.
65 /// <param name="era">The era number.</param>
66 /// <exception name="T:System.ArgumentException">
67 /// The exception is thrown if the era is not equal
68 /// <see cref="M:JulianEra"/>.
70 internal void M_CheckEra(ref int era) {
71 if (era == CurrentEra)
74 throw new ArgumentException("Era value was not valid.");
78 /// A protected method checking calendar year and the era number.
80 /// <param name="year">An integer representing the calendar year.
82 /// <param name="era">The era number.</param>
83 /// <exception cref="T:System.ArgumentException">
84 /// The exception is thrown if the era is not equal
85 /// <see cref="M:JulianEra"/>.
87 /// <exception cref="T:System.ArgumentOutOfRangeException">
88 /// The exception is thrown if the calendar year is outside of
89 /// the allowed range.
91 internal override void M_CheckYE(int year, ref int era) {
93 M_ArgumentInRange("year", year, 1, 9999);
97 /// A protected method checking the calendar year, month, and
100 /// <param name="year">An integer representing the calendar year.
102 /// <param name="month">An integer giving the calendar month.
104 /// <param name="era">The era number.</param>
105 /// <exception cref="T:System.ArgumentException">
106 /// The exception is thrown if the era is not equal
107 /// <see cref="M:JulianEra"/>.
109 /// <exception cref="T:System.ArgumentOutOfRangeException">
110 /// The exception is thrown if the calendar year or month is
111 /// outside of the allowed range.
113 internal void M_CheckYME(int year, int month, ref int era) {
114 M_CheckYE(year, ref era);
115 if (month < 1 || month > 12)
116 throw new ArgumentOutOfRangeException("month",
117 "Month must be between one and twelve.");
121 /// A protected method checking the calendar day, month, and year
122 /// and the era number.
124 /// <param name="year">An integer representing the calendar year.
126 /// <param name="month">An integer giving the calendar month.
128 /// <param name="day">An integer giving the calendar day.
130 /// <param name="era">The era number.</param>
131 /// <exception cref="T:System.ArgumentException">
132 /// The exception is thrown if the era is not equal
133 /// <see cref="M:JulianEra"/>.
135 /// <exception cref="T:System.ArgumentOutOfRangeException">
136 /// The exception is thrown if the calendar year, month, or day is
137 /// outside of the allowed range.
139 internal void M_CheckYMDE(int year, int month, int day, ref int era)
141 M_CheckYME(year, month, ref era);
142 M_ArgumentInRange("day", day, 1,
143 GetDaysInMonth(year, month, era));
144 if (year == 9999 && ((month == 10 && day > 19) || month > 10))
145 throw new ArgumentOutOfRangeException(
146 "The maximum Julian date is 19. 10. 9999.");
150 /// Overridden. Adds months to a given date.
152 /// <param name="time">The
153 /// <see cref="T:System.DateTime"/> to which to add
156 /// <param name="months">The number of months to add.</param>
157 /// <returns>A new <see cref="T:System.DateTime"/> value, that
158 /// results from adding <paramref name="months"/> to the specified
159 /// DateTime.</returns>
160 public override DateTime AddMonths(DateTime time, int months) {
161 int rd = CCFixed.FromDateTime(time);
162 int day, month, year;
163 CCJulianCalendar.dmy_from_fixed(
164 out day, out month, out year, rd);
166 rd = CCJulianCalendar.fixed_from_dmy(day, month, year);
167 DateTime t = CCFixed.ToDateTime(rd);
168 return t.Add(time.TimeOfDay);
172 /// Overridden. Adds years to a given date.
174 /// <param name="time">The
175 /// <see cref="T:System.DateTime"/> to which to add
178 /// <param name="years">The number of years to add.</param>
179 /// <returns>A new <see cref="T:System.DateTime"/> value, that
180 /// results from adding <paramref name="years"/> to the specified
181 /// DateTime.</returns>
182 public override DateTime AddYears(DateTime time, int years) {
183 int rd = CCFixed.FromDateTime(time);
184 int day, month, year;
185 CCJulianCalendar.dmy_from_fixed(
186 out day, out month, out year, rd);
188 rd = CCJulianCalendar.fixed_from_dmy(day, month, year);
189 DateTime t = CCFixed.ToDateTime(rd);
190 return t.Add(time.TimeOfDay);
194 /// Overridden. Gets the day of the month from
195 /// <paramref name="time"/>.
197 /// <param name="time">The
198 /// <see cref="T:System.DateTime"/> that specifies a
201 /// <returns>An integer giving the day of months, starting with 1.
203 public override int GetDayOfMonth(DateTime time) {
204 int rd = CCFixed.FromDateTime(time);
205 return CCJulianCalendar.day_from_fixed(rd);
209 /// Overridden. Gets the day of the week from the specified date.
211 /// <param name="time">The
212 /// <see cref="T:System.DateTime"/> that specifies a
215 /// <returns>An integer giving the day of months, starting with 1.
217 public override DayOfWeek GetDayOfWeek(DateTime time) {
218 int rd = CCFixed.FromDateTime(time);
219 return (DayOfWeek)CCFixed.day_of_week(rd);
223 /// Overridden. Gives the number of the day in the year.
225 /// <param name="time">The
226 /// <see cref="T:System.DateTime"/> that specifies a
229 /// <returns>An integer representing the day of the year,
230 /// starting with 1.</returns>
231 public override int GetDayOfYear(DateTime time) {
232 int rd = CCFixed.FromDateTime(time);
233 int year = CCJulianCalendar.year_from_fixed(rd);
234 int rd1_1 = CCJulianCalendar.fixed_from_dmy(1, 1, year);
235 return rd - rd1_1 + 1;
239 /// Overridden. Gives the number of days in the specified month
240 /// of the given year and era.
242 /// <param name="year">An integer that gives the year.
244 /// <param name="month">An integer that gives the month, starting
246 /// <param name="era">An intger that gives the era of the specified
248 /// <returns>An integer that gives the number of days of the
249 /// specified month.</returns>
250 /// <exception cref="T:System.ArgumentOutOfRangeException">
251 /// The exception is thrown, if <paramref name="month"/>,
252 /// <paramref name="year"/> ,or <paramref name="era"/> is outside
253 /// the allowed range.
255 public override int GetDaysInMonth(int year, int month, int era) {
256 M_CheckYME(year, month, ref era);
257 int rd1 = CCJulianCalendar.fixed_from_dmy(1, month, year);
258 int rd2 = CCJulianCalendar.fixed_from_dmy(1, month+1, year);
263 /// Overridden. Gives the number of days of the specified
264 /// year of the given era.
266 /// <param name="year">An integer that specifies the year.
268 /// <param name="era">An ineger that specifies the era.
270 /// <returns>An integer that gives the number of days of the
271 /// specified year.</returns>
272 /// <exception cref="T:System.ArgumentOutOfRangeExceiption">
273 /// The exception is thrown, if
274 /// <paramref name="year"/> is outside the allowed range.
276 public override int GetDaysInYear(int year, int era) {
277 M_CheckYE(year, ref era);
278 int rd1 = CCJulianCalendar.fixed_from_dmy(1, 1, year);
279 int rd2 = CCJulianCalendar.fixed_from_dmy(1, 1, year+1);
285 /// Overridden. Gives the era of the specified date.
287 /// <param name="time">The
288 /// <see cref="T:System.DateTime"/> that specifies a
291 /// <returns>An integer representing the era of the calendar.
293 public override int GetEra(DateTime time) {
294 // should change, if more than one era is supported
299 /// Overridden. Gives the number of the month of the specified
302 /// <param name="time">The
303 /// <see cref="T:System.DateTime"/> that specifies a
306 /// <returns>An integer representing the month,
307 /// starting with 1.</returns>
308 public override int GetMonth(DateTime time) {
309 int rd = CCFixed.FromDateTime(time);
310 return CCJulianCalendar.month_from_fixed(rd);
314 /// Overridden. Gives the number of months in the specified year
317 /// <param name="year">An integer that specifies the year.
319 /// <param name="era">An integer that specifies the era.
321 /// <returns>An integer that gives the number of the months in the
322 /// specified year.</returns>
323 /// <exception cref="T:System.ArgumentOutOfRangeException">
324 /// The exception is thrown, if the year or the era are not valid.
326 public override int GetMonthsInYear(int year, int era) {
327 M_CheckYE(year, ref era);
332 /// Overridden. Gives the number of the year of the specified
335 /// <param name="time">The
336 /// <see cref="T:System.DateTime"/> that specifies a
339 /// <returns>An integer representing the year,
340 /// starting with 1.</returns>
341 public override int GetYear(DateTime time) {
342 int rd = CCFixed.FromDateTime(time);
343 return CCJulianCalendar.year_from_fixed(rd);
347 /// Overridden. Tells whether the given day
350 /// <param name="year">An integer that specifies the year in the
353 /// <param name="month">An integer that specifies the month.
355 /// <param name="day">An integer that specifies the day.
357 /// <param name="era">An integer that specifies the era.
359 /// <returns>A boolean that tells whether the given day is a leap
362 /// <exception cref="T:System.ArgumentOutOfRangeException">
363 /// The exception is thrown, if the year, month, day, or era is not
366 public override bool IsLeapDay(int year, int month, int day, int era)
368 M_CheckYMDE(year, month, day, ref era);
369 return IsLeapYear(year) && month == 2 && day == 29;
373 /// Overridden. Tells whether the given month
376 /// <param name="year">An integer that specifies the year in the
379 /// <param name="month">An integer that specifies the month.
381 /// <param name="era">An integer that specifies the era.
383 /// <returns>A boolean that tells whether the given month is a leap
386 /// <exception cref="T:System.ArgumentOutOfRangeException">
387 /// The exception is thrown, if the year, month, or era is not
390 public override bool IsLeapMonth(int year, int month, int era) {
391 M_CheckYME(year, month, ref era);
396 /// Overridden. Tells whether the given year
399 /// <param name="year">An integer that specifies the year in the
402 /// <param name="era">An integer that specifies the era.
404 /// <returns>A boolean that tells whether the given year is a leap
407 /// <exception cref="T:System.ArgumentOutOfRangeException">
408 /// The exception is thrown, if the year or era is not
411 public override bool IsLeapYear(int year, int era) {
412 M_CheckYE(year, ref era);
413 return CCJulianCalendar.is_leap_year(year);
417 /// Overridden. Creates the
418 /// <see cref="T:System.DateTime"/> from the parameters.
420 /// <param name="year">An integer that gives the year in the
421 /// <paramref name="era"/>.
423 /// <param name="month">An integer that specifies the month.
425 /// <param name="day">An integer that specifies the day.
427 /// <param name="hour">An integer that specifies the hour.
429 /// <param name="minute">An integer that specifies the minute.
431 /// <param name="second">An integer that gives the second.
433 /// <param name="milliseconds">An integer that gives the
436 /// <param name="era">An integer that specifies the era.
439 /// <see cref="T:system.DateTime"/> representig the date and time.
441 /// <exception cref="T:System.ArgumentOutOfRangeException">
442 /// The exception is thrown, if at least one of the parameters
445 public override DateTime ToDateTime(int year, int month, int day,
446 int hour, int minute, int second, int milliseconds,
449 M_CheckYMDE(year, month, day, ref era);
450 M_CheckHMSM(hour, minute, second, milliseconds);
451 int rd = CCJulianCalendar.fixed_from_dmy(day, month, year);
452 return CCFixed.ToDateTime(rd,
453 hour, minute, second, milliseconds);
457 public override int ToFourDigitYear(int year)
459 throw new NotImplementedException();
462 } // class JulianCalendar
464 } // namespace System.Globalization