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
{
34 /// This is the Julian calendar.
37 /// <para>The Julian calendar supports only the Common Era from
38 /// January 1, 1 (Gregorian) to December 31, 9999 (Gregorian).
40 /// <para>The implementation uses the
41 /// <see cref="N:CalendricalCalculations"/> namespace.
45 [MonoTODO ("Serialization format not compatible with .NET")]
46 public class JulianCalendar
: Calendar
{
48 /// Default constructor.
50 public JulianCalendar() {
51 M_AbbrEraNames
= new string[] {"C.E."}
;
52 M_EraNames
= new string[] {"Common Era"}
;
53 if (twoDigitYearMax
== 99)
54 twoDigitYearMax
= 2029;
58 /// The era number for the Common Era (C.E.) or Anno Domini (A.D.)
61 public static readonly int JulianEra
= 1;
63 /// <value>Overridden. Gives the eras supported by the Julian
64 /// calendar as an array of integers.
66 public override int[] Eras
{
68 return new int[] { JulianEra }
;
72 int twoDigitYearMax
= 2029;
74 public override int TwoDigitYearMax
77 return twoDigitYearMax
;
81 M_ArgumentInRange ("value", value, 100, M_MaxYear
);
83 twoDigitYearMax
= value;
88 /// A protected method checking the era number.
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"/>.
95 internal void M_CheckEra(ref int era
) {
96 if (era
== CurrentEra
)
99 throw new ArgumentException("Era value was not valid.");
103 /// A protected method checking calendar year and the era number.
105 /// <param name="year">An integer representing the calendar year.
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"/>.
112 /// <exception cref="T:System.ArgumentOutOfRangeException">
113 /// The exception is thrown if the calendar year is outside of
114 /// the allowed range.
116 internal override void M_CheckYE(int year
, ref int era
) {
118 M_ArgumentInRange("year", year
, 1, 9999);
122 /// A protected method checking the calendar year, month, and
125 /// <param name="year">An integer representing the calendar year.
127 /// <param name="month">An integer giving the calendar month.
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"/>.
134 /// <exception cref="T:System.ArgumentOutOfRangeException">
135 /// The exception is thrown if the calendar year or month is
136 /// outside of the allowed range.
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.");
146 /// A protected method checking the calendar day, month, and year
147 /// and the era number.
149 /// <param name="year">An integer representing the calendar year.
151 /// <param name="month">An integer giving the calendar month.
153 /// <param name="day">An integer giving the calendar day.
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"/>.
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.
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.");
175 /// Overridden. Adds months to a given date.
177 /// <param name="time">The
178 /// <see cref="T:System.DateTime"/> to which to add
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
);
191 rd
= CCJulianCalendar
.fixed_from_dmy(day
, month
, year
);
192 DateTime t
= CCFixed
.ToDateTime(rd
);
193 return t
.Add(time
.TimeOfDay
);
197 /// Overridden. Adds years to a given date.
199 /// <param name="time">The
200 /// <see cref="T:System.DateTime"/> to which to add
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
);
213 rd
= CCJulianCalendar
.fixed_from_dmy(day
, month
, year
);
214 DateTime t
= CCFixed
.ToDateTime(rd
);
215 return t
.Add(time
.TimeOfDay
);
219 /// Overridden. Gets the day of the month from
220 /// <paramref name="time"/>.
222 /// <param name="time">The
223 /// <see cref="T:System.DateTime"/> that specifies a
226 /// <returns>An integer giving the day of months, starting with 1.
228 public override int GetDayOfMonth(DateTime time
) {
229 int rd
= CCFixed
.FromDateTime(time
);
230 return CCJulianCalendar
.day_from_fixed(rd
);
234 /// Overridden. Gets the day of the week from the specified date.
236 /// <param name="time">The
237 /// <see cref="T:System.DateTime"/> that specifies a
240 /// <returns>An integer giving the day of months, starting with 1.
242 public override DayOfWeek
GetDayOfWeek(DateTime time
) {
243 int rd
= CCFixed
.FromDateTime(time
);
244 return (DayOfWeek
)CCFixed
.day_of_week(rd
);
248 /// Overridden. Gives the number of the day in the year.
250 /// <param name="time">The
251 /// <see cref="T:System.DateTime"/> that specifies a
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;
264 /// Overridden. Gives the number of days in the specified month
265 /// of the given year and era.
267 /// <param name="year">An integer that gives the year.
269 /// <param name="month">An integer that gives the month, starting
271 /// <param name="era">An intger that gives the era of the specified
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.
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
);
288 /// Overridden. Gives the number of days of the specified
289 /// year of the given era.
291 /// <param name="year">An integer that specifies the year.
293 /// <param name="era">An ineger that specifies the era.
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.
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);
310 /// Overridden. Gives the era of the specified date.
312 /// <param name="time">The
313 /// <see cref="T:System.DateTime"/> that specifies a
316 /// <returns>An integer representing the era of the calendar.
318 public override int GetEra(DateTime time
) {
319 // should change, if more than one era is supported
324 /// Overridden. Gives the number of the month of the specified
327 /// <param name="time">The
328 /// <see cref="T:System.DateTime"/> that specifies a
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
);
339 /// Overridden. Gives the number of months in the specified year
342 /// <param name="year">An integer that specifies the year.
344 /// <param name="era">An integer that specifies the era.
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.
351 public override int GetMonthsInYear(int year
, int era
) {
352 M_CheckYE(year
, ref era
);
357 /// Overridden. Gives the number of the year of the specified
360 /// <param name="time">The
361 /// <see cref="T:System.DateTime"/> that specifies a
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
);
372 /// Overridden. Tells whether the given day
375 /// <param name="year">An integer that specifies the year in the
378 /// <param name="month">An integer that specifies the month.
380 /// <param name="day">An integer that specifies the day.
382 /// <param name="era">An integer that specifies the era.
384 /// <returns>A boolean that tells whether the given day is a leap
387 /// <exception cref="T:System.ArgumentOutOfRangeException">
388 /// The exception is thrown, if the year, month, day, or era is not
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;
398 /// Overridden. Tells whether the given month
401 /// <param name="year">An integer that specifies the year in the
404 /// <param name="month">An integer that specifies the month.
406 /// <param name="era">An integer that specifies the era.
408 /// <returns>A boolean that tells whether the given month is a leap
411 /// <exception cref="T:System.ArgumentOutOfRangeException">
412 /// The exception is thrown, if the year, month, or era is not
415 public override bool IsLeapMonth(int year
, int month
, int era
) {
416 M_CheckYME(year
, month
, ref era
);
421 /// Overridden. Tells whether the given year
424 /// <param name="year">An integer that specifies the year in the
427 /// <param name="era">An integer that specifies the era.
429 /// <returns>A boolean that tells whether the given year is a leap
432 /// <exception cref="T:System.ArgumentOutOfRangeException">
433 /// The exception is thrown, if the year or era is not
436 public override bool IsLeapYear(int year
, int era
) {
437 M_CheckYE(year
, ref era
);
438 return CCJulianCalendar
.is_leap_year(year
);
442 /// Overridden. Creates the
443 /// <see cref="T:System.DateTime"/> from the parameters.
445 /// <param name="year">An integer that gives the year in the
446 /// <paramref name="era"/>.
448 /// <param name="month">An integer that specifies the month.
450 /// <param name="day">An integer that specifies the day.
452 /// <param name="hour">An integer that specifies the hour.
454 /// <param name="minute">An integer that specifies the minute.
456 /// <param name="second">An integer that gives the second.
458 /// <param name="milliseconds">An integer that gives the
461 /// <param name="era">An integer that specifies the era.
464 /// <see cref="T:system.DateTime"/> representig the date and time.
466 /// <exception cref="T:System.ArgumentOutOfRangeException">
467 /// The exception is thrown, if at least one of the parameters
470 public override DateTime
ToDateTime(int year
, int month
, int day
,
471 int hour
, int minute
, int second
, int milliseconds
,
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();
488 public override CalendarAlgorithmType AlgorithmType
{
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
{
503 public override DateTime MaxSupportedDateTime
{
510 } // class JulianCalendar
512 } // namespace System.Globalization