1 // CalendricalCalculations.cs
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 {
31 using System.Collections;
33 /// <summary>A class that provides mathematical functions.</summary>
36 /// We are breaking the .Net
37 /// naming conventions to be compatible to the "Calendrical Calculations"
41 internal class CCMath {
43 /// A static method which rounds a double value.
45 /// <param name="x">The double value to round.</param>
46 /// <returns>The rounded double.</returns>
47 public static double round(double x) {
48 return System.Math.Floor(x+0.5);
52 /// A static method that computes the remainder of the division
55 /// <param name="x">The double value which is divided.</param>
56 /// <param name="y">The divisor.</param>
57 /// <returns>The remainder as double value.</returns>
58 public static double mod(double x, double y) {
59 return x - y * System.Math.Floor(x/y);
63 /// The static method divides two integers.
65 /// <param name="x">The integer x value.</param>
66 /// <param name="y">The integer y value.</param>
67 /// <returns>The qotient of x and y defined by floor(x/y).
70 /// Please notify that the function is not compatible to the standard
71 /// integer divide operation /.
73 public static int div(int x, int y) {
74 return (int)System.Math.Floor((double)x/(double)y);
78 /// The static method computes the remainder of two integers.
80 /// <param name="x">The integer value which will be divided.</param>
81 /// <param name="y">The divisor integer value.</param>
82 /// <returns> The remainder as integer value.</returns>
84 /// Please notify that the method is not compatible to the C#
85 /// remainder operation %.
87 public static int mod(int x, int y) {
88 return x - y * div(x, y);
92 /// A static method that combines integer division and remainder
95 /// <param name="remainder">Remainder integer output value.
97 /// <param name="x">Integer to be divided.</param>
98 /// <param name="y">Divisor integer value.</param>
99 /// <returns>The quotient as integer.</returns>
100 /// <seealso cref="M:div"/>
101 /// <seealso cref="M:mod"/>
102 public static int div_mod(out int remainder, int x, int y) {
104 remainder = x - y * d;
109 /// A static method returning the sign of the argument.
111 /// <param name="x">The double argument.</param>
112 /// <returns>An integer value: -1 for a negative argument;
113 /// 0 for a zero argument, and 1 for a positive argument.
115 public static int signum(double x) {
124 /// A static method returning the sign of the integer
127 /// <param name="x">The integer argument.</param>
128 /// <returns>An integer value: -1 for a negative argument;
129 /// 0 for a zero argument, and 1 for a positive argument.
131 public static int signum(int x) {
140 /// An adjusted remainder function as defined in "Calendrical
143 /// <param name="x">The double x argument.</param>
144 /// <param name="y">The double y argument, the divisor.</param>
145 /// <returns>A double value representing remainder; but instead 0.0
146 /// the divisor y is returned.
148 public static double amod(double x, double y) {
149 double d = mod(x, y);
150 return (d == 0.0) ? y : d;
154 /// The adjusted remainder functions for integers as defined in
155 /// "Calendrical Calculations".
157 /// <param name="x">The integer argument to be divided.</param>
158 /// <param name="y">The integer divisor argument.</param>
159 /// <returns>The remainder as an integer; however instead 0
160 /// is the divisor y returned.
162 public static int amod(int x, int y) {
164 return (i == 0) ? y : i;
168 /// <summary>The class implements methods to handle the fixed date value from
169 /// the "Calendrical Calculations" books.
173 /// For implementing the Calendar classes I used the algorithms from the
174 /// book "Calendrical Calculations" by Nachum Dershowitz and Edward M.
175 /// Rheingold, second reprint 1998. Trying to prevent the introduction of new
176 /// bugs, I implemented their algorithms in the
177 /// <see cref="N:CalendricalCalculations"/>
178 /// namespace and wrapped it in the calendar classes.
181 /// The fixed day number is also known as R.D. - rata die.
182 /// Midnight at the onset of Monday,
183 /// January 1, year 1 (Gregorian) is R.D. 1.
185 /// <para>Here are all my references:</para>
186 /// <list type="table">
187 /// <item><description>
188 /// [1] Nachum Dershowitz and Edward M. Rheingold: "Calendrical Calculations";
189 /// Cambridge University Press; second reprint 1998.
190 /// </description></item>
191 /// <item><description>
192 /// [2] P. Kenneth Seidelmann (ed.): "Explanatory Supplement to the Astronomical
193 /// Almanac"; University Science Books, Sausalito; 1992
194 /// </description></item>
195 /// <item><description>
196 /// [3] F. Richard Stephenson: "Historical Eclipses and Earth Rotation";
197 /// Cambridge University Press; 1997
198 /// </description></item>
201 internal class CCFixed {
202 /// <summary>The method computes the
203 /// <see cref="T:System.DateTime"/>
204 /// from a fixed day number.
206 /// <param name="date">A integer representing the fixed day number.
208 /// <returns>The <see cref="T:System.DateTime"/> representing
211 public static System.DateTime ToDateTime(int date) {
212 long ticks = (date - 1) * System.TimeSpan.TicksPerDay;
213 return new System.DateTime(ticks);
216 /// <summary>The method computes the
217 /// <see cref="T:System.DateTime"/>
218 /// from a fixed day number and time arguments.
220 /// <param name="date">An integer representing the fixed day number.
222 /// <param name="hour">An integer argument specifying the hour.
224 /// <param name="minute">An integer argument specifying the minute.
226 /// <param name="second">An integer argument giving the second.
228 /// <param name="milliseconds">An double argument specifying
229 /// the milliseconds. Notice that
230 /// <see cref="T:System.DateTime"/> has 100 nanosecond resolution.
232 /// <returns>The <see cref="T:System.DateTime"/> representing
235 public static System.DateTime ToDateTime(int date,
236 int hour, int minute, int second, double milliseconds)
238 System.DateTime time = ToDateTime(date);
239 time = time.AddHours(hour);
240 time = time.AddMinutes(minute);
241 time = time.AddSeconds(second);
242 return time.AddMilliseconds(milliseconds);
246 /// A static method computing the fixed day number from a
247 /// <see cref="T:System.DateTime"/> value.
249 /// <param name="time">A
250 /// <see cref="T:System.DateTime"/> value representing the date.
252 /// <returns>The fixed day number as integer representing the date.
254 public static int FromDateTime(System.DateTime time) {
255 return 1 + (int)(time.Ticks / System.TimeSpan.TicksPerDay);
259 /// The static method computes the <see cref="T:DayOfWeek"/>.
261 /// <param name="date">An integer representing the fixed day number.
263 /// <returns>The day of week.</returns>
264 public static DayOfWeek day_of_week(int date) {
265 return (DayOfWeek)CCMath.mod(date, 7);
269 /// The static method computes the date of a day of week on or before
270 /// a particular date.
272 /// <param name="date">An integer representing the date as
273 /// fixed day number.
275 /// <param name="k">An integer representing the day of the week,
276 /// starting with 0 for sunday.
278 /// <returns>The fixed day number of the day of week specified by k
279 /// on or before the given date.
281 public static int kday_on_or_before(int date, int k) {
282 return date - (int)day_of_week(date-k);
286 /// The static method computes the date of a day of week on or after
287 /// a particular date.
289 /// <param name="date">An integer representing the date as
290 /// fixed day number.
292 /// <param name="k">An integer representing the day of the week,
293 /// starting with 0 for sunday.
295 /// <returns>The fixed day number of the day of week specified by k
296 /// on or after the given date.
298 public static int kday_on_or_after(int date, int k) {
299 return kday_on_or_before(date+6, k);
303 /// The static method computes the date of a day of week that is
304 /// nearest to a particular date.
306 /// <param name="date">An integer representing the date as
307 /// fixed day number.
309 /// <param name="k">An integer representing the day of the week,
310 /// starting with 0 for sunday.
312 /// <returns>The fixed day number of the day of week neares to the
315 public static int kd_nearest(int date, int k) {
316 return kday_on_or_before(date+3, k);
320 /// The static method computes the date of a day of week after
321 /// a particular date.
323 /// <param name="date">An integer representing the date as
324 /// fixed day number.
326 /// <param name="k">An integer representing the day of the week,
327 /// starting with 0 for sunday.
329 /// <returns>The fixed day number of the day of week specified by k
330 /// after the given date.
332 public static int kday_after(int date, int k) {
333 return kday_on_or_before(date+7, k);
337 /// The static method computes the date of a day of week before
338 /// a particular date.
340 /// <param name="date">An integer representing the date as
341 /// fixed day number.
343 /// <param name="k">An integer representing the day of the week,
344 /// starting with 0 for sunday.
346 /// <returns>The fixed day number of the day of week specified by k
347 /// before the given date.
349 public static int kday_before(int date, int k) {
350 return kday_on_or_before(date-1, k);
355 /// A class encapsulating the functions of the Gregorian calendar as static
360 /// This class is not compatible to
361 /// <see cref="T:System.Globalization.GregorianCalendar"/>.
364 /// The fixed day number is also known as R.D. - rata die.
365 /// Midnight at the onset of Monday,
366 /// January 1, year 1 (Gregorian) is R.D. 1.
368 /// <seealso cref="T:CCFixed"/>
370 internal class CCGregorianCalendar {
371 /// <summary>An integer defining the epoch of the Gregorian calendar
372 /// as fixed day number.</summary>
373 /// <remarks>The epoch is January 3, 1 C.E. (Julian).</remarks>
376 /// <summary>The enumeration defines the months of the Gregorian
432 /// The method tells whether the year is a leap year.
434 /// <param name="year">An integer representing the Gregorian year.
436 /// <returns>A boolean which is true if <paramref name="year"/> is
439 public static bool is_leap_year(int year) {
440 if (CCMath.mod(year, 4) != 0)
442 switch (CCMath.mod(year, 400)) {
454 /// The method returns the fixed day number of the given Gregorian
457 /// <param name="day">An integer representing the day of the month,
460 /// <param name="month">An integer representing the month in the
463 /// <param name="year">An integer representing the Gregorian year.
464 /// Non-positive values are allowed also.
466 /// <returns>An integer value representing the fixed day number.
468 public static int fixed_from_dmy(int day, int month, int year) {
471 k += CCMath.div(year-1, 4);
472 k -= CCMath.div(year-1, 100);
473 k += CCMath.div(year-1, 400);
474 k += CCMath.div(367*month-362, 12);
476 k += is_leap_year(year) ? -1 : -2;
485 /// The method computes the Gregorian year from a fixed day number.
487 /// <param name="date">The fixed day number.
489 /// <returns>An integer value giving the Gregorian year of the date.
491 public static int year_from_fixed(int date) {
492 int d = date - epoch;
493 int n_400 = CCMath.div_mod(out d, d, 146097);
494 int n_100 = CCMath.div_mod(out d, d, 36524);
495 int n_4 = CCMath.div_mod(out d, d, 1461);
496 int n_1 = CCMath.div(d, 365);
498 int year = 400*n_400 + 100*n_100 + 4*n_4 + n_1;
499 return (n_100 == 4 || n_1 == 4) ? year : year + 1;
503 /// The method computes the Gregorian year and month from a fixed day
506 /// <param name="month">The output value giving the Gregorian month.
508 /// <param name="year">The output value giving the Gregorian year.
510 /// <param name="date">An integer value specifying the fixed day
512 public static void my_from_fixed(out int month, out int year,
515 year = year_from_fixed(date);
517 int prior_days = date - fixed_from_dmy(1, (int)Month.january,
521 if (date < fixed_from_dmy(1, (int)Month.march, year)) {
523 } else if (is_leap_year(year)) {
529 month = CCMath.div(12 * (prior_days + correction) + 373, 367);
534 /// The method computes the Gregorian year, month, and day from a
535 /// fixed day number.
537 /// <param name="day">The output value returning the day of the
540 /// <param name="month">The output value giving the Gregorian month.
542 /// <param name="year">The output value giving the Gregorian year.
544 /// <param name="date">An integer value specifying the fixed day
546 public static void dmy_from_fixed(out int day, out int month,
550 my_from_fixed(out month, out year, date);
551 day = date - fixed_from_dmy(1, month, year) + 1;
554 /// <summary>A method computing the Gregorian month from a fixed
557 /// <param name="date">An integer specifying the fixed day number.
559 /// <returns>An integer value representing the Gregorian month.
561 public static int month_from_fixed(int date) {
564 my_from_fixed(out month, out year, date);
569 /// A method computing the day of the month from a fixed day number.
571 /// <param name="date">An integer specifying the fixed day number.
573 /// <returns>An integer value representing the day of the month.
575 public static int day_from_fixed(int date) {
576 int day, month, year;
578 dmy_from_fixed(out day, out month, out year, date);
583 /// The method computes the difference between two Gregorian dates.
585 /// <param name="dayA">The integer parameter gives the day of month
586 /// of the first date.
588 /// <param name="monthA">The integer parameter gives the Gregorian
589 /// month of the first date.
591 /// <param name="yearA">The integer parameter gives the Gregorian
592 /// year of the first date.
594 /// <param name="dayB">The integer parameter gives the day of month
595 /// of the second date.
597 /// <param name="monthB">The integer parameter gives the Gregorian
598 /// month of the second date.
600 /// <param name="yearB">The integer parameter gives the Gregorian
601 /// year of the second date.
603 /// <returns>An integer giving the difference of days from the first
606 public static int date_difference(int dayA, int monthA, int yearA,
607 int dayB, int monthB, int yearB)
609 return fixed_from_dmy(dayB, monthB, yearB) -
610 fixed_from_dmy(dayA, monthA, yearA);
614 /// The method computes the number of the day in the year from
615 /// a Gregorian date.
617 /// <param name="day">An integer representing the day of the month,
620 /// <param name="month">An integer representing the month in the
623 /// <param name="year">An integer representing the Gregorian year.
624 /// Non-positive values are allowed also.
626 /// <returns>An integer value giving the number of the day in the
627 /// Gregorian year, counting from 1.
629 public static int day_number(int day, int month, int year) {
630 return date_difference(31, (int)Month.december, year-1,
635 /// The method computes the days remaining in the given Gregorian
636 /// year from a Gregorian date.
638 /// <param name="day">An integer representing the day of the month,
641 /// <param name="month">An integer representing the month in the
644 /// <param name="year">An integer representing the Gregorian year.
645 /// Non-positive values are allowed also.
647 /// <returns>An integer value giving the number of days remaining in
648 /// the Gregorian year.
650 public static int days_remaining(int day, int month, int year) {
651 return date_difference(day, month, year,
652 31, (int)Month.december, year);
655 // Helper functions for the Gregorian calendars.
658 /// Adds months to the given date.
660 /// <param name="time">The
661 /// <see cref="T:System.DateTime"/> to which to add
664 /// <param name="months">The number of months to add.</param>
665 /// <returns>A new <see cref="T:System.DateTime"/> value, that
666 /// results from adding <paramref name="months"/> to the specified
667 /// DateTime.</returns>
668 public static System.DateTime AddMonths(System.DateTime time,
671 int rd = CCFixed.FromDateTime(time);
672 int day, month, year;
673 dmy_from_fixed(out day, out month, out year, rd);
675 rd = fixed_from_dmy(day, month, year);
676 System.DateTime t = CCFixed.ToDateTime(rd);
677 return t.Add(time.TimeOfDay);
681 /// Adds years to the given date.
683 /// <param name="time">The
684 /// <see cref="T:System.DateTime"/> to which to add
687 /// <param name="years">The number of years to add.</param>
688 /// <returns>A new <see cref="T:System.DateTime"/> value, that
689 /// results from adding <paramref name="years"/> to the specified
690 /// DateTime.</returns>
691 public static System.DateTime AddYears(System.DateTime time,
694 int rd = CCFixed.FromDateTime(time);
695 int day, month, year;
696 dmy_from_fixed(out day, out month, out year, rd);
698 rd = fixed_from_dmy(day, month, year);
699 System.DateTime t = CCFixed.ToDateTime(rd);
700 return t.Add(time.TimeOfDay);
704 /// Gets the of the month from <paramref name="time"/>.
706 /// <param name="time">The
707 /// <see cref="T:System.DateTime"/> that specifies a
710 /// <returns>An integer giving the day of months, starting with 1.
712 public static int GetDayOfMonth(System.DateTime time) {
713 return day_from_fixed(CCFixed.FromDateTime(time));
717 /// The method gives the number of the day in the year.
719 /// <param name="time">The
720 /// <see cref="T:System.DateTime"/> that specifies a
723 /// <returns>An integer representing the day of the year,
724 /// starting with 1.</returns>
725 public static int GetDayOfYear(System.DateTime time) {
726 int rd = CCFixed.FromDateTime(time);
727 int year = year_from_fixed(rd);
728 int rd1_1 = fixed_from_dmy(1, 1, year);
729 return rd - rd1_1 + 1;
733 /// A method that gives the number of days of the specified
734 /// month of the <paramref name="year"/>.
736 /// <param name="year">An integer that gives the year in the current
738 /// <param name="month">An integer that gives the month, starting
740 /// <returns>An integer that gives the number of days of the
741 /// specified month.</returns>
742 public static int GetDaysInMonth(int year, int month) {
743 int rd1 = fixed_from_dmy(1, month, year);
744 int rd2 = fixed_from_dmy(1, month+1, year);
749 /// The method gives the number of days in the specified year.
751 /// <param name="year">An integer that gives the year.
753 /// <returns>An integer that gives the number of days of the
754 /// specified year.</returns>
755 public static int GetDaysInYear(int year) {
756 int rd1 = fixed_from_dmy(1, 1, year);
757 int rd2 = fixed_from_dmy(1, 1, year+1);
762 /// The method gives the number of the month of the specified
765 /// <param name="time">The
766 /// <see cref="T:System.DateTime"/> that specifies a
769 /// <returns>An integer representing the month,
770 /// starting with 1.</returns>
771 public static int GetMonth(System.DateTime time) {
772 return month_from_fixed(CCFixed.FromDateTime(time));
776 /// The method gives the number of the year of the specified
779 /// <param name="time">The
780 /// <see cref="T:System.DateTime"/> that specifies a
783 /// <returns>An integer representing the year.
785 public static int GetYear(System.DateTime time) {
786 return year_from_fixed(CCFixed.FromDateTime(time));
790 /// A virtual method that tells whether the given day
793 /// <param name="year">An integer that specifies the year.
795 /// <param name="month">An integer that specifies the month.
797 /// <param name="day">An integer that specifies the day.
799 /// <returns>A boolean that tells whether the given day is a leap
802 public static bool IsLeapDay(int year, int month, int day) {
803 return is_leap_year(year) && month == 2 && day == 29;
807 /// A method that creates the
808 /// <see cref="T:System.DateTime"/> from the parameters.
810 /// <param name="year">An integer that gives the year
812 /// <param name="month">An integer that specifies the month.
814 /// <param name="day">An integer that specifies the day.
816 /// <param name="hour">An integer that specifies the hour.
818 /// <param name="minute">An integer that specifies the minute.
820 /// <param name="second">An integer that gives the second.
822 /// <param name="milliseconds">An integer that gives the
826 /// <see cref="T:system.DateTime"/> representig the date and time.
828 public static System.DateTime ToDateTime(int year, int month, int day,
829 int hour, int minute, int second, int milliseconds)
831 return CCFixed.ToDateTime(fixed_from_dmy(day, month, year),
832 hour, minute, second, milliseconds);
834 } // class CCGregorianCalendar
837 /// A class encapsulating the functions of the Julian calendar as static
841 /// <para>The algorithms don't support a year 0. Years before Common Era
842 /// (B.C.E. or B.C.) are negative and years of Common Era (C.E. or A.D.)
846 /// This class is not compatible to
847 /// <see cref="T:System.Globalization.JulianCalendar"/>.
849 /// <seealso cref="T:CCFixed"/>
851 internal class CCJulianCalendar {
852 /// <summary>An integer defining the epoch of the Julian calendar
853 /// as fixed day number.</summary>
854 /// <remarks>The epoch is December 30, 0 (Gregorian).</remarks>
855 const int epoch = -1; // 30. 12. 0 Gregorian
857 /// <summary>The enumeration defines the months of the Julian
912 /// The method tells whether the year is a leap year.
914 /// <param name="year">An integer representing the Julian year.
916 /// <returns>A boolean which is true if <paramref name="year"/> is
919 public static bool is_leap_year(int year) {
920 return CCMath.mod(year, 4) == (year > 0 ? 0 : 3);
924 /// The method returns the fixed day number of the given Julian
927 /// <param name="day">An integer representing the day of the month,
930 /// <param name="month">An integer representing the month in the
933 /// <param name="year">An integer representing the Julian year.
934 /// Positive and Negative values are allowed.
936 /// <returns>An integer value representing the fixed day number.
938 public static int fixed_from_dmy(int day, int month, int year) {
939 int y = year < 0 ? year+1 : year;
942 k += CCMath.div(y-1, 4);
943 k += CCMath.div(367*month-362, 12);
945 k += is_leap_year(year) ? -1 : -2;
953 /// The method computes the Julian year from a fixed day number.
955 /// <param name="date">The fixed day number.
957 /// <returns>An integer value giving the Julian year of the date.
959 public static int year_from_fixed(int date) {
960 int approx = CCMath.div(4*(date-epoch)+1464, 1461);
961 return approx <= 0 ? approx - 1 : approx;
965 /// The method computes the Julian year and month from a fixed day
968 /// <param name="month">The output value giving the Julian month.
970 /// <param name="year">The output value giving the Julian year.
972 /// <param name="date">An integer value specifying the fixed day
974 public static void my_from_fixed(out int month, out int year, int date)
976 year = year_from_fixed(date);
978 int prior_days = date - fixed_from_dmy(1, (int)Month.january,
982 if (date < fixed_from_dmy(1, (int)Month.march, year)) {
984 } else if (is_leap_year(year)) {
990 month = CCMath.div(12 * (prior_days + correction) + 373, 367);
995 /// The method computes the Julian year, month, and day from a
996 /// fixed day number.
998 /// <param name="day">The output value returning the day of the
1001 /// <param name="month">The output value giving the Julian month.
1003 /// <param name="year">The output value giving the Julian year.
1005 /// <param name="date">An integer value specifying the fixed day
1007 public static void dmy_from_fixed(out int day, out int month,
1008 out int year, int date)
1010 my_from_fixed(out month, out year, date);
1011 day = date - fixed_from_dmy(1, month, year) + 1;
1014 /// <summary>A method computing the Julian month from a fixed
1017 /// <param name="date">An integer specifying the fixed day number.
1019 /// <returns>An integer value representing the Julian month.
1021 public static int month_from_fixed(int date) {
1024 my_from_fixed(out month, out year, date);
1029 /// A method computing the day of the month from a fixed day number.
1031 /// <param name="date">An integer specifying the fixed day number.
1033 /// <returns>An integer value representing the day of the month.
1035 public static int day_from_fixed(int date) {
1040 dmy_from_fixed(out day, out month, out year, date);
1045 /// The method computes the difference between two Julian dates.
1047 /// <param name="dayA">The integer parameter gives the day of month
1048 /// of the first date.
1050 /// <param name="monthA">The integer parameter gives the Julian
1051 /// month of the first date.
1053 /// <param name="yearA">The integer parameter gives the Julian
1054 /// year of the first date.
1056 /// <param name="dayB">The integer parameter gives the day of month
1057 /// of the second date.
1059 /// <param name="monthB">The integer parameter gives the Julian
1060 /// month of the second date.
1062 /// <param name="yearB">The integer parameter gives the Julian
1063 /// year of the second date.
1065 /// <returns>An integer giving the difference of days from the first
1066 /// the second date.
1068 public static int date_difference(int dayA, int monthA, int yearA,
1069 int dayB, int monthB, int yearB)
1071 return fixed_from_dmy(dayB, monthB, yearB) -
1072 fixed_from_dmy(dayA, monthA, yearA);
1076 /// The method computes the number of the day in the year from
1079 /// <param name="day">An integer representing the day of the month,
1080 /// counting from 1.
1082 /// <param name="month">An integer representing the month in the
1085 /// <param name="year">An integer representing the Julian year.
1086 /// Negative values are allowed also.
1088 /// <returns>An integer value giving the number of the day in the
1089 /// Julian year, counting from 1.
1091 public static int day_number(int day, int month, int year) {
1092 return date_difference(31, (int)Month.december, year-1,
1097 /// The method computes the days remaining in the given Julian
1098 /// year from a Julian date.
1100 /// <param name="day">An integer representing the day of the month,
1101 /// counting from 1.
1103 /// <param name="month">An integer representing the month in the
1106 /// <param name="year">An integer representing the Julian year.
1107 /// Negative values are allowed also.
1109 /// <returns>An integer value giving the number of days remaining in
1110 /// the Julian year.
1112 public static int days_remaining(int day, int month, int year) {
1113 return date_difference(day, month, year,
1114 31, (int)Month.december, year);
1116 } // class CCJulianCalendar
1119 /// A class encapsulating the functions of the Hebrew calendar as static
1124 /// This class is not compatible to
1125 /// <see cref="T:System.Globalization.HebrewCalendar"/>.
1127 /// <seealso cref="T:CCFixed"/>
1129 internal class CCHebrewCalendar {
1130 /// <summary>An integer defining the epoch of the Hebrew calendar
1131 /// as fixed day number.</summary>
1132 /// <remarks>The epoch is October 10, 3761 B.C.E. (Julian).</remarks>
1133 const int epoch = -1373427;
1135 /// <summary>The enumeration defines the months of the Gregorian
1139 /// The enumaration differs from .NET which defines Tishri as month 1.
1191 /// Adar I. Only in years with Adar II.
1195 /// Adar II. Only in years wirh Adar I.
1201 /// The method tells whether the year is a leap year.
1203 /// <param name="year">An integer representing the Hebrew year.
1205 /// <returns>A boolean which is true if <paramref name="year"/> is
1208 public static bool is_leap_year(int year) {
1209 return CCMath.mod(7*year+1, 19) < 7;
1213 /// The Method gives the number of the last month in a year, which
1214 /// is equal with the number of month in a Hebrew year.
1216 /// <param name="year">An integer representing the Hebrew year.
1218 /// <returns>An integer giving the number of the last month of the
1219 /// Hebrew year, which is the same as the numbers of month in the
1222 public static int last_month_of_year(int year) {
1223 return is_leap_year(year) ? 13 : 12;
1227 /// <summary>The method is a helper function.</summary>
1228 /// <param name="year">An integer specifying the Hebrew year.
1230 /// <returns>An integer representing the number of elapsed days
1231 /// until the Hebrew year.</returns>
1232 public static int elapsed_days(int year) {
1233 int months_elapsed = CCMath.div(235*year-234, 19);
1235 int d = CCMath.div_mod(out r, months_elapsed, 1080);
1236 int parts_elapsed = 204 + 793 * r;
1237 int hours_elapsed = 11 + 12 * months_elapsed +
1238 793 * d + CCMath.div(parts_elapsed, 1080);
1240 int day = 29*months_elapsed + CCMath.div(hours_elapsed, 24);
1242 if (CCMath.mod(3*(day+1), 7) < 3) {
1249 /// <summary>A method computing the delay of new year for the given
1252 /// <param name="year">An integer that gives the Hebrew year.
1254 /// <returns>The new year delay in days of the given Hebrew year.
1256 public static int new_year_delay(int year) {
1257 int ny1 = elapsed_days(year);
1258 int ny2 = elapsed_days(year+1);
1260 if (ny2 - ny1 == 356) {
1263 int ny0 = elapsed_days(year-1);
1264 if (ny1 - ny0 == 382) {
1271 /// The method computes the last day of month (nummer of days in a
1272 /// month) of the given Hebrew year.
1274 /// <param name="month">The Hebrew month, allowed value between
1275 /// One and Thirteen.
1277 /// <param name="year">An integer that gives the Hebrew year.
1279 /// <returns>The number of the last day of the month of the given
1280 /// Hebrew year, which gives automatically the number of days in the
1283 /// <exception cref="T:System.ArgumentOutOfRange.Exception">
1284 /// The exception is thrown if month not between One and Thirteen.
1286 public static int last_day_of_month(int month, int year) {
1287 if (month < 1 || month > 13)
1288 throw new System.ArgumentOutOfRangeException("month",
1289 "Month should be between One and Thirteen.");
1295 if (!long_heshvan(year))
1299 if (short_kislev(year))
1304 if (!is_leap_year(year))
1313 /// The functions checks whether the month Heshvan is a long one
1314 /// in the given Hebrew year.
1316 /// <param name="year">An integer that gives the Hebrew year.
1318 /// <returns>A boolean value: true if there is a long Heshvan
1319 /// in the given Hebrew year; false otherwise.
1321 public static bool long_heshvan(int year) {
1322 return CCMath.mod(days_in_year(year), 10) == 5;
1326 /// The functions checks whether the month Kislev is a short one
1327 /// in the given Hebrew year.
1329 /// <param name="year">An integer that gives the Hebrew year.
1331 /// <returns>A boolean value: true if there is a short Kislev
1332 /// in the given Hebrew year; false otherwise.
1334 public static bool short_kislev(int year) {
1335 return CCMath.mod(days_in_year(year), 10) == 3;
1339 /// The functions gives the number of days in the specified Hebrew
1342 /// <param name="year">An integer that gives the Hebrew year.
1344 /// <returns>The days of the Hebrew year as integer.
1346 public static int days_in_year(int year) {
1347 return fixed_from_dmy(1, 7, year+1) -
1348 fixed_from_dmy(1, 7, year);
1352 /// The method returns the fixed day number of the given Hebrew
1355 /// <param name="day">An integer representing the day of the month,
1356 /// counting from 1.
1358 /// <param name="month">An integer representing the month in the
1361 /// <param name="year">An integer representing the Hebrew year.
1362 /// Non-positive values are allowed also.
1364 /// <returns>An integer value representing the fixed day number.
1366 public static int fixed_from_dmy(int day, int month, int year) {
1369 k += elapsed_days(year);
1370 k += new_year_delay(year);
1373 int l = last_month_of_year(year);
1374 for (m = 7; m <= l; m++) {
1375 k += last_day_of_month(m, year);
1377 for (m = 1; m < month; m++) {
1378 k += last_day_of_month(m, year);
1382 for (m = 7; m < month; m++) {
1383 k += last_day_of_month(m, year);
1393 /// The method computes the Hebrew year from a fixed day number.
1395 /// <param name="date">The fixed day number.
1397 /// <returns>An integer value giving the Hebrew year of the date.
1399 public static int year_from_fixed(int date) {
1400 int approx = (int)System.Math.Floor(
1401 ((double)(date - epoch))/(35975351.0/98496.0));
1403 for (y = approx; date >= fixed_from_dmy(1, 7, y); y++) {}
1408 /// The method computes the Hebrew year and month from a fixed day
1411 /// <param name="month">The output value giving the Hebrew month.
1413 /// <param name="year">The output value giving the Hebrew year.
1415 /// <param name="date">An integer value specifying the fixed day
1417 public static void my_from_fixed(out int month, out int year,
1420 year = year_from_fixed(date);
1422 int start = date < fixed_from_dmy(1, 1, year) ? 7 : 1;
1425 date > fixed_from_dmy(last_day_of_month(month, year),
1432 /// The method computes the Hebrew year, month, and day from a
1433 /// fixed day number.
1435 /// <param name="day">The output value returning the day of the
1438 /// <param name="month">The output value giving the Hebrew month.
1440 /// <param name="year">The output value giving the Hebrew year.
1442 /// <param name="date">An integer value specifying the fixed day
1444 public static void dmy_from_fixed(out int day, out int month,
1445 out int year, int date)
1447 my_from_fixed(out month, out year, date);
1448 day = date - fixed_from_dmy(1, month, year) + 1;
1451 /// <summary>A method computing the Hebrew month from a fixed
1454 /// <param name="date">An integer specifying the fixed day number.
1456 /// <returns>An integer value representing the Hebrew month.
1458 public static int month_from_fixed(int date) {
1461 my_from_fixed(out month, out year, date);
1466 /// A method computing the day of the month from a fixed day number.
1468 /// <param name="date">An integer specifying the fixed day number.
1470 /// <returns>An integer value representing the day of the month.
1472 public static int day_from_fixed(int date) {
1473 int day, month, year;
1475 dmy_from_fixed(out day, out month, out year, date);
1480 /// The method computes the difference between two Hebrew dates.
1482 /// <param name="dayA">The integer parameter gives the day of month
1483 /// of the first date.
1485 /// <param name="monthA">The integer parameter gives the Hebrew
1486 /// month of the first date.
1488 /// <param name="yearA">The integer parameter gives the Hebrew
1489 /// year of the first date.
1491 /// <param name="dayB">The integer parameter gives the day of month
1492 /// of the second date.
1494 /// <param name="monthB">The integer parameter gives the Hebrew
1495 /// month of the second date.
1497 /// <param name="yearB">The integer parameter gives the Hebrew
1498 /// year of the second date.
1500 /// <returns>An integer giving the difference of days from the first
1501 /// the second date.
1503 public static int date_difference(int dayA, int monthA, int yearA,
1504 int dayB, int monthB, int yearB)
1506 return fixed_from_dmy(dayB, monthB, yearB) -
1507 fixed_from_dmy(dayA, monthA, yearA);
1511 /// The method computes the number of the day in the year from
1514 /// <param name="day">An integer representing the day of the month,
1515 /// counting from 1.
1517 /// <param name="month">An integer representing the month in the
1520 /// <param name="year">An integer representing the Hebrew year.
1522 /// <returns>An integer value giving the number of the day in the
1523 /// Hebrew year, counting from 1.
1525 public static int day_number(int day, int month, int year) {
1526 return date_difference(1, 7, year,
1527 day, month, year) + 1;
1531 /// The method computes the days remaining in the given Hebrew
1532 /// year from a Hebrew date.
1534 /// <param name="day">An integer representing the day of the month,
1535 /// counting from 1.
1537 /// <param name="month">An integer representing the month in the
1540 /// <param name="year">An integer representing the Hebrew year.
1542 /// <returns>An integer value giving the number of days remaining in
1543 /// the Hebrew year.
1545 public static int days_remaining(int day, int month, int year) {
1546 return date_difference(day, month, year,
1549 } // class HebrewCalendar
1553 /// A class encapsulating the functions of the Islamic calendar as static
1557 /// <para>There is no difference here in using Hijri or Islamic calendar.
1559 /// <para>The epoch of the Islamic calendar isn't fixed, because we cannot
1560 /// surely say today, when the crescent of the new moon has been observed
1561 /// around the July 16, 622 C.E. Julian. Even today the start and end of
1562 /// the month Ramadan is defined by religous authorities. So the calendar
1563 /// can be offset by two days.
1566 /// We don't support the offset here, however we changed the epoch from
1567 /// "Calendrical Calculations" to value, that .Net seems to be using.
1570 /// This class is not compatible to
1571 /// <see cref="T:System.Globalization.HijriCalendar"/>.
1573 /// <seealso cref="T:CCFixed"/>
1575 internal class CCHijriCalendar {
1576 /// <summary>An integer defining the epoch of the Gregorian calendar
1577 /// as fixed day number.</summary>
1580 /// The epoch is given as 16 July 622 C.E. Julian (R.D. 227015)
1581 /// in Calendrical Calculations, the approximate date of
1582 /// the emigration of
1583 /// Muhammed to Medina. However there is no way to determine today
1584 /// the observation of the crescent of the new moon in July 622 C.E.
1585 /// (Julian). So there is some variability in the epoch.
1586 /// Religous authorities determine the epoch by observing the
1587 /// crescent of the new moon for the month Ramadan, so there might
1588 /// be an offsets by two days of the epoch.
1591 /// supports an AddHijriDate parameter in the registry to adapt
1592 /// for it. It seems that the .NET implementation of
1593 /// HijriCalendar uses an epoch of 227014, so we use it here. The
1594 /// ArgumentOutOfRangeException gives July, 18 622 as epoch,
1595 /// which is 227014 supporting our theory.
1598 const int epoch = 227014;
1600 /// <summary>The enumeration defines the months of the Islamic
1655 /// The method tells whether the year is a leap year.
1657 /// <param name="year">An integer representing the Islamic year.
1659 /// <returns>A boolean which is true if <paramref name="year"/> is
1662 public static bool is_leap_year(int year) {
1663 return CCMath.mod(14+11*year, 30) < 11;
1667 /// The method returns the fixed day number of the given Islamic
1670 /// <param name="day">An integer representing the day of the month,
1671 /// counting from 1.
1673 /// <param name="month">An integer representing the month in the
1676 /// <param name="year">An integer representing the Islamic year.
1677 /// Non-positive values are allowed also.
1679 /// <returns>An integer value representing the fixed day number.
1681 public static int fixed_from_dmy(int day, int month, int year) {
1683 k += 354 * (year-1);
1684 k += CCMath.div(3+11*year, 30);
1685 k += (int)System.Math.Ceiling(29.5 * (double)(month-1));
1692 /// The method computes the Islamic year from a fixed day number.
1694 /// <param name="date">The fixed day number.
1696 /// <returns>An integer value giving the Islamic year of the date.
1698 public static int year_from_fixed(int date) {
1699 return CCMath.div(30*(date-epoch)+10646, 10631);
1703 /// The method computes the Islamic year and month from a fixed day
1706 /// <param name="month">The output value giving the Islamic month.
1708 /// <param name="year">The output value giving the Islamic year.
1710 /// <param name="date">An integer value specifying the fixed day
1712 public static void my_from_fixed(out int month, out int year, int date)
1714 year = year_from_fixed(date);
1716 int m = 1+(int)System.Math.Ceiling(
1717 ((double)(date-29-fixed_from_dmy(1,1,year)))/29.5);
1719 month = m < 12 ? m : 12;
1723 /// The method computes the Islamic year, month, and day from a
1724 /// fixed day number.
1726 /// <param name="day">The output value returning the day of the
1729 /// <param name="month">The output value giving the Islamic month.
1731 /// <param name="year">The output value giving the Islamic year.
1733 /// <param name="date">An integer value specifying the fixed day
1735 public static void dmy_from_fixed(out int day, out int month,
1736 out int year, int date)
1738 my_from_fixed(out month, out year, date);
1739 day = date - fixed_from_dmy(1, month, year) + 1;
1742 /// <summary>A method computing the Islamic month from a fixed
1745 /// <param name="date">An integer specifying the fixed day number.
1747 /// <returns>An integer value representing the Islamic month.
1749 public static int month_from_fixed(int date) {
1752 my_from_fixed(out month, out year, date);
1757 /// A method computing the day of the month from a fixed day number.
1759 /// <param name="date">An integer specifying the fixed day number.
1761 /// <returns>An integer value representing the day of the month.
1763 public static int day_from_fixed(int date) {
1768 dmy_from_fixed(out day, out month, out year, date);
1773 /// The method computes the difference between two Islamic dates.
1775 /// <param name="dayA">The integer parameter gives the day of month
1776 /// of the first date.
1778 /// <param name="monthA">The integer parameter gives the Islamic
1779 /// month of the first date.
1781 /// <param name="yearA">The integer parameter gives the Islamic
1782 /// year of the first date.
1784 /// <param name="dayB">The integer parameter gives the day of month
1785 /// of the second date.
1787 /// <param name="monthB">The integer parameter gives the Islamic
1788 /// month of the second date.
1790 /// <param name="yearB">The integer parameter gives the Islamic
1791 /// year of the second date.
1793 /// <returns>An integer giving the difference of days from the first
1794 /// the second date.
1796 public static int date_difference(int dayA, int monthA, int yearA,
1797 int dayB, int monthB, int yearB)
1799 return fixed_from_dmy(dayB, monthB, yearB) -
1800 fixed_from_dmy(dayA, monthA, yearA);
1804 /// The method computes the number of the day in the year from
1807 /// <param name="day">An integer representing the day of the month,
1808 /// counting from 1.
1810 /// <param name="month">An integer representing the month in the
1813 /// <param name="year">An integer representing the Islamic year.
1815 /// <returns>An integer value giving the number of the day in the
1816 /// Islamic year, counting from 1.
1818 public static int day_number(int day, int month, int year) {
1819 return date_difference(31, 12, year-1, day, month, year);
1823 /// The method computes the days remaining in the given Islamic
1824 /// year from a Islamic date.
1826 /// <param name="day">An integer representing the day of the month,
1827 /// counting from 1.
1829 /// <param name="month">An integer representing the month in the
1832 /// <param name="year">An integer representing the Islamic year.
1833 /// Non-positive values are allowed also.
1835 /// <returns>An integer value giving the number of days remaining in
1836 /// the Islamic year.
1838 public static int days_remaining(int day, int month, int year) {
1839 return date_difference(day, month, year,31, 12, year);
1841 } // class CCHijriCalendar
1844 /// A class that supports the Gregorian based calendars with other eras
1845 /// (e.g. <see cref="T:System.Gloablization.JapaneseCalendar"/>).
1847 [System.Serializable]
1848 internal class CCGregorianEraHandler {
1850 /// A struct that represents a single era.
1852 [System.Serializable]
1855 /// The integer number identifying the era.
1860 /// A get-only property that gives the era integer number.
1862 public int Nr { get { return _nr; } }
1864 /// <summary>This integer gives the first day of the era as
1865 /// fixed day number.
1867 private int _start; // inclusive
1869 /// This integer gives the gregorian year of the
1870 /// <see cref="M:_start"/> value.
1872 private int _gregorianYearStart;
1874 /// This integer gives the last day of the era as fixed day
1877 private int _end; // inclusive
1879 /// This integer gives the largest year number of this era.
1881 private int _maxYear;
1884 /// This constructor creates the era structure.
1886 /// <param name="nr">The integer number of the era.
1888 /// <param name="start">The fixed day number defining the
1889 /// first day of the era.
1891 /// <param name="end">The fixed day number that defines the
1892 /// last day of the era.
1894 public Era(int nr, int start, int end) {
1896 throw new System.ArgumentException(
1897 "Era number shouldn't be zero.");
1900 throw new System.ArgumentException(
1901 "Era should start before end.");
1906 _gregorianYearStart =
1907 CCGregorianCalendar.year_from_fixed(_start);
1908 int gregorianYearEnd =
1909 CCGregorianCalendar.year_from_fixed(_end);
1910 _maxYear = gregorianYearEnd - _gregorianYearStart + 1;
1914 /// This method computes the Gregorian year from the year
1917 /// <param name="year">An integer giving the year in the
1921 /// The Gregorian year as integer.
1923 /// <exception cref="T:System.ArgumentOutOfRangeException">
1924 /// The exception is thrown if the year isn't valid in this
1927 public int GregorianYear(int year) {
1928 if (year < 1 || year > _maxYear) {
1929 System.IO.StringWriter sw =
1930 new System.IO.StringWriter();
1932 "Valid Values are between " +
1933 "{0} and {1}, inclusive.",
1935 throw new System.ArgumentOutOfRangeException(
1936 "year", sw.ToString());
1938 return year + _gregorianYearStart - 1;
1942 /// This function checks wether the given fixed day number is
1943 /// ion the time span of the era.
1945 /// <param name="date">An integer giving the fixed day
1948 /// <returns>A boolean: true if the argument is in the time
1949 /// span of the era.
1951 public bool Covers(int date) {
1952 return _start <= date && date <= _end;
1956 /// This function returns the year of the era and sets
1957 /// the era in an output parameter.
1959 /// <param name="era">An output parameter returning the
1962 /// <param name="date">An integer giving the fixed day
1965 /// <returns>An integer giving the year of the era.
1967 /// <exception cref="T:System.ArgumentOutOfRangeException">
1968 /// The exception is thrown if date is outside of the time
1969 /// span of the era.
1971 public int EraYear(out int era, int date) {
1973 throw new System.ArgumentOutOfRangeException(
1975 "Time was out of Era range.");
1977 CCGregorianCalendar.year_from_fixed(date);
1979 return gregorianYear - _gregorianYearStart + 1;
1984 /// A private member storing the eras in a
1985 /// <see cref="T:System.Collections.SortedList"/>.
1987 private SortedList _Eras;
1990 /// The property returns the era numbers as an array of integers.
1994 int[] a = new int[_Eras.Count];
1996 for (int i = 0; i < _Eras.Count; i++) {
1997 Era e = (Era)_Eras.GetByIndex(i);
2008 public CCGregorianEraHandler() {
2009 _Eras = new SortedList();
2013 /// Method adds an era to the GregorianEraHandler instance.
2015 /// <param name="nr">The integer number of the era.
2017 /// <param name="rd_start">The fixed day number defining the
2018 /// first day of the era.
2020 /// <param name="rd_end">The fixed day number that defines the
2021 /// last day of the era.
2023 public void appendEra(int nr, int rd_start, int rd_end) {
2024 Era era = new Era(nr, rd_start, rd_end);
2025 _Eras[(System.Object)nr] = era;
2028 /// Method adds a yet not-ended era to the GregorianEraHandler
2031 /// <param name="nr">The integer number of the era.
2033 /// <param name="rd_start">The fixed day number defining the
2034 /// first day of the era.
2036 public void appendEra(int nr, int rd_start) {
2037 appendEra(nr, rd_start,
2038 CCFixed.FromDateTime(DateTime.MaxValue));
2042 /// This method computes the Gregorian year from the year
2043 /// of the given era.
2045 /// <param name="year">An integer giving the year in the
2048 /// <param name="era">An integer giving the era number.
2051 /// The Gregorian year as integer.
2053 /// <exception cref="T:System.ArgumentOutOfRangeException">
2054 /// The exception is thrown if the year isn't valid in this
2057 public int GregorianYear(int year, int era) {
2058 Era e = (Era)_Eras[(System.Object)era];
2059 return e.GregorianYear(year);
2063 /// This function returns the year of the era and sets
2064 /// the era in an output parameter.
2066 /// <param name="era">An output parameter returning the
2069 /// <param name="date">An integer giving the fixed day
2072 /// <returns>An integer giving the year of the era.
2074 /// <exception cref="T:System.ArgumentOutOfRangeException">
2075 /// The exception is thrown if the fixed day number is outside of the
2076 /// time spans of all eras.
2078 public int EraYear(out int era, int date)
2080 IList list = _Eras.GetValueList();
2082 foreach (Era e in list) {
2084 return e.EraYear(out era, date);
2087 throw new System.ArgumentOutOfRangeException("date",
2088 "Time value was out of era range.");
2092 /// The method checks whether a given
2093 /// <see cref="T:System.DateTime"/> is covered by any era.
2095 /// <param name="time">A
2096 /// <see cref="T:System.DateTime"/> giving the date and time.
2098 /// <exception cref="T:System.ArgumentOutOfRangeException">
2099 /// The exception is thrown if the argument isn't inside the time
2100 /// span of any era.
2102 public void CheckDateTime(System.DateTime time) {
2103 int date = CCFixed.FromDateTime(time);
2105 if (!ValidDate(date))
2106 throw new System.ArgumentOutOfRangeException("time",
2107 "Time value was out of era range.");
2111 /// The method tests whether a given
2112 /// fixed day number is covered by any era.
2114 /// <param name="date">An integer representing the fixed day number.
2116 /// <returns> A boolean is returned: true if the argument is inside
2117 /// the time span of one era; false otherwise.
2119 public bool ValidDate(int date) {
2120 IList list = _Eras.GetValueList();
2122 foreach (Era e in list) {
2131 /// The method tests, whether the era number does exist.
2133 /// <param name="era">An integer giving the era number.
2135 /// <returns>A boole value: True if the era number does exist;
2136 /// false otherwise.
2138 public bool ValidEra(int era) {
2139 return _Eras.Contains((System.Object)era);
2141 } // class CCGregorianEraHandler
2143 } // namespace System.Globalization