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 /// A protected method checking the era number.
51 /// <param name="era">The era number.</param>
52 /// <exception name="T:System.ArgumentException">
53 /// The exception is thrown if the era is not equal
54 /// <see cref="M:JulianEra"/>.
56 internal void M_CheckEra(ref int era) {
57 if (era == CurrentEra)
60 throw new ArgumentException("Era value was not valid.");
64 /// A protected method checking calendar year and the era number.
66 /// <param name="year">An integer representing the calendar year.
68 /// <param name="era">The era number.</param>
69 /// <exception cref="T:System.ArgumentException">
70 /// The exception is thrown if the era is not equal
71 /// <see cref="M:JulianEra"/>.
73 /// <exception cref="T:System.ArgumentOutOfRangeException">
74 /// The exception is thrown if the calendar year is outside of
75 /// the allowed range.
77 internal override void M_CheckYE(int year, ref int era) {
79 M_ArgumentInRange("year", year, 1, 9999);
83 /// A protected method checking the calendar year, month, and
86 /// <param name="year">An integer representing the calendar year.
88 /// <param name="month">An integer giving the calendar month.
90 /// <param name="era">The era number.</param>
91 /// <exception cref="T:System.ArgumentException">
92 /// The exception is thrown if the era is not equal
93 /// <see cref="M:JulianEra"/>.
95 /// <exception cref="T:System.ArgumentOutOfRangeException">
96 /// The exception is thrown if the calendar year or month is
97 /// outside of the allowed range.
99 internal void M_CheckYME(int year, int month, ref int era) {
100 M_CheckYE(year, ref era);
101 if (month < 1 || month > 12)
102 throw new ArgumentOutOfRangeException("month",
103 "Month must be between one and twelve.");
107 /// A protected method checking the calendar day, month, and year
108 /// and the era number.
110 /// <param name="year">An integer representing the calendar year.
112 /// <param name="month">An integer giving the calendar month.
114 /// <param name="day">An integer giving the calendar day.
116 /// <param name="era">The era number.</param>
117 /// <exception cref="T:System.ArgumentException">
118 /// The exception is thrown if the era is not equal
119 /// <see cref="M:JulianEra"/>.
121 /// <exception cref="T:System.ArgumentOutOfRangeException">
122 /// The exception is thrown if the calendar year, month, or day is
123 /// outside of the allowed range.
125 internal void M_CheckYMDE(int year, int month, int day, ref int era)
127 M_CheckYME(year, month, ref era);
128 M_ArgumentInRange("day", day, 1,
129 GetDaysInMonth(year, month, era));
130 if (year == 9999 && ((month == 10 && day > 19) || month > 10))
131 throw new ArgumentOutOfRangeException(
132 "The maximum Julian date is 19. 10. 9999.");
136 /// Overridden. Adds months to a given date.
138 /// <param name="time">The
139 /// <see cref="T:System.DateTime"/> to which to add
142 /// <param name="months">The number of months to add.</param>
143 /// <returns>A new <see cref="T:System.DateTime"/> value, that
144 /// results from adding <paramref name="months"/> to the specified
145 /// DateTime.</returns>
146 public override DateTime AddMonths(DateTime time, int months) {
147 int rd = CCFixed.FromDateTime(time);
148 int day, month, year;
149 CCJulianCalendar.dmy_from_fixed(
150 out day, out month, out year, rd);
152 rd = CCJulianCalendar.fixed_from_dmy(day, month, year);
153 DateTime t = CCFixed.ToDateTime(rd);
154 return t.Add(time.TimeOfDay);
158 /// Overridden. Adds years to a given date.
160 /// <param name="time">The
161 /// <see cref="T:System.DateTime"/> to which to add
164 /// <param name="years">The number of years to add.</param>
165 /// <returns>A new <see cref="T:System.DateTime"/> value, that
166 /// results from adding <paramref name="years"/> to the specified
167 /// DateTime.</returns>
168 public override DateTime AddYears(DateTime time, int years) {
169 int rd = CCFixed.FromDateTime(time);
170 int day, month, year;
171 CCJulianCalendar.dmy_from_fixed(
172 out day, out month, out year, rd);
174 rd = CCJulianCalendar.fixed_from_dmy(day, month, year);
175 DateTime t = CCFixed.ToDateTime(rd);
176 return t.Add(time.TimeOfDay);
180 /// Overridden. Gets the day of the month from
181 /// <paramref name="time"/>.
183 /// <param name="time">The
184 /// <see cref="T:System.DateTime"/> that specifies a
187 /// <returns>An integer giving the day of months, starting with 1.
189 public override int GetDayOfMonth(DateTime time) {
190 int rd = CCFixed.FromDateTime(time);
191 return CCJulianCalendar.day_from_fixed(rd);
195 /// Overridden. Gets the day of the week from the specified date.
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 DayOfWeek GetDayOfWeek(DateTime time) {
204 int rd = CCFixed.FromDateTime(time);
205 return (DayOfWeek)CCFixed.day_of_week(rd);
209 /// Overridden. Gives the number of the day in the year.
211 /// <param name="time">The
212 /// <see cref="T:System.DateTime"/> that specifies a
215 /// <returns>An integer representing the day of the year,
216 /// starting with 1.</returns>
217 public override int GetDayOfYear(DateTime time) {
218 int rd = CCFixed.FromDateTime(time);
219 int year = CCJulianCalendar.year_from_fixed(rd);
220 int rd1_1 = CCJulianCalendar.fixed_from_dmy(1, 1, year);
221 return rd - rd1_1 + 1;
225 /// Overridden. Gives the number of days in the specified month
226 /// of the given year and era.
228 /// <param name="year">An integer that gives the year.
230 /// <param name="month">An integer that gives the month, starting
232 /// <param name="era">An intger that gives the era of the specified
234 /// <returns>An integer that gives the number of days of the
235 /// specified month.</returns>
236 /// <exception cref="T:System.ArgumentOutOfRangeException">
237 /// The exception is thrown, if <paramref name="month"/>,
238 /// <paramref name="year"/> ,or <paramref name="era"/> is outside
239 /// the allowed range.
241 public override int GetDaysInMonth(int year, int month, int era) {
242 M_CheckYME(year, month, ref era);
243 int rd1 = CCJulianCalendar.fixed_from_dmy(1, month, year);
244 int rd2 = CCJulianCalendar.fixed_from_dmy(1, month+1, year);
249 /// Overridden. Gives the number of days of the specified
250 /// year of the given era.
252 /// <param name="year">An integer that specifies the year.
254 /// <param name="era">An ineger that specifies the era.
256 /// <returns>An integer that gives the number of days of the
257 /// specified year.</returns>
258 /// <exception cref="T:System.ArgumentOutOfRangeExceiption">
259 /// The exception is thrown, if
260 /// <paramref name="year"/> is outside the allowed range.
262 public override int GetDaysInYear(int year, int era) {
263 M_CheckYE(year, ref era);
264 int rd1 = CCJulianCalendar.fixed_from_dmy(1, 1, year);
265 int rd2 = CCJulianCalendar.fixed_from_dmy(1, 1, year+1);
271 /// Overridden. Gives the era of the specified date.
273 /// <param name="time">The
274 /// <see cref="T:System.DateTime"/> that specifies a
277 /// <returns>An integer representing the era of the calendar.
279 public override int GetEra(DateTime time) {
280 // should change, if more than one era is supported
285 /// Overridden. Gives the number of the month of the specified
288 /// <param name="time">The
289 /// <see cref="T:System.DateTime"/> that specifies a
292 /// <returns>An integer representing the month,
293 /// starting with 1.</returns>
294 public override int GetMonth(DateTime time) {
295 int rd = CCFixed.FromDateTime(time);
296 return CCJulianCalendar.month_from_fixed(rd);
300 /// Overridden. Gives the number of months in the specified year
303 /// <param name="year">An integer that specifies the year.
305 /// <param name="era">An integer that specifies the era.
307 /// <returns>An integer that gives the number of the months in the
308 /// specified year.</returns>
309 /// <exception cref="T:System.ArgumentOutOfRangeException">
310 /// The exception is thrown, if the year or the era are not valid.
312 public override int GetMonthsInYear(int year, int era) {
313 M_CheckYE(year, ref era);
318 /// Overridden. Gives the number of the year of the specified
321 /// <param name="time">The
322 /// <see cref="T:System.DateTime"/> that specifies a
325 /// <returns>An integer representing the year,
326 /// starting with 1.</returns>
327 public override int GetYear(DateTime time) {
328 int rd = CCFixed.FromDateTime(time);
329 return CCJulianCalendar.year_from_fixed(rd);
333 /// Overridden. Tells whether the given day
336 /// <param name="year">An integer that specifies the year in the
339 /// <param name="month">An integer that specifies the month.
341 /// <param name="day">An integer that specifies the day.
343 /// <param name="era">An integer that specifies the era.
345 /// <returns>A boolean that tells whether the given day is a leap
348 /// <exception cref="T:System.ArgumentOutOfRangeException">
349 /// The exception is thrown, if the year, month, day, or era is not
352 public override bool IsLeapDay(int year, int month, int day, int era)
354 M_CheckYMDE(year, month, day, ref era);
355 return IsLeapYear(year) && month == 2 && day == 29;
359 /// Overridden. Tells whether the given month
362 /// <param name="year">An integer that specifies the year in the
365 /// <param name="month">An integer that specifies the month.
367 /// <param name="era">An integer that specifies the era.
369 /// <returns>A boolean that tells whether the given month is a leap
372 /// <exception cref="T:System.ArgumentOutOfRangeException">
373 /// The exception is thrown, if the year, month, or era is not
376 public override bool IsLeapMonth(int year, int month, int era) {
377 M_CheckYME(year, month, ref era);
382 /// Overridden. Tells whether the given year
385 /// <param name="year">An integer that specifies the year in the
388 /// <param name="era">An integer that specifies the era.
390 /// <returns>A boolean that tells whether the given year is a leap
393 /// <exception cref="T:System.ArgumentOutOfRangeException">
394 /// The exception is thrown, if the year or era is not
397 public override bool IsLeapYear(int year, int era) {
398 M_CheckYE(year, ref era);
399 return CCJulianCalendar.is_leap_year(year);
403 /// Overridden. Creates the
404 /// <see cref="T:System.DateTime"/> from the parameters.
406 /// <param name="year">An integer that gives the year in the
407 /// <paramref name="era"/>.
409 /// <param name="month">An integer that specifies the month.
411 /// <param name="day">An integer that specifies the day.
413 /// <param name="hour">An integer that specifies the hour.
415 /// <param name="minute">An integer that specifies the minute.
417 /// <param name="second">An integer that gives the second.
419 /// <param name="milliseconds">An integer that gives the
422 /// <param name="era">An integer that specifies the era.
425 /// <see cref="T:system.DateTime"/> representig the date and time.
427 /// <exception cref="T:System.ArgumentOutOfRangeException">
428 /// The exception is thrown, if at least one of the parameters
431 public override DateTime ToDateTime(int year, int month, int day,
432 int hour, int minute, int second, int milliseconds,
435 M_CheckYMDE(year, month, day, ref era);
436 M_CheckHMSM(hour, minute, second, milliseconds);
437 int rd = CCJulianCalendar.fixed_from_dmy(day, month, year);
438 return CCFixed.ToDateTime(rd,
439 hour, minute, second, milliseconds);
441 } // class JulianCalendar
443 } // namespace System.Globalization