2007-01-25 Chris Toshok <toshok@ximian.com>
[mcs.git] / class / corlib / System.Globalization / JulianCalendar.cs
blob8200940c1d4220445e01e7cec1e89c2ba584cbd2
1 // JulianCalendar.cs
2 //
3 // (C) Ulrich Kunitz 2002
4 //
6 //
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 namespace System.Globalization {
31 using System;
33 /// <summary>
34 /// This is the Julian calendar.
35 /// </summary>
36 /// <remarks>
37 /// <para>The Julian calendar supports only the Common Era from
38 /// January 1, 1 (Gregorian) to December 31, 9999 (Gregorian).
39 /// </para>
40 /// <para>The implementation uses the
41 /// <see cref="N:CalendricalCalculations"/> namespace.
42 /// </para>
43 /// </remarks>
44 [Serializable]
45 [MonoTODO ("Serialization format not compatible with .NET")]
46 public class JulianCalendar : Calendar {
47 /// <summary>
48 /// Default constructor.
49 /// </summary>
50 public JulianCalendar() {
51 M_AbbrEraNames = new string[] {"C.E."};
52 M_EraNames = new string[] {"Common Era"};
53 if (twoDigitYearMax == 99)
54 twoDigitYearMax = 2029;
57 /// <summary>
58 /// The era number for the Common Era (C.E.) or Anno Domini (A.D.)
59 /// respective.
60 /// </summary>
61 public static readonly int JulianEra = 1;
63 /// <value>Overridden. Gives the eras supported by the Julian
64 /// calendar as an array of integers.
65 /// </value>
66 public override int[] Eras {
67 get {
68 return new int[] { JulianEra };
72 int twoDigitYearMax = 2029;
74 public override int TwoDigitYearMax
76 get {
77 return twoDigitYearMax;
79 set {
80 CheckReadOnly ();
81 M_ArgumentInRange ("value", value, 100, M_MaxYear);
83 twoDigitYearMax = value;
87 /// <summary>
88 /// A protected method checking the era number.
89 /// </summary>
90 /// <param name="era">The era number.</param>
91 /// <exception name="T:System.ArgumentException">
92 /// The exception is thrown if the era is not equal
93 /// <see cref="M:JulianEra"/>.
94 /// </exception>
95 internal void M_CheckEra(ref int era) {
96 if (era == CurrentEra)
97 era = JulianEra;
98 if (era != JulianEra)
99 throw new ArgumentException("Era value was not valid.");
102 /// <summary>
103 /// A protected method checking calendar year and the era number.
104 /// </summary>
105 /// <param name="year">An integer representing the calendar year.
106 /// </param>
107 /// <param name="era">The era number.</param>
108 /// <exception cref="T:System.ArgumentException">
109 /// The exception is thrown if the era is not equal
110 /// <see cref="M:JulianEra"/>.
111 /// </exception>
112 /// <exception cref="T:System.ArgumentOutOfRangeException">
113 /// The exception is thrown if the calendar year is outside of
114 /// the allowed range.
115 /// </exception>
116 internal override void M_CheckYE(int year, ref int era) {
117 M_CheckEra(ref era);
118 M_ArgumentInRange("year", year, 1, 9999);
121 /// <summary>
122 /// A protected method checking the calendar year, month, and
123 /// era number.
124 /// </summary>
125 /// <param name="year">An integer representing the calendar year.
126 /// </param>
127 /// <param name="month">An integer giving the calendar month.
128 /// </param>
129 /// <param name="era">The era number.</param>
130 /// <exception cref="T:System.ArgumentException">
131 /// The exception is thrown if the era is not equal
132 /// <see cref="M:JulianEra"/>.
133 /// </exception>
134 /// <exception cref="T:System.ArgumentOutOfRangeException">
135 /// The exception is thrown if the calendar year or month is
136 /// outside of the allowed range.
137 /// </exception>
138 internal void M_CheckYME(int year, int month, ref int era) {
139 M_CheckYE(year, ref era);
140 if (month < 1 || month > 12)
141 throw new ArgumentOutOfRangeException("month",
142 "Month must be between one and twelve.");
145 /// <summary>
146 /// A protected method checking the calendar day, month, and year
147 /// and the era number.
148 /// </summary>
149 /// <param name="year">An integer representing the calendar year.
150 /// </param>
151 /// <param name="month">An integer giving the calendar month.
152 /// </param>
153 /// <param name="day">An integer giving the calendar day.
154 /// </param>
155 /// <param name="era">The era number.</param>
156 /// <exception cref="T:System.ArgumentException">
157 /// The exception is thrown if the era is not equal
158 /// <see cref="M:JulianEra"/>.
159 /// </exception>
160 /// <exception cref="T:System.ArgumentOutOfRangeException">
161 /// The exception is thrown if the calendar year, month, or day is
162 /// outside of the allowed range.
163 /// </exception>
164 internal void M_CheckYMDE(int year, int month, int day, ref int era)
166 M_CheckYME(year, month, ref era);
167 M_ArgumentInRange("day", day, 1,
168 GetDaysInMonth(year, month, era));
169 if (year == 9999 && ((month == 10 && day > 19) || month > 10))
170 throw new ArgumentOutOfRangeException(
171 "The maximum Julian date is 19. 10. 9999.");
174 /// <summary>
175 /// Overridden. Adds months to a given date.
176 /// </summary>
177 /// <param name="time">The
178 /// <see cref="T:System.DateTime"/> to which to add
179 /// months.
180 /// </param>
181 /// <param name="months">The number of months to add.</param>
182 /// <returns>A new <see cref="T:System.DateTime"/> value, that
183 /// results from adding <paramref name="months"/> to the specified
184 /// DateTime.</returns>
185 public override DateTime AddMonths(DateTime time, int months) {
186 int rd = CCFixed.FromDateTime(time);
187 int day, month, year;
188 CCJulianCalendar.dmy_from_fixed(
189 out day, out month, out year, rd);
190 month += months;
191 rd = CCJulianCalendar.fixed_from_dmy(day, month, year);
192 DateTime t = CCFixed.ToDateTime(rd);
193 return t.Add(time.TimeOfDay);
196 /// <summary>
197 /// Overridden. Adds years to a given date.
198 /// </summary>
199 /// <param name="time">The
200 /// <see cref="T:System.DateTime"/> to which to add
201 /// years.
202 /// </param>
203 /// <param name="years">The number of years to add.</param>
204 /// <returns>A new <see cref="T:System.DateTime"/> value, that
205 /// results from adding <paramref name="years"/> to the specified
206 /// DateTime.</returns>
207 public override DateTime AddYears(DateTime time, int years) {
208 int rd = CCFixed.FromDateTime(time);
209 int day, month, year;
210 CCJulianCalendar.dmy_from_fixed(
211 out day, out month, out year, rd);
212 year += years;
213 rd = CCJulianCalendar.fixed_from_dmy(day, month, year);
214 DateTime t = CCFixed.ToDateTime(rd);
215 return t.Add(time.TimeOfDay);
218 /// <summary>
219 /// Overridden. Gets the day of the month from
220 /// <paramref name="time"/>.
221 /// </summary>
222 /// <param name="time">The
223 /// <see cref="T:System.DateTime"/> that specifies a
224 /// date.
225 /// </param>
226 /// <returns>An integer giving the day of months, starting with 1.
227 /// </returns>
228 public override int GetDayOfMonth(DateTime time) {
229 int rd = CCFixed.FromDateTime(time);
230 return CCJulianCalendar.day_from_fixed(rd);
233 /// <summary>
234 /// Overridden. Gets the day of the week from the specified date.
235 /// </summary>
236 /// <param name="time">The
237 /// <see cref="T:System.DateTime"/> that specifies a
238 /// date.
239 /// </param>
240 /// <returns>An integer giving the day of months, starting with 1.
241 /// </returns>
242 public override DayOfWeek GetDayOfWeek(DateTime time) {
243 int rd = CCFixed.FromDateTime(time);
244 return (DayOfWeek)CCFixed.day_of_week(rd);
247 /// <summary>
248 /// Overridden. Gives the number of the day in the year.
249 /// </summary>
250 /// <param name="time">The
251 /// <see cref="T:System.DateTime"/> that specifies a
252 /// date.
253 /// </param>
254 /// <returns>An integer representing the day of the year,
255 /// starting with 1.</returns>
256 public override int GetDayOfYear(DateTime time) {
257 int rd = CCFixed.FromDateTime(time);
258 int year = CCJulianCalendar.year_from_fixed(rd);
259 int rd1_1 = CCJulianCalendar.fixed_from_dmy(1, 1, year);
260 return rd - rd1_1 + 1;
263 /// <summary>
264 /// Overridden. Gives the number of days in the specified month
265 /// of the given year and era.
266 /// </summary>
267 /// <param name="year">An integer that gives the year.
268 /// </param>
269 /// <param name="month">An integer that gives the month, starting
270 /// with 1.</param>
271 /// <param name="era">An intger that gives the era of the specified
272 /// year.</param>
273 /// <returns>An integer that gives the number of days of the
274 /// specified month.</returns>
275 /// <exception cref="T:System.ArgumentOutOfRangeException">
276 /// The exception is thrown, if <paramref name="month"/>,
277 /// <paramref name="year"/> ,or <paramref name="era"/> is outside
278 /// the allowed range.
279 /// </exception>
280 public override int GetDaysInMonth(int year, int month, int era) {
281 M_CheckYME(year, month, ref era);
282 int rd1 = CCJulianCalendar.fixed_from_dmy(1, month, year);
283 int rd2 = CCJulianCalendar.fixed_from_dmy(1, month+1, year);
284 return rd2 - rd1;
287 /// <summary>
288 /// Overridden. Gives the number of days of the specified
289 /// year of the given era.
290 /// </summary>
291 /// <param name="year">An integer that specifies the year.
292 /// </param>
293 /// <param name="era">An ineger that specifies the era.
294 /// </param>
295 /// <returns>An integer that gives the number of days of the
296 /// specified year.</returns>
297 /// <exception cref="T:System.ArgumentOutOfRangeExceiption">
298 /// The exception is thrown, if
299 /// <paramref name="year"/> is outside the allowed range.
300 /// </exception>
301 public override int GetDaysInYear(int year, int era) {
302 M_CheckYE(year, ref era);
303 int rd1 = CCJulianCalendar.fixed_from_dmy(1, 1, year);
304 int rd2 = CCJulianCalendar.fixed_from_dmy(1, 1, year+1);
305 return rd2 - rd1;
309 /// <summary>
310 /// Overridden. Gives the era of the specified date.
311 /// </summary>
312 /// <param name="time">The
313 /// <see cref="T:System.DateTime"/> that specifies a
314 /// date.
315 /// </param>
316 /// <returns>An integer representing the era of the calendar.
317 /// </returns>
318 public override int GetEra(DateTime time) {
319 // should change, if more than one era is supported
320 return JulianEra;
323 /// <summary>
324 /// Overridden. Gives the number of the month of the specified
325 /// date.
326 /// </summary>
327 /// <param name="time">The
328 /// <see cref="T:System.DateTime"/> that specifies a
329 /// date.
330 /// </param>
331 /// <returns>An integer representing the month,
332 /// starting with 1.</returns>
333 public override int GetMonth(DateTime time) {
334 int rd = CCFixed.FromDateTime(time);
335 return CCJulianCalendar.month_from_fixed(rd);
338 /// <summary>
339 /// Overridden. Gives the number of months in the specified year
340 /// and era.
341 /// </summary>
342 /// <param name="year">An integer that specifies the year.
343 /// </param>
344 /// <param name="era">An integer that specifies the era.
345 /// </param>
346 /// <returns>An integer that gives the number of the months in the
347 /// specified year.</returns>
348 /// <exception cref="T:System.ArgumentOutOfRangeException">
349 /// The exception is thrown, if the year or the era are not valid.
350 /// </exception>
351 public override int GetMonthsInYear(int year, int era) {
352 M_CheckYE(year, ref era);
353 return 12;
356 /// <summary>
357 /// Overridden. Gives the number of the year of the specified
358 /// date.
359 /// </summary>
360 /// <param name="time">The
361 /// <see cref="T:System.DateTime"/> that specifies a
362 /// date.
363 /// </param>
364 /// <returns>An integer representing the year,
365 /// starting with 1.</returns>
366 public override int GetYear(DateTime time) {
367 int rd = CCFixed.FromDateTime(time);
368 return CCJulianCalendar.year_from_fixed(rd);
371 /// <summary>
372 /// Overridden. Tells whether the given day
373 /// is a leap day.
374 /// </summary>
375 /// <param name="year">An integer that specifies the year in the
376 /// given era.
377 /// </param>
378 /// <param name="month">An integer that specifies the month.
379 /// </param>
380 /// <param name="day">An integer that specifies the day.
381 /// </param>
382 /// <param name="era">An integer that specifies the era.
383 /// </param>
384 /// <returns>A boolean that tells whether the given day is a leap
385 /// day.
386 /// </returns>
387 /// <exception cref="T:System.ArgumentOutOfRangeException">
388 /// The exception is thrown, if the year, month, day, or era is not
389 /// valid.
390 /// </exception>
391 public override bool IsLeapDay(int year, int month, int day, int era)
393 M_CheckYMDE(year, month, day, ref era);
394 return IsLeapYear(year) && month == 2 && day == 29;
397 /// <summary>
398 /// Overridden. Tells whether the given month
399 /// is a leap month.
400 /// </summary>
401 /// <param name="year">An integer that specifies the year in the
402 /// given era.
403 /// </param>
404 /// <param name="month">An integer that specifies the month.
405 /// </param>
406 /// <param name="era">An integer that specifies the era.
407 /// </param>
408 /// <returns>A boolean that tells whether the given month is a leap
409 /// month.
410 /// </returns>
411 /// <exception cref="T:System.ArgumentOutOfRangeException">
412 /// The exception is thrown, if the year, month, or era is not
413 /// valid.
414 /// </exception>
415 public override bool IsLeapMonth(int year, int month, int era) {
416 M_CheckYME(year, month, ref era);
417 return false;
420 /// <summary>
421 /// Overridden. Tells whether the given year
422 /// is a leap year.
423 /// </summary>
424 /// <param name="year">An integer that specifies the year in the
425 /// given era.
426 /// </param>
427 /// <param name="era">An integer that specifies the era.
428 /// </param>
429 /// <returns>A boolean that tells whether the given year is a leap
430 /// year.
431 /// </returns>
432 /// <exception cref="T:System.ArgumentOutOfRangeException">
433 /// The exception is thrown, if the year or era is not
434 /// valid.
435 /// </exception>
436 public override bool IsLeapYear(int year, int era) {
437 M_CheckYE(year, ref era);
438 return CCJulianCalendar.is_leap_year(year);
441 /// <summary>
442 /// Overridden. Creates the
443 /// <see cref="T:System.DateTime"/> from the parameters.
444 /// </summary>
445 /// <param name="year">An integer that gives the year in the
446 /// <paramref name="era"/>.
447 /// </param>
448 /// <param name="month">An integer that specifies the month.
449 /// </param>
450 /// <param name="day">An integer that specifies the day.
451 /// </param>
452 /// <param name="hour">An integer that specifies the hour.
453 /// </param>
454 /// <param name="minute">An integer that specifies the minute.
455 /// </param>
456 /// <param name="second">An integer that gives the second.
457 /// </param>
458 /// <param name="milliseconds">An integer that gives the
459 /// milliseconds.
460 /// </param>
461 /// <param name="era">An integer that specifies the era.
462 /// </param>
463 /// <returns>
464 /// <see cref="T:system.DateTime"/> representig the date and time.
465 /// </returns>
466 /// <exception cref="T:System.ArgumentOutOfRangeException">
467 /// The exception is thrown, if at least one of the parameters
468 /// is out of range.
469 /// </exception>
470 public override DateTime ToDateTime(int year, int month, int day,
471 int hour, int minute, int second, int milliseconds,
472 int era)
474 M_CheckYMDE(year, month, day, ref era);
475 M_CheckHMSM(hour, minute, second, milliseconds);
476 int rd = CCJulianCalendar.fixed_from_dmy(day, month, year);
477 return CCFixed.ToDateTime(rd,
478 hour, minute, second, milliseconds);
481 [MonoTODO("Not supported")]
482 public override int ToFourDigitYear(int year)
484 throw new NotImplementedException();
487 #if NET_2_0
488 public override CalendarAlgorithmType AlgorithmType {
489 get {
490 return CalendarAlgorithmType.SolarCalendar;
494 static DateTime JulianMin = new DateTime (1, 1, 1, 0, 0, 0);
495 static DateTime JulianMax = new DateTime (9999, 12, 31, 11, 59, 59);
497 public override DateTime MinSupportedDateTime {
498 get {
499 return JulianMin;
503 public override DateTime MaxSupportedDateTime {
504 get {
505 return JulianMax;
508 #endif
510 } // class JulianCalendar
512 } // namespace System.Globalization