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 ("Serialization format not compatible with.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 Nisan. 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 return CCHebrewCalendar.last_day_of_month(ccmonth, year);
463 /// Overridden. Gives the number of days of the specified
464 /// year of the given era.
466 /// <param name="year">An integer that specifies the year.
468 /// <param name="era">An ineger that specifies the era.
470 /// <returns>An integer that gives the number of days of the
471 /// specified year.</returns>
472 /// <exception cref="T:System.ArgumentOutOfRangeExceiption">
473 /// The exception is thrown, if
474 /// <paramref name="year"/> or <paramref name="era"/> are outside the
477 public override int GetDaysInYear(int year, int era) {
478 M_CheckYE(year, ref era);
479 int rd1 = CCHebrewCalendar.fixed_from_dmy(1, 7, year);
480 int rd2 = CCHebrewCalendar.fixed_from_dmy(1, 7, year+1);
486 /// Overridden. Gives the era of the specified date.
488 /// <param name="time">The
489 /// <see cref="T:System.DateTime"/> that specifies a
492 /// <returns>An integer representing the era of the calendar.
494 /// <exception cref="T:System.ArgumentOutOfRangeException">
495 /// The exception is thrown if the
496 /// <see cref="T:System.DateTime"/> parameter is not in the years
497 /// between 5343 A.M. and 6000 A.M., inclusive.
499 public override int GetEra(DateTime time) {
500 M_CheckDateTime(time);
505 /// Overridden. Gives the number of the month of the specified
508 /// <param name="time">The
509 /// <see cref="T:System.DateTime"/> that specifies a
512 /// <returns>An integer representing the month,
513 /// starting with 1.</returns>
514 /// <exception cref="T:System.ArgumentOutOfRangeException">
515 /// The exception is thrown if the
516 /// <see cref="T:System.DateTime"/> parameter is not in the years
517 /// between 5343 A.M. and 6000 A.M., inclusive.
519 public override int GetMonth(DateTime time) {
520 M_CheckDateTime(time);
521 int rd = CCFixed.FromDateTime(time);
523 CCHebrewCalendar.my_from_fixed(out ccmonth, out year, rd);
524 return M_Month(ccmonth, year);
528 /// Overridden. Gives the number of months in the specified year
531 /// <param name="year">An integer that specifies the year.
533 /// <param name="era">An integer that specifies the era.
535 /// <returns>An integer that gives the number of the months in the
536 /// specified year.</returns>
537 /// <exception cref="T:System.ArgumentOutOfRangeException">
538 /// The exception is thrown, if the year or the era are not valid.
540 public override int GetMonthsInYear(int year, int era) {
541 M_CheckYE(year, ref era);
542 return CCHebrewCalendar.last_month_of_year(year);
546 /// Overridden. Gives the number of the year of the specified
549 /// <param name="time">The
550 /// <see cref="T:System.DateTime"/> that specifies a
553 /// <returns>An integer representing the year,
554 /// starting with 1.</returns>
555 /// <exception cref="T:System.ArgumentOutOfRangeException">
556 /// The exception is thrown if the
557 /// <see cref="T:System.DateTime"/> parameter is not in the years
558 /// between 5343 A.M. and 6000 A.M., inclusive.
560 public override int GetYear(DateTime time) {
561 M_CheckDateTime(time);
562 int rd = CCFixed.FromDateTime(time);
563 return CCHebrewCalendar.year_from_fixed(rd);
567 /// Overridden. Tells whether the given day
570 /// <param name="year">An integer that specifies the year in the
573 /// <param name="month">An integer that specifies the month.
575 /// <param name="day">An integer that specifies the day.
577 /// <param name="era">An integer that specifies the era.
579 /// <returns>A boolean that tells whether the given day is a leap
582 /// <exception cref="T:System.ArgumentOutOfRangeException">
583 /// The exception is thrown, if the year, month, day, or era is not
586 /// <remarks>All days in Adar II are viewed as leap days and the
587 /// last day of Adar I.
589 public override bool IsLeapDay(int year, int month, int day, int era)
591 M_CheckYMDE(year, month, day, ref era);
592 return IsLeapYear(year) &&
593 (month == 7 || (month == 6 && day == 30));
597 /// Overridden. Tells whether the given month
600 /// <param name="year">An integer that specifies the year in the
603 /// <param name="month">An integer that specifies the month.
605 /// <param name="era">An integer that specifies the era.
607 /// <returns>A boolean that tells whether the given month is a leap
610 /// <exception cref="T:System.ArgumentOutOfRangeException">
611 /// The exception is thrown, if the year, month, or era is not
615 /// Adar II is viewed as leap month.
617 public override bool IsLeapMonth(int year, int month, int era) {
618 M_CheckYME(year, month, ref era);
619 return IsLeapYear(year) && month == 7;
623 /// Overridden. Tells whether the given year
626 /// <param name="year">An integer that specifies the year in the
629 /// <param name="era">An integer that specifies the era.
631 /// <returns>A boolean that tells whether the given year is a leap
634 /// <exception cref="T:System.ArgumentOutOfRangeException">
635 /// The exception is thrown, if the year or era is not
638 public override bool IsLeapYear(int year, int era) {
639 M_CheckYE(year, ref era);
640 return CCHebrewCalendar.is_leap_year(year);
644 /// Overridden. Creates the
645 /// <see cref="T:System.DateTime"/> from the parameters.
647 /// <param name="year">An integer that gives the year in the
648 /// <paramref name="era"/>.
650 /// <param name="month">An integer that specifies the month.
652 /// <param name="day">An integer that specifies the day.
654 /// <param name="hour">An integer that specifies the hour.
656 /// <param name="minute">An integer that specifies the minute.
658 /// <param name="second">An integer that gives the second.
660 /// <param name="milliseconds">An integer that gives the
663 /// <param name="era">An integer that specifies the era.
666 /// <see cref="T:system.DateTime"/> representig the date and time.
668 /// <exception cref="T:System.ArgumentOutOfRangeException">
669 /// The exception is thrown, if at least one of the parameters
672 public override DateTime ToDateTime(int year, int month, int day,
673 int hour, int minute, int second, int milliseconds,
676 M_CheckYMDE(year, month, day, ref era);
677 M_CheckHMSM(hour, minute, second, milliseconds);
678 int ccm = M_CCMonth(month, year);
679 int rd = CCHebrewCalendar.fixed_from_dmy(day, ccm, year);
680 return CCFixed.ToDateTime(rd,
681 hour, minute, second, milliseconds);
684 public override int ToFourDigitYear (int year)
686 M_ArgumentInRange ("year", year, 0, 99);
688 int baseExtra = this.twoDigitYearMax % 100;
689 int baseCentury = this.twoDigitYearMax - baseExtra;
691 if (year <= baseExtra)
692 return baseCentury + year;
694 return baseCentury + year - 100;
697 public override CalendarAlgorithmType AlgorithmType {
699 return CalendarAlgorithmType.LunisolarCalendar;
703 static DateTime Min = new DateTime (1583, 1, 1, 0, 0, 0);
704 static DateTime Max = new DateTime (2239, 9, 29, 11, 59, 59);
706 public override DateTime MinSupportedDateTime {
712 public override DateTime MaxSupportedDateTime {
719 } // class HebrewCalendar
721 } // namespace System.Globalization