1 // System.Globalization.Calendar.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
{
33 using System
.Runtime
.InteropServices
;
36 /// The class serves as a base class for calendar classes.
40 public abstract class Calendar
: ICloneable
42 /// <value>An protected integer property that gives the number of
43 /// days in a week. It might be overridden.</value>
44 internal virtual int M_DaysInWeek
50 /// The protected method creates the string used in the
51 /// <see cref="T:System.ArgumentOutOfRangeException"/>
53 /// <param name="a">An object that represents the smallest
54 /// allowable value.</param>
55 /// <param name="b">An object that represents the greatest allowable
57 /// <returns>The string used in the
58 /// <see cref="T:System.ArgumentOutOfRangeException"/>
60 internal string M_ValidValues(object a
, object b
)
62 StringWriter sw
= new StringWriter();
63 sw
.Write("Valid values are between {0} and {1}, inclusive.",
69 /// The protected method checks wether the parameter
70 /// <paramref name="arg"/> is in the allowed range.
72 /// <param name="param">A string that gives the name of the
73 /// parameter to check.</param>
74 /// <param name="arg">An integer that gives the value to check.
76 /// <param name="a">An integer that represents the smallest allowed
78 /// <param name="b">An integer that represents the greatest allowed
80 /// <exception cref="T:System.ArgumentOutOfRangeException">
81 /// The exception is thrown, if the <paramref name="arg"/> is outside
82 /// the allowed range.
84 internal void M_ArgumentInRange(string param
, int arg
, int a
, int b
)
86 if (a
<= arg
&& arg
<= b
)
88 throw new ArgumentOutOfRangeException(param
, M_ValidValues(a
, b
));
92 /// The protected method, that checks whether
93 /// <paramref name="hour"/>, <paramref name="minute"/>,
94 /// <paramref name="second"/>, and <parameref name="millisecond"/>
95 /// are in their valid ranges
97 /// <param name="hour">An integer that represents a hour,
98 /// should be between 0 and 23.</param>
99 /// <param name="minute">An integer that represents a minute,
100 /// should be between 0 and 59.</param>
101 /// <param name="second">An integer that represents a second,
102 /// should be between 0 and 59.</param>
103 /// <param name="milliseconds">An integer that represents a number
104 /// of milliseconds, should be between 0 and 999999.</param>
105 /// <exception cref="T:System.ArgumentOutOfRangeException">
106 /// The Exception is thrown, if one of the parameter is outside the
107 /// allowed the range.
109 internal void M_CheckHMSM(int hour
, int minute
, int second
,
112 M_ArgumentInRange("hour", hour
, 0, 23);
113 M_ArgumentInRange("minute", minute
, 0, 59);
114 M_ArgumentInRange("second", second
, 0, 59);
115 M_ArgumentInRange("milliseconds", milliseconds
, 0, 999999);
119 /// A represantation of the CurrentEra.
121 public const int CurrentEra
= 0;
123 /// <value>When overridden gives the eras supported by the
124 /// calendar as an array of integers.
126 public abstract int[] Eras { get; }
132 [System
.Runtime
.InteropServices
.ComVisible(false)]
133 public virtual CalendarAlgorithmType AlgorithmType
{
135 return CalendarAlgorithmType
.Unknown
;
140 [System
.Runtime
.InteropServices
.ComVisible(false)]
141 public virtual DateTime MaxSupportedDateTime
{
143 return DateTime
.MaxValue
;
147 [System
.Runtime
.InteropServices
.ComVisible(false)]
148 public virtual DateTime MinSupportedDateTime
{
150 return DateTime
.MinValue
;
154 // LAMESPEC: huh, why not Calendar but Object?
156 public virtual object Clone ()
158 Calendar c
= (Calendar
) MemberwiseClone ();
159 c
.m_isReadOnly
= false;
164 public virtual int GetLeapMonth (int year
)
166 return GetLeapMonth (year
, GetEra (ToDateTime (year
, 1, 1, 0, 0, 0, 0)));
170 public virtual int GetLeapMonth (int year
, int era
)
172 int max
= GetMonthsInYear (year
, era
);
173 for (int i
= 1; i
<= max
; i
++)
174 if (IsLeapMonth (year
, i
, era
))
180 public bool IsReadOnly
{
181 get { return m_isReadOnly; }
185 public static Calendar
ReadOnly (Calendar calendar
)
187 if (calendar
.m_isReadOnly
)
189 Calendar c
= (Calendar
) calendar
.Clone ();
190 c
.m_isReadOnly
= true;
194 internal void CheckReadOnly ()
197 throw new InvalidOperationException ("This Calendar is read-only.");
201 /// The protected member stores the value for the
202 /// <see cref="P:TwoDigitYearMax"/>
206 internal int twoDigitYearMax
;
210 /// Private field containing the maximum year for the calendar.
213 private int M_MaxYearValue
= 0;
216 /// Get-only property returing the maximum allowed year for this
219 internal virtual int M_MaxYear
{
221 if (M_MaxYearValue
== 0) {
222 M_MaxYearValue
= GetYear(DateTime
.MaxValue
);
224 return M_MaxYearValue
;
229 /// Checks whether the year is the era is valid, if era = CurrentEra
230 /// the right value is set.
232 /// <param name="year">The year to check.</param>
233 /// <param name="era">The era to check.</Param>
234 /// <exception cref="T:ArgumentOutOfRangeException">
235 /// The exception will be thrown, if the year is not valid.
237 internal virtual void M_CheckYE(int year
, ref int era
)
240 // By default, we do nothing.
242 // This used to be an abstract method in Mono's implementation,
243 // but that means that end-user code could not create their
246 // Binaries would also crash in this condition.
250 /// <para>The property gives the maximum value for years with two
251 /// digits. If the property has the value 2029, than the two-digit
252 /// integer 29 results in the year 2029 and 30 in the
253 /// year 1930.</para>
254 /// <para>It might be overridden.</para>
256 public virtual int TwoDigitYearMax
{
257 get { return twoDigitYearMax; }
260 M_ArgumentInRange("year", value, 100, M_MaxYear
);
261 int era
= CurrentEra
;
262 M_CheckYE(value, ref era
);
263 twoDigitYearMax
= value;
268 /// The virtual method adds days to a given date.
270 /// <param name="time">The
271 /// <see cref="T:System.DateTime"/> to which to add
274 /// <param name="days">The number of days to add.</param>
275 /// <returns>A new <see cref="T:System.DateTime"/> value, that
276 /// results from adding <paramref name="days"/> to the specified
277 /// DateTime.</returns>
278 public virtual DateTime
AddDays(DateTime time
, int days
) {
279 return time
.Add(TimeSpan
.FromDays(days
));
283 /// The virtual method adds hours to a given date.
285 /// <param name="time">The
286 /// <see cref="T:System.DateTime"/> to which to add
289 /// <param name="hours">The number of hours to add.</param>
290 /// <returns>A new <see cref="T:System.DateTime"/> value, that
291 /// results from adding <paramref name="hours"/> to the specified
292 /// DateTime.</returns>
293 public virtual DateTime
AddHours(DateTime time
, int hours
) {
294 return time
.Add(TimeSpan
.FromHours(hours
));
298 /// The virtual method adds milliseconds to a given date.
300 /// <param name="time">The
301 /// <see cref="T:System.DateTime"/> to which to add
304 /// <param name="milliseconds">The number of milliseconds given as
305 /// double to add. Keep in mind the 100 nanosecond resolution of
306 /// <see cref="T:System.DateTime"/>.
308 /// <returns>A new <see cref="T:System.DateTime"/> value, that
309 /// results from adding <paramref name="milliseconds"/> to the specified
310 /// DateTime.</returns>
311 public virtual DateTime
AddMilliseconds(DateTime time
,
314 return time
.Add(TimeSpan
.FromMilliseconds(milliseconds
));
318 /// The virtual method adds minutes to a given date.
320 /// <param name="time">The
321 /// <see cref="T:System.DateTime"/> to which to add
324 /// <param name="minutes">The number of minutes to add.</param>
325 /// <returns>A new <see cref="T:System.DateTime"/> value, that
326 /// results from adding <paramref name="minutes"/> to the specified
327 /// DateTime.</returns>
328 public virtual DateTime
AddMinutes(DateTime time
, int minutes
) {
329 return time
.Add(TimeSpan
.FromMinutes(minutes
));
333 /// When overrideden adds months to a given date.
335 /// <param name="time">The
336 /// <see cref="T:System.DateTime"/> to which to add
339 /// <param name="months">The number of months to add.</param>
340 /// <returns>A new <see cref="T:System.DateTime"/> value, that
341 /// results from adding <paramref name="months"/> to the specified
342 /// DateTime.</returns>
343 public abstract DateTime
AddMonths(DateTime time
, int months
);
346 /// The virtual method adds seconds to a given date.
348 /// <param name="time">The
349 /// <see cref="T:System.DateTime"/> to which to add
352 /// <param name="seconds">The number of seconds to add.</param>
353 /// <returns>A new <see cref="T:System.DateTime"/> value, that
354 /// results from adding <paramref name="seconds"/> to the specified
355 /// DateTime.</returns>
356 public virtual DateTime
AddSeconds(DateTime time
, int seconds
) {
357 return time
.Add(TimeSpan
.FromSeconds(seconds
));
361 /// A wirtual method that adds weeks to a given date.
363 /// <param name="time">The
364 /// <see cref="T:System.DateTime"/> to which to add
367 /// <param name="weeks">The number of weeks to add.</param>
368 /// <returns>A new <see cref="T:System.DateTime"/> value, that
369 /// results from adding <paramref name="weeks"/> to the specified
370 /// DateTime.</returns>
371 public virtual DateTime
AddWeeks(DateTime time
, int weeks
) {
372 return time
.AddDays(weeks
* M_DaysInWeek
);
376 /// When overrideden adds years to a given date.
378 /// <param name="time">The
379 /// <see cref="T:System.DateTime"/> to which to add
382 /// <param name="years">The number of years to add.</param>
383 /// <returns>A new <see cref="T:System.DateTime"/> value, that
384 /// results from adding <paramref name="years"/> to the specified
385 /// DateTime.</returns>
386 public abstract DateTime
AddYears(DateTime time
, int years
);
389 /// When overriden gets the day of the month from
390 /// <paramref name="time"/>.
392 /// <param name="time">The
393 /// <see cref="T:System.DateTime"/> that specifies a
396 /// <returns>An integer giving the day of months, starting with 1.
398 public abstract int GetDayOfMonth(DateTime time
);
401 /// When overriden gets the day of the week from the specified date.
403 /// <param name="time">The
404 /// <see cref="T:System.DateTime"/> that specifies a
407 /// <returns>An integer giving the day of months, starting with 1.
409 public abstract DayOfWeek
GetDayOfWeek(DateTime time
);
412 /// When overridden gives the number of the day in the year.
414 /// <param name="time">The
415 /// <see cref="T:System.DateTime"/> that specifies a
418 /// <returns>An integer representing the day of the year,
419 /// starting with 1.</returns>
420 public abstract int GetDayOfYear(DateTime time
);
423 /// A virtual method that gives the number of days of the specified
424 /// month of the <paramref name="year"/> and the
425 /// <see cref="P:CurrentEra"/>.
427 /// <param name="year">An integer that gives the year in the current
429 /// <param name="month">An integer that gives the month, starting
431 /// <returns>An integer that gives the number of days of the
432 /// specified month.</returns>
433 /// <exception cref="T:System.ArgumentOutOfRangeException">
434 /// The exception is thrown, if <paramref name="month"/> or
435 /// <paramref name="year"/> is outside the allowed range.
437 public virtual int GetDaysInMonth(int year
, int month
) {
438 return GetDaysInMonth(year
, month
, CurrentEra
);
442 /// When overridden gives the number of days in the specified month
443 /// of the given year and era.
445 /// <param name="year">An integer that gives the year.
447 /// <param name="month">An integer that gives the month, starting
449 /// <param name="era">An intger that gives the era of the specified
451 /// <returns>An integer that gives the number of days of the
452 /// specified month.</returns>
453 /// <exception cref="T:System.ArgumentOutOfRangeException">
454 /// The exception is thrown, if <paramref name="month"/>,
455 /// <paramref name="year"/> ,or <paramref name="era"/> is outside
456 /// the allowed range.
458 public abstract int GetDaysInMonth(int year
, int month
, int era
);
461 /// A virtual method that gives the number of days of the specified
462 /// year of the <see cref="P:CurrentEra"/>.
464 /// <param name="year">An integer that gives the year in the current
466 /// <returns>An integer that gives the number of days of the
467 /// specified year.</returns>
468 /// <exception cref="T:System.ArgumentOutOfRangeException">
469 /// The exception is thrown, if
470 /// <paramref name="year"/> is outside the allowed range.
472 public virtual int GetDaysInYear(int year
) {
473 return GetDaysInYear(year
, CurrentEra
);
477 /// When overridden gives the number of days of the specified
478 /// year of the given era..
480 /// <param name="year">An integer that specifies the year.
482 /// <param name="era">An ineger that specifies the era.
484 /// <returns>An integer that gives the number of days of the
485 /// specified year.</returns>
486 /// <exception cref="T:System.ArgumentOutOfRangeExceiption">
487 /// The exception is thrown, if
488 /// <paramref name="year"/> is outside the allowed range.
490 public abstract int GetDaysInYear(int year
, int era
);
493 /// When overridden gives the era of the specified date.
495 /// <param name="time">The
496 /// <see cref="T:System.DateTime"/> that specifies a
499 /// <returns>An integer representing the era of the calendar.
501 public abstract int GetEra(DateTime time
);
504 /// Virtual method that gives the hour of the specified time.
506 /// <param name="time">The
507 /// <see cref="T:System.DateTime"/> that specifies the
510 /// <returns>An integer that gives the hour of the specified time,
511 /// starting with 0.</returns>
512 public virtual int GetHour(DateTime time
) {
513 return time
.TimeOfDay
.Hours
;
517 /// Virtual method that gives the milliseconds in the current second
518 /// of the specified time.
520 /// <param name="time">The
521 /// <see cref="T:System.DateTime"/> that specifies the
524 /// <returns>An integer that gives the milliseconds in the seconds
525 /// of the specified time, starting with 0.</returns>
526 public virtual double GetMilliseconds(DateTime time
) {
527 return time
.TimeOfDay
.Milliseconds
;
531 /// Virtual method that gives the minute of the specified time.
533 /// <param name="time">The
534 /// <see cref="T:System.DateTime"/> that specifies the
537 /// <returns>An integer that gives the minute of the specified time,
538 /// starting with 0.</returns>
539 public virtual int GetMinute(DateTime time
) {
540 return time
.TimeOfDay
.Minutes
;
544 /// When overridden gives the number of the month of the specified
547 /// <param name="time">The
548 /// <see cref="T:System.DateTime"/> that specifies a
551 /// <returns>An integer representing the month,
552 /// starting with 1.</returns>
553 public abstract int GetMonth(DateTime time
);
556 /// Virtual method that gives the number of months of the specified
557 /// year of the <see cref="M:CurrentEra"/>.
559 /// <param name="year">An integer that specifies the year in the
562 /// <returns>An integer that gives the number of the months in the
563 /// specified year.</returns>
564 /// <exception cref="T:System.ArgumentOutOfRangeException">
565 /// The exception is thrown, if the year is not allowed in the
568 public virtual int GetMonthsInYear(int year
) {
569 return GetMonthsInYear(year
, CurrentEra
);
573 /// When overridden gives the number of months in the specified year
576 /// <param name="year">An integer that specifies the year.
578 /// <param name="era">An integer that specifies the era.
580 /// <returns>An integer that gives the number of the months in the
581 /// specified year.</returns>
582 /// <exception cref="T:System.ArgumentOutOfRangeException">
583 /// The exception is thrown, if the year or the era are not valid.
585 public abstract int GetMonthsInYear(int year
, int era
);
588 /// Virtual method that gives the second of the specified time.
590 /// <param name="time">The
591 /// <see cref="T:System.DateTime"/> that specifies the
594 /// <returns>An integer that gives the second of the specified time,
595 /// starting with 0.</returns>
596 public virtual int GetSecond(DateTime time
) {
597 return time
.TimeOfDay
.Seconds
;
601 /// A protected method to calculate the number of days between two
604 /// <param name="timeA">A <see cref="T:System.DateTime"/>
605 /// representing the first date.
607 /// <param name="timeB">A <see cref="T:System.DateTime"/>
608 /// representing the second date.
610 /// <returns>An integer that represents the difference of days
611 /// between <paramref name="timeA"/> and <paramref name="timeB"/>.
613 internal int M_DiffDays(DateTime timeA
, DateTime timeB
) {
614 long diff
= timeA
.Ticks
- timeB
.Ticks
;
617 return (int)(diff
/TimeSpan
.TicksPerDay
);
621 return -1 + (int)(diff
/TimeSpan
.TicksPerDay
);
625 /// A protected method that gives the first day of the second week of
628 /// <param name="year">An integer that represents the year.</param>
629 /// <param name="rule">The
630 /// <see cref="T:System.Globalization.CalendarWeekRule"/>
631 /// to be used for the calculation.
633 /// <param name="firstDayOfWeek">
634 /// The <see cref="T:System.Globalization.DayOfWeek"/>
635 /// specifying the first day in a week.
637 /// <returns>The <see cref="T:System.DateTime"/> representing
638 /// the first day of the second week of the year.
640 internal DateTime
M_GetFirstDayOfSecondWeekOfYear(
641 int year
, CalendarWeekRule rule
, DayOfWeek firstDayOfWeek
)
643 DateTime d1
= ToDateTime(year
, 1, 1, 0, 0, 0, 0);
644 int dow1
= (int)GetDayOfWeek(d1
);
645 int fdow
= (int)firstDayOfWeek
;
649 case CalendarWeekRule
.FirstDay
:
654 d
+= fdow
+ M_DaysInWeek
- dow1
;
657 case CalendarWeekRule
.FirstFullWeek
:
663 d
+= fdow
+ M_DaysInWeek
- dow1
;
666 case CalendarWeekRule
.FirstFourDayWeek
:
667 int dow4
= (dow1
+ 3)%M_DaysInWeek
;
674 d
+= fdow
+ M_DaysInWeek
- dow4
;
679 return AddDays(d1
, d
);
683 /// A virtual method that gives the number of the week in the year.
685 /// <param name="time">A
686 /// <see cref="T:System.DateTime"/> representing the date.
688 /// <param name="rule">The
689 /// <see cref="T:System.Globalization.CalendarWeekRule"/>
690 /// to be used for the calculation.
692 /// <param name="firstDayOfWeek">
693 /// The <see cref="T:System.Globalization.DayOfWeek"/>
694 /// specifying the first day in a week.
696 /// <returns>An integer representing the number of the week in the
697 /// year, starting with 1.
699 public virtual int GetWeekOfYear(DateTime time
,
700 CalendarWeekRule rule
,
701 DayOfWeek firstDayOfWeek
)
703 if (firstDayOfWeek
< DayOfWeek
.Sunday
||
704 DayOfWeek
.Saturday
< firstDayOfWeek
)
706 throw new ArgumentOutOfRangeException("firstDayOfWeek",
707 "Value is not a valid day of week.");
709 int year
= GetYear(time
);
714 DateTime secondWeek
= M_GetFirstDayOfSecondWeekOfYear(
715 year
, rule
, firstDayOfWeek
);
716 days
= M_DiffDays(time
, secondWeek
) + M_DaysInWeek
;
722 return 1 + days
/M_DaysInWeek
;
726 /// When overridden gives the number of the year of the specified
729 /// <param name="time">The
730 /// <see cref="T:System.DateTime"/> that specifies a
733 /// <returns>An integer representing the year,
734 /// starting with 1.</returns>
735 public abstract int GetYear(DateTime time
);
738 /// A virtual method that tells whether the given day in the
739 /// <see cref="M:CurrentEra"/> is a leap day.
741 /// <param name="year">An integer that specifies the year in the
744 /// <param name="month">An integer that specifies the month.
746 /// <param name="day">An integer that specifies the day.
748 /// <returns>A boolean that tells whether the given day is a leap
751 /// <exception cref="T:System.ArgumentOutOfRangeException">
752 /// The exception is thrown, if the year, month or day is not valid
755 public virtual bool IsLeapDay(int year
, int month
, int day
) {
756 return IsLeapDay(year
, month
, day
, CurrentEra
);
760 /// Tells when overridden whether the given day
763 /// <param name="year">An integer that specifies the year in the
766 /// <param name="month">An integer that specifies the month.
768 /// <param name="day">An integer that specifies the day.
770 /// <param name="era">An integer that specifies the era.
772 /// <returns>A boolean that tells whether the given day is a leap
775 /// <exception cref="T:System.ArgumentOutOfRangeException">
776 /// The exception is thrown, if the year, month, day, or era is not
779 public abstract bool IsLeapDay(int year
, int month
, int day
, int era
);
782 /// A virtual method that tells whether the given month of the
783 /// specified year in the
784 /// <see cref="M:CurrentEra"/> is a leap month.
786 /// <param name="year">An integer that specifies the year in the
789 /// <param name="month">An integer that specifies the month.
791 /// <returns>A boolean that tells whether the given month is a leap
794 /// <exception cref="T:System.ArgumentOutOfRangeException">
795 /// The exception is thrown, if the year or month is not valid
798 public virtual bool IsLeapMonth(int year
, int month
) {
799 return IsLeapMonth(year
, month
, CurrentEra
);
803 /// Tells when overridden whether the given month
806 /// <param name="year">An integer that specifies the year in the
809 /// <param name="month">An integer that specifies the month.
811 /// <param name="era">An integer that specifies the era.
813 /// <returns>A boolean that tells whether the given month is a leap
816 /// <exception cref="T:System.ArgumentOutOfRangeException">
817 /// The exception is thrown, if the year, month, or era is not
820 public abstract bool IsLeapMonth(int year
, int month
, int era
);
823 /// A virtual method that tells whether the given year
825 /// <see cref="M:CurrentEra"/> is a leap year.
827 /// <param name="year">An integer that specifies the year in the
830 /// <returns>A boolean that tells whether the given year is a leap
833 /// <exception cref="T:System.ArgumentOutOfRangeException">
834 /// The exception is thrown, if the year is not valid
837 public virtual bool IsLeapYear(int year
) {
838 return IsLeapYear(year
, CurrentEra
);
842 /// Tells when overridden whether the given year
845 /// <param name="year">An integer that specifies the year in the
848 /// <param name="era">An integer that specifies the era.
850 /// <returns>A boolean that tells whether the given year is a leap
853 /// <exception cref="T:System.ArgumentOutOfRangeException">
854 /// The exception is thrown, if the year or era is not
857 public abstract bool IsLeapYear(int year
, int era
);
860 /// A virtual method that creates the
861 /// <see cref="T:System.DateTime"/> from the parameters.
863 /// <param name="year">An integer that gives the year in the
864 /// <see cref="M:CurrentEra"/>.
866 /// <param name="month">An integer that specifies the month.
868 /// <param name="day">An integer that specifies the day.
870 /// <param name="hour">An integer that specifies the hour.
872 /// <param name="minute">An integer that specifies the minute.
874 /// <param name="second">An integer that gives the second.
876 /// <param name="milliseconds">An integer that gives the
880 /// <see cref="T:system.DateTime"/> representig the date and time.
882 /// <exception cref="T:System.ArgumentOutOfRangeException">
883 /// The exception is thrown, if at least one of the parameters
886 public virtual DateTime
ToDateTime(int year
, int month
, int day
,
887 int hour
, int minute
, int second
, int millisecond
)
889 return ToDateTime (year
, month
, day
, hour
, minute
, second
,
890 millisecond
, CurrentEra
);
895 /// When overridden creates the
896 /// <see cref="T:System.DateTime"/> from the parameters.
898 /// <param name="year">An integer that gives the year in the
899 /// <paramref name="era"/>.
901 /// <param name="month">An integer that specifies the month.
903 /// <param name="day">An integer that specifies the day.
905 /// <param name="hour">An integer that specifies the hour.
907 /// <param name="minute">An integer that specifies the minute.
909 /// <param name="second">An integer that gives the second.
911 /// <param name="milliseconds">An integer that gives the
914 /// <param name="era">An integer that specifies the era.
917 /// <see cref="T:system.DateTime"/> representig the date and time.
919 /// <exception cref="T:System.ArgumentOutOfRangeException">
920 /// The exception is thrown, if at least one of the parameters
923 public abstract DateTime
ToDateTime(int year
, int month
, int day
,
924 int hour
, int minute
, int second
, int millisecond
,
928 /// A virtual method that converts a two-digit year to a four-digit
929 /// year. It uses the <see cref="M:TwoDigitYearMax"/> property.
931 /// <param name="year">An integer that gives the two-digit year.
933 /// <returns>An integer giving the four digit year.
935 /// <exception cref="T:System.ArgumentOutOfRangeException">
936 /// The exception is thrown if the year is negative or the resulting
939 public virtual int ToFourDigitYear(int year
) {
941 throw new ArgumentOutOfRangeException(
942 "year", "Non-negative number required.");
943 /* seems not to be the right thing to do, but .NET is
947 int year2
= TwoDigitYearMax
%100;
948 int d
= year
- year2
;
949 year
= TwoDigitYearMax
+ d
+ (d
<= 0 ? 0 : -100);
951 int era
= CurrentEra
;
952 M_CheckYE(year
, ref era
);
956 // TwoDigitYearMax: Windows reads it from the Registry, we
957 // should have an XML file with the defaults
959 /// The default constructor, is sets the TwoDigitYearMax to 2029.
962 /// The .NET framework reads the value from the registry.
963 /// We should implement it here. Currently I set the default values
964 /// in the ctors of the derived classes, if it is 99.
966 protected Calendar() {
967 twoDigitYearMax
= 99;
970 /// <summary>Protected field storing the abbreviated era names.
973 internal string[] M_AbbrEraNames
;
974 /// <summary>Protected field storing the era names.
977 internal string[] M_EraNames
;
980 /// The property stores the era names. It might be overwritten by
983 internal string[] AbbreviatedEraNames
{
985 if (M_AbbrEraNames
== null ||
986 M_AbbrEraNames
.Length
!= Eras
.Length
)
988 "Internal: M_AbbrEraNames " +
989 "wrong initialized!");
990 return (string[])M_AbbrEraNames
.Clone();
994 if (value.Length
!= Eras
.Length
) {
995 StringWriter sw
= new StringWriter();
996 sw
.Write("Array length must be equal Eras " +
997 "length {0}.", Eras
.Length
);
998 throw new ArgumentException(
1001 M_AbbrEraNames
= (string[])value.Clone();
1006 /// The property stores the era names. It might be overwritten by
1009 internal string[] EraNames
{
1011 if (M_EraNames
== null ||
1012 M_EraNames
.Length
!= Eras
.Length
)
1013 throw new Exception(
1014 "Internal: M_EraNames " +
1015 "not initialized!");
1016 return (string[])M_EraNames
.Clone();
1020 if (value.Length
!= Eras
.Length
) {
1021 StringWriter sw
= new StringWriter();
1022 sw
.Write("Array length must be equal Eras " +
1023 "length {0}.", Eras
.Length
);
1024 throw new ArgumentException(
1027 M_EraNames
= (string[])value.Clone();
1031 internal int m_currentEraValue
; // Unused, by MS serializes this
1035 } // namespace System.Globalization