3 // (C) Ulrich Kunitz 2002
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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 {
35 /// This is the Hebrew calendar.
38 /// <para>The Hebrew calendar supports only years between 5343 A.M. and
39 /// 6000 A.M. This has been done to be compatible with .NET.
41 /// <para>The implementation uses the
42 /// <see cref="N:CalendricalCalculations"/> namespace.
46 [MonoTODO ("Fix serialization compatibility with MS.NET")]
47 public class HebrewCalendar : Calendar {
51 public HebrewCalendar() {
52 M_AbbrEraNames = new string[] {"A.M."};
53 M_EraNames = new string[] {"Anno Mundi"};
54 if (M_TwoDigitYearMax == 99)
55 M_TwoDigitYearMax = 5790;
59 /// The era number for the Anno Mundi (A.M.) era, called
62 public static readonly int HebrewEra = 1;
66 /// <see cref="T:System.DateTime"/> ticks for first day of year
69 internal const long M_MinTicks = 499147488000000000L;
72 /// <see cref="T:System.DateTime"/> ticks for the last day of year
75 internal const long M_MaxTicks = 706783967999999999L;
77 /// The minimum year in the A.M. era supported.
79 internal const int M_MinYear = 5343;
81 /// The maximum year supported in the A.M. era.
83 internal override int M_MaxYear {
87 /// <value>Overridden. Gives the eras supported by the Gregorian
88 /// calendar as an array of integers.
90 public override int[] Eras {
92 return new int[] { HebrewEra };
96 int twoDigitYearMax = 5790;
98 public override int TwoDigitYearMax
101 return twoDigitYearMax;
105 M_ArgumentInRange ("value", value, M_MinYear, M_MaxYear);
107 twoDigitYearMax = value;
112 /// A protected member checking a
113 /// <see cref="T:System.DateTime"/> value.
115 /// <param name="time">The
116 /// <see cref="T:System.DateTime"/>
119 /// <exception cref="T:System.ArgumentOutOfRangeException">
120 /// The exception is thrown if the
121 /// <see cref="T:System.DateTime"/> parameter is not in the years
122 /// between 5343 A.M. and 6000 A.M., inclusive.
124 internal void M_CheckDateTime(DateTime time) {
125 if (time.Ticks < M_MinTicks || time.Ticks > M_MaxTicks)
126 throw new ArgumentOutOfRangeException(
128 "Only hebrew years between 5343 and 6000," +
129 " inclusive, are supported.");
133 /// A protected method checking the era number.
135 /// <param name="era">The era number.</param>
136 /// <exception name="T:System.ArgumentException">
137 /// The exception is thrown if the era is not equal
138 /// <see cref="F:HebrewEra"/>.
140 internal void M_CheckEra(ref int era) {
141 if (era == CurrentEra)
143 if (era != HebrewEra)
144 throw new ArgumentException("Era value was not valid.");
148 /// A protected method checking calendar year and the era number.
150 /// <param name="year">An integer representing the calendar year.
152 /// <param name="era">The era number.</param>
153 /// <exception cref="T:System.ArgumentException">
154 /// The exception is thrown if the era is not equal
155 /// <see cref="F:HebrewEra"/>.
157 /// <exception cref="T:System.ArgumentOutOfRangeException">
158 /// The exception is thrown if the calendar year is outside of
159 /// the allowed range.
161 internal override void M_CheckYE(int year, ref int era) {
163 if (year < M_MinYear || year > M_MaxYear)
164 throw new ArgumentOutOfRangeException(
166 "Only hebrew years between 5343 and 6000," +
167 " inclusive, are supported.");
171 /// A protected method checking the calendar year, month, and
174 /// <param name="year">An integer representing the calendar year.
176 /// <param name="month">An integer giving the calendar month.
178 /// <param name="era">The era number.</param>
179 /// <exception cref="T:System.ArgumentException">
180 /// The exception is thrown if the era is not equal
181 /// <see cref="F:HebrewEra"/>.
183 /// <exception cref="T:System.ArgumentOutOfRangeException">
184 /// The exception is thrown if the calendar year or month is
185 /// outside of the allowed range.
187 internal void M_CheckYME(int year, int month, ref int era) {
188 M_CheckYE(year, ref era);
189 int l = CCHebrewCalendar.last_month_of_year(year);
190 if (month < 1 || month > l) {
191 StringWriter sw = new StringWriter();
192 sw.Write("Month must be between 1 and {0}.", l);
193 throw new ArgumentOutOfRangeException("month",
199 /// A protected method checking the calendar day, month, and year
200 /// and the era number.
202 /// <param name="year">An integer representing the calendar year.
204 /// <param name="month">An integer giving the calendar month.
206 /// <param name="day">An integer giving the calendar day.
208 /// <param name="era">The era number.</param>
209 /// <exception cref="T:System.ArgumentException">
210 /// The exception is thrown if the era is not equal
211 /// <see cref="F:HebrewEra"/>.
213 /// <exception cref="T:System.ArgumentOutOfRangeException">
214 /// The exception is thrown if the calendar year, month, or day is
215 /// outside of the allowed range.
217 internal void M_CheckYMDE(int year, int month, int day,
220 M_CheckYME(year, month, ref era);
221 M_ArgumentInRange("day", day, 1, GetDaysInMonth(year, month,
227 /// Overrideden. Adds months to a given date.
229 /// <param name="time">The
230 /// <see cref="T:System.DateTime"/> to which to add
233 /// <param name="months">The number of months to add.</param>
234 /// <returns>A new <see cref="T:System.DateTime"/> value, that
235 /// results from adding <paramref name="months"/> to the specified
236 /// DateTime.</returns>
237 /// <exception cref="T:System.ArgumentOutOfRangeException">
238 /// The exception is thrown if the
239 /// <see cref="T:System.DateTime"/> return value is not in the years
240 /// between 5343 A.M. and 6000 A.M., inclusive.
242 public override DateTime AddMonths(DateTime time, int months) {
249 int rd = CCFixed.FromDateTime(time);
250 CCHebrewCalendar.dmy_from_fixed(
251 out d, out m, out y, rd);
261 m = GetMonthsInYear(y);
267 int my = GetMonthsInYear(y);
268 if (m+months <= my) {
278 t = ToDateTime(y, m, d, 0, 0, 0, 0);
279 t = t.Add(time.TimeOfDay);
286 /// Overridden. Adds years to a given date.
288 /// <param name="time">The
289 /// <see cref="T:System.DateTime"/> to which to add
292 /// <param name="years">The number of years to add.</param>
293 /// <returns>A new <see cref="T:System.DateTime"/> value, that
294 /// results from adding <paramref name="years"/> to the specified
295 /// DateTime.</returns>
296 /// <exception cref="T:System.ArgumentOutOfRangeException">
297 /// The exception is thrown if the
298 /// <see cref="T:System.DateTime"/> return value is not in the years
299 /// between 5343 A.M. and 6000 A.M., inclusive.
301 public override DateTime AddYears(DateTime time, int years) {
302 int rd = CCFixed.FromDateTime(time);
303 int day, month, year;
304 CCHebrewCalendar.dmy_from_fixed(
305 out day, out month, out year, rd);
307 rd = CCHebrewCalendar.fixed_from_dmy(day, month, year);
308 DateTime t = CCFixed.ToDateTime(rd);
309 t = t.Add(time.TimeOfDay);
315 /// Overriden. Gets the day of the month from
316 /// <paramref name="time"/>.
318 /// <param name="time">The
319 /// <see cref="T:System.DateTime"/> that specifies a
322 /// <returns>An integer giving the day of months, starting with 1.
324 /// <exception cref="T:System.ArgumentOutOfRangeException">
325 /// The exception is thrown if the
326 /// <see cref="T:System.DateTime"/> parameter is not in the years
327 /// between 5343 A.M. and 6000 A.M., inclusive.
329 public override int GetDayOfMonth(DateTime time) {
330 M_CheckDateTime(time);
331 int rd = CCFixed.FromDateTime(time);
332 return CCHebrewCalendar.day_from_fixed(rd);
336 /// Overriden. Gets the day of the week from the specified date.
338 /// <param name="time">The
339 /// <see cref="T:System.DateTime"/> that specifies a
342 /// <returns>An integer giving the day of months, starting with 1.
344 /// <exception cref="T:System.ArgumentOutOfRangeException">
345 /// The exception is thrown if the
346 /// <see cref="T:System.DateTime"/> parameter is not in the years
347 /// between 5343 A.M. and 6000 A.M., inclusive.
349 public override DayOfWeek GetDayOfWeek(DateTime time) {
350 M_CheckDateTime(time);
351 int rd = CCFixed.FromDateTime(time);
352 return (DayOfWeek)CCFixed.day_of_week(rd);
356 /// Overridden. Gives the number of the day in the year.
358 /// <param name="time">The
359 /// <see cref="T:System.DateTime"/> that specifies a
362 /// <returns>An integer representing the day of the year,
363 /// starting with 1.</returns>
364 /// <exception cref="T:System.ArgumentOutOfRangeException">
365 /// The exception is thrown if the
366 /// <see cref="T:System.DateTime"/> parameter is not in the years
367 /// between 5343 A.M. and 6000 A.M., inclusive.
369 public override int GetDayOfYear(DateTime time) {
370 M_CheckDateTime(time);
371 int rd = CCFixed.FromDateTime(time);
372 int year = CCHebrewCalendar.year_from_fixed(rd);
373 int rd1_7 = CCHebrewCalendar.fixed_from_dmy(1, 7, year);
374 return rd - rd1_7 + 1;
378 /// The method maps a .NET Hebrew month to a Calencdrical
379 /// Calculations Hebrew month.
381 /// <param name="month">An integer representing a month in .NET
382 /// counting (starting with Tishri).
384 /// <param name="year">An integer representing the Hebrew year.
386 /// <returns>The Hebrew month in Calendrical Calculations counting,
387 /// staring with the Hebrew month Nisan.
391 /// In .NET the month counting starts with the Hebrew month Tishri.
392 /// Calendrical Calculations starts with the month Tisan. So we must
396 internal int M_CCMonth(int month, int year) {
401 int l = CCHebrewCalendar.last_month_of_year(year);
406 return month <= 7 ? 6+month : month-7;
412 /// The method maps a Calendrical Calculations Hebrew month
413 /// to a .NET Hebrew month.
415 /// <param name="ccmonth">An integer representing a month in
416 /// Calendrical Calculations counting, starting with Nisan.
418 /// <param name="year">An integer representing the Hebrew year.
420 /// <returns>The Hebrew month in .NET counting,
421 /// staring with the Hebrew month Tishri.
425 /// In .NET the month counting starts with the Hebrew month Tishri.
426 /// Calendrical Calculations starts with the month Tisan. So we must
430 internal int M_Month(int ccmonth, int year) {
434 int l = CCHebrewCalendar.last_month_of_year(year);
435 return ccmonth + (l == 12 ? 6 : 7);
440 /// Overridden. Gives the number of days in the specified month
441 /// of the given year and era.
443 /// <param name="year">An integer that gives the year.
445 /// <param name="month">An integer that gives the month, starting
447 /// <param name="era">An integer that gives the era of the specified
449 /// <returns>An integer that gives the number of days of the
450 /// specified month.</returns>
451 /// <exception cref="T:System.ArgumentOutOfRangeException">
452 /// The exception is thrown, if <paramref name="month"/>,
453 /// <paramref name="year"/> ,or <paramref name="era"/> is outside
454 /// the allowed range.
456 public override int GetDaysInMonth(int year, int month, int era) {
457 M_CheckYME(year, month, ref era);
458 int ccmonth = M_CCMonth(month, year);
459 int rd1 = CCHebrewCalendar.fixed_from_dmy(1, ccmonth, year);
460 int rd2 = CCHebrewCalendar.fixed_from_dmy(1, ccmonth+1, year);
465 /// Overridden. Gives the number of days of the specified
466 /// year of the given era.
468 /// <param name="year">An integer that specifies the year.
470 /// <param name="era">An ineger that specifies the era.
472 /// <returns>An integer that gives the number of days of the
473 /// specified year.</returns>
474 /// <exception cref="T:System.ArgumentOutOfRangeExceiption">
475 /// The exception is thrown, if
476 /// <paramref name="year"/> or <paramref name="era"/> are outside the
479 public override int GetDaysInYear(int year, int era) {
480 M_CheckYE(year, ref era);
481 int rd1 = CCHebrewCalendar.fixed_from_dmy(1, 7, year);
482 int rd2 = CCHebrewCalendar.fixed_from_dmy(1, 7, year+1);
488 /// Overridden. Gives the era of the specified date.
490 /// <param name="time">The
491 /// <see cref="T:System.DateTime"/> that specifies a
494 /// <returns>An integer representing the era of the calendar.
496 /// <exception cref="T:System.ArgumentOutOfRangeException">
497 /// The exception is thrown if the
498 /// <see cref="T:System.DateTime"/> parameter is not in the years
499 /// between 5343 A.M. and 6000 A.M., inclusive.
501 public override int GetEra(DateTime time) {
502 M_CheckDateTime(time);
507 /// Overridden. Gives the number of the month of the specified
510 /// <param name="time">The
511 /// <see cref="T:System.DateTime"/> that specifies a
514 /// <returns>An integer representing the month,
515 /// starting with 1.</returns>
516 /// <exception cref="T:System.ArgumentOutOfRangeException">
517 /// The exception is thrown if the
518 /// <see cref="T:System.DateTime"/> parameter is not in the years
519 /// between 5343 A.M. and 6000 A.M., inclusive.
521 public override int GetMonth(DateTime time) {
522 M_CheckDateTime(time);
523 int rd = CCFixed.FromDateTime(time);
525 CCHebrewCalendar.my_from_fixed(out ccmonth, out year, rd);
526 return M_Month(ccmonth, year);
530 /// Overridden. Gives the number of months in the specified year
533 /// <param name="year">An integer that specifies the year.
535 /// <param name="era">An integer that specifies the era.
537 /// <returns>An integer that gives the number of the months in the
538 /// specified year.</returns>
539 /// <exception cref="T:System.ArgumentOutOfRangeException">
540 /// The exception is thrown, if the year or the era are not valid.
542 public override int GetMonthsInYear(int year, int era) {
543 M_CheckYE(year, ref era);
544 return CCHebrewCalendar.last_month_of_year(year);
548 /// Overridden. Gives the number of the year of the specified
551 /// <param name="time">The
552 /// <see cref="T:System.DateTime"/> that specifies a
555 /// <returns>An integer representing the year,
556 /// starting with 1.</returns>
557 /// <exception cref="T:System.ArgumentOutOfRangeException">
558 /// The exception is thrown if the
559 /// <see cref="T:System.DateTime"/> parameter is not in the years
560 /// between 5343 A.M. and 6000 A.M., inclusive.
562 public override int GetYear(DateTime time) {
563 M_CheckDateTime(time);
564 int rd = CCFixed.FromDateTime(time);
565 return CCHebrewCalendar.year_from_fixed(rd);
569 /// Overridden. Tells whether the given day
572 /// <param name="year">An integer that specifies the year in the
575 /// <param name="month">An integer that specifies the month.
577 /// <param name="day">An integer that specifies the day.
579 /// <param name="era">An integer that specifies the era.
581 /// <returns>A boolean that tells whether the given day is a leap
584 /// <exception cref="T:System.ArgumentOutOfRangeException">
585 /// The exception is thrown, if the year, month, day, or era is not
588 /// <remarks>All days in Adar II are viewed as leap days and the
589 /// last day of Adar I.
591 public override bool IsLeapDay(int year, int month, int day, int era)
593 M_CheckYMDE(year, month, day, ref era);
594 return IsLeapYear(year) &&
595 (month == 7 || (month == 6 && day == 30));
599 /// Overridden. Tells whether the given month
602 /// <param name="year">An integer that specifies the year in the
605 /// <param name="month">An integer that specifies the month.
607 /// <param name="era">An integer that specifies the era.
609 /// <returns>A boolean that tells whether the given month is a leap
612 /// <exception cref="T:System.ArgumentOutOfRangeException">
613 /// The exception is thrown, if the year, month, or era is not
617 /// Adar II is viewed as leap month.
619 public override bool IsLeapMonth(int year, int month, int era) {
620 M_CheckYME(year, month, ref era);
621 return IsLeapYear(year) && month == 7;
625 /// Overridden. Tells whether the given year
628 /// <param name="year">An integer that specifies the year in the
631 /// <param name="era">An integer that specifies the era.
633 /// <returns>A boolean that tells whether the given year is a leap
636 /// <exception cref="T:System.ArgumentOutOfRangeException">
637 /// The exception is thrown, if the year or era is not
640 public override bool IsLeapYear(int year, int era) {
641 M_CheckYE(year, ref era);
642 return CCHebrewCalendar.is_leap_year(year);
646 /// Overridden. Creates the
647 /// <see cref="T:System.DateTime"/> from the parameters.
649 /// <param name="year">An integer that gives the year in the
650 /// <paramref name="era"/>.
652 /// <param name="month">An integer that specifies the month.
654 /// <param name="day">An integer that specifies the day.
656 /// <param name="hour">An integer that specifies the hour.
658 /// <param name="minute">An integer that specifies the minute.
660 /// <param name="second">An integer that gives the second.
662 /// <param name="milliseconds">An integer that gives the
665 /// <param name="era">An integer that specifies the era.
668 /// <see cref="T:system.DateTime"/> representig the date and time.
670 /// <exception cref="T:System.ArgumentOutOfRangeException">
671 /// The exception is thrown, if at least one of the parameters
674 public override DateTime ToDateTime(int year, int month, int day,
675 int hour, int minute, int second, int milliseconds,
678 M_CheckYMDE(year, month, day, ref era);
679 M_CheckHMSM(hour, minute, second, milliseconds);
680 int ccm = M_CCMonth(month, year);
681 int rd = CCHebrewCalendar.fixed_from_dmy(day, ccm, year);
682 return CCFixed.ToDateTime(rd,
683 hour, minute, second, milliseconds);
686 public override int ToFourDigitYear (int year)
688 M_ArgumentInRange ("year", year, 0, 99);
690 int baseExtra = this.twoDigitYearMax % 100;
691 int baseCentury = this.twoDigitYearMax - baseExtra;
693 if (year <= baseExtra)
694 return baseCentury + year;
696 return baseCentury + year - 100;
699 public override CalendarAlgorithmType AlgorithmType {
701 return CalendarAlgorithmType.LunisolarCalendar;
705 static DateTime Min = new DateTime (1583, 1, 1, 0, 0, 0);
706 static DateTime Max = new DateTime (2239, 9, 29, 11, 59, 59);
708 public override DateTime MinSupportedDateTime {
714 public override DateTime MaxSupportedDateTime {
721 } // class HebrewCalendar
723 } // namespace System.Globalization