GregorianCalendar.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. // GregorianCalendar.cs
  2. //
  3. // (C) Ulrich Kunitz 2002
  4. //
  5. namespace System.Globalization {
  6. using System;
  7. /// <summary>
  8. /// This is the Gregorian calendar.
  9. /// </summary>
  10. /// <remarks>
  11. /// <para>The Gregorian calendar supports only the Common Era from
  12. /// the Gregorian year 1 to the Gregorian year 9999.
  13. /// </para>
  14. /// <para>The implementation uses the
  15. /// <see cref="N:CalendricalCalculations"/> namespace.
  16. /// </para>
  17. /// </remarks>
  18. [Serializable]
  19. public class GregorianCalendar : Calendar {
  20. /// <summary>
  21. /// The era number for the Common Era (C.E.) or Anno Domini (A.D.)
  22. /// respective.
  23. /// </summary>
  24. public const int ADEra = 1;
  25. /// <value>Overridden. Gives the eras supported by the Gregorian
  26. /// calendar as an array of integers.
  27. /// </value>
  28. public override int[] Eras {
  29. get {
  30. return new int[] { ADEra };
  31. }
  32. }
  33. [MonoTODO]
  34. public override int TwoDigitYearMax
  35. {
  36. get {
  37. throw new NotImplementedException();
  38. }
  39. set {
  40. throw new NotImplementedException();
  41. }
  42. }
  43. /// <summary>
  44. /// A protected member storing the
  45. /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>.
  46. /// </summary>
  47. internal GregorianCalendarTypes M_CalendarType;
  48. /// <value>
  49. /// The property stores the
  50. /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>.
  51. /// </value>
  52. public virtual GregorianCalendarTypes CalendarType {
  53. get { return M_CalendarType; }
  54. set {
  55. // mscorlib 1:0:33000:0 doesn't check anything here
  56. M_CalendarType = value;
  57. }
  58. }
  59. /// <summary>
  60. /// A protected method checking the era number.
  61. /// </summary>
  62. /// <param name="era">The era number.</param>
  63. /// <exception name="T:System.ArgumentException">
  64. /// The exception is thrown if the era is not equal
  65. /// <see cref="M:ADEra"/>.
  66. /// </exception>
  67. internal void M_CheckEra(ref int era) {
  68. if (era == CurrentEra)
  69. era = ADEra;
  70. if (era != ADEra)
  71. throw new ArgumentException("Era value was not valid.");
  72. }
  73. /// <summary>
  74. /// A protected method checking calendar year and the era number.
  75. /// </summary>
  76. /// <param name="year">An integer representing the calendar year.
  77. /// </param>
  78. /// <param name="era">The era number.</param>
  79. /// <exception cref="T:System.ArgumentException">
  80. /// The exception is thrown if the era is not equal
  81. /// <see cref="M:ADEra"/>.
  82. /// </exception>
  83. /// <exception cref="T:System.ArgumentOutOfRangeException">
  84. /// The exception is thrown if the calendar year is outside of
  85. /// the allowed range.
  86. /// </exception>
  87. internal override void M_CheckYE(int year, ref int era) {
  88. M_CheckEra(ref era);
  89. M_ArgumentInRange("year", year, 1, 9999);
  90. }
  91. /// <summary>
  92. /// A protected method checking the calendar year, month, and
  93. /// era number.
  94. /// </summary>
  95. /// <param name="year">An integer representing the calendar year.
  96. /// </param>
  97. /// <param name="month">An integer giving the calendar month.
  98. /// </param>
  99. /// <param name="era">The era number.</param>
  100. /// <exception cref="T:System.ArgumentException">
  101. /// The exception is thrown if the era is not equal
  102. /// <see cref="M:ADEra"/>.
  103. /// </exception>
  104. /// <exception cref="T:System.ArgumentOutOfRangeException">
  105. /// The exception is thrown if the calendar year or month is
  106. /// outside of the allowed range.
  107. /// </exception>
  108. internal void M_CheckYME(int year, int month, ref int era) {
  109. M_CheckYE(year, ref era);
  110. if (month < 1 || month > 12)
  111. throw new ArgumentOutOfRangeException("month",
  112. "Month must be between one and twelve.");
  113. }
  114. /// <summary>
  115. /// A protected method checking the calendar day, month, and year
  116. /// and the era number.
  117. /// </summary>
  118. /// <param name="year">An integer representing the calendar year.
  119. /// </param>
  120. /// <param name="month">An integer giving the calendar month.
  121. /// </param>
  122. /// <param name="day">An integer giving the calendar day.
  123. /// </param>
  124. /// <param name="era">The era number.</param>
  125. /// <exception cref="T:System.ArgumentException">
  126. /// The exception is thrown if the era is not equal
  127. /// <see cref="M:ADEra"/>.
  128. /// </exception>
  129. /// <exception cref="T:System.ArgumentOutOfRangeException">
  130. /// The exception is thrown if the calendar year, month, or day is
  131. /// outside of the allowed range.
  132. /// </exception>
  133. internal void M_CheckYMDE(int year, int month, int day, ref int era)
  134. {
  135. M_CheckYME(year, month, ref era);
  136. M_ArgumentInRange("day", day, 1,
  137. GetDaysInMonth(year, month, era));
  138. }
  139. /// <summary>
  140. /// Overridden. Adds months to a given date.
  141. /// </summary>
  142. /// <param name="time">The
  143. /// <see cref="T:System.DateTime"/> to which to add
  144. /// months.
  145. /// </param>
  146. /// <param name="months">The number of months to add.</param>
  147. /// <returns>A new <see cref="T:System.DateTime"/> value, that
  148. /// results from adding <paramref name="months"/> to the specified
  149. /// DateTime.</returns>
  150. public override DateTime AddMonths(DateTime time, int months) {
  151. return CCGregorianCalendar.AddMonths(time, months);
  152. }
  153. [MonoTODO]
  154. public override DateTime AddWeeks(DateTime time, int weeks)
  155. {
  156. throw new NotImplementedException();
  157. }
  158. /// <summary>
  159. /// Overridden. Adds years to a given date.
  160. /// </summary>
  161. /// <param name="time">The
  162. /// <see cref="T:System.DateTime"/> to which to add
  163. /// years.
  164. /// </param>
  165. /// <param name="years">The number of years to add.</param>
  166. /// <returns>A new <see cref="T:System.DateTime"/> value, that
  167. /// results from adding <paramref name="years"/> to the specified
  168. /// DateTime.</returns>
  169. public override DateTime AddYears(DateTime time, int years) {
  170. return CCGregorianCalendar.AddYears(time, years);
  171. }
  172. /// <summary>
  173. /// Overridden. Gets the day of the month from
  174. /// <paramref name="time"/>.
  175. /// </summary>
  176. /// <param name="time">The
  177. /// <see cref="T:System.DateTime"/> that specifies a
  178. /// date.
  179. /// </param>
  180. /// <returns>An integer giving the day of months, starting with 1.
  181. /// </returns>
  182. public override int GetDayOfMonth(DateTime time) {
  183. return CCGregorianCalendar.GetDayOfMonth(time);
  184. }
  185. /// <summary>
  186. /// Overridden. Gets the day of the week from the specified date.
  187. /// </summary>
  188. /// <param name="time">The
  189. /// <see cref="T:System.DateTime"/> that specifies a
  190. /// date.
  191. /// </param>
  192. /// <returns>An integer giving the day of months, starting with 1.
  193. /// </returns>
  194. public override DayOfWeek GetDayOfWeek(DateTime time) {
  195. int rd = CCFixed.FromDateTime(time);
  196. return (DayOfWeek)CCFixed.day_of_week(rd);
  197. }
  198. /// <summary>
  199. /// Overridden. Gives the number of the day in the year.
  200. /// </summary>
  201. /// <param name="time">The
  202. /// <see cref="T:System.DateTime"/> that specifies a
  203. /// date.
  204. /// </param>
  205. /// <returns>An integer representing the day of the year,
  206. /// starting with 1.</returns>
  207. public override int GetDayOfYear(DateTime time) {
  208. return CCGregorianCalendar.GetDayOfYear(time);
  209. }
  210. /// <summary>
  211. /// Overridden. Gives the number of days in the specified month
  212. /// of the given year and era.
  213. /// </summary>
  214. /// <param name="year">An integer that gives the year.
  215. /// </param>
  216. /// <param name="month">An integer that gives the month, starting
  217. /// with 1.</param>
  218. /// <param name="era">An intger that gives the era of the specified
  219. /// year.</param>
  220. /// <returns>An integer that gives the number of days of the
  221. /// specified month.</returns>
  222. /// <exception cref="T:System.ArgumentOutOfRangeException">
  223. /// The exception is thrown, if <paramref name="month"/>,
  224. /// <paramref name="year"/> ,or <paramref name="era"/> is outside
  225. /// the allowed range.
  226. /// </exception>
  227. public override int GetDaysInMonth(int year, int month, int era) {
  228. // mscorlib doesn't check year, probably a bug; we do
  229. M_CheckYME(year, month, ref era);
  230. return CCGregorianCalendar.GetDaysInMonth(year, month);
  231. }
  232. /// <summary>
  233. /// Overridden. Gives the number of days of the specified
  234. /// year of the given era.
  235. /// </summary>
  236. /// <param name="year">An integer that specifies the year.
  237. /// </param>
  238. /// <param name="era">An ineger that specifies the era.
  239. /// </param>
  240. /// <returns>An integer that gives the number of days of the
  241. /// specified year.</returns>
  242. /// <exception cref="T:System.ArgumentOutOfRangeExceiption">
  243. /// The exception is thrown, if
  244. /// <paramref name="year"/> is outside the allowed range.
  245. /// </exception>
  246. public override int GetDaysInYear(int year, int era) {
  247. M_CheckYE(year, ref era);
  248. return CCGregorianCalendar.GetDaysInYear(year);
  249. }
  250. /// <summary>
  251. /// Overridden. Gives the era of the specified date.
  252. /// </summary>
  253. /// <param name="time">The
  254. /// <see cref="T:System.DateTime"/> that specifies a
  255. /// date.
  256. /// </param>
  257. /// <returns>An integer representing the era of the calendar.
  258. /// </returns>
  259. public override int GetEra(DateTime time) {
  260. return ADEra;
  261. }
  262. /// <summary>
  263. /// Overridden. Gives the number of the month of the specified
  264. /// date.
  265. /// </summary>
  266. /// <param name="time">The
  267. /// <see cref="T:System.DateTime"/> that specifies a
  268. /// date.
  269. /// </param>
  270. /// <returns>An integer representing the month,
  271. /// starting with 1.</returns>
  272. public override int GetMonth(DateTime time) {
  273. return CCGregorianCalendar.GetMonth(time);
  274. }
  275. /// <summary>
  276. /// Overridden. Gives the number of months in the specified year
  277. /// and era.
  278. /// </summary>
  279. /// <param name="year">An integer that specifies the year.
  280. /// </param>
  281. /// <param name="era">An integer that specifies the era.
  282. /// </param>
  283. /// <returns>An integer that gives the number of the months in the
  284. /// specified year.</returns>
  285. /// <exception cref="T:System.ArgumentOutOfRangeException">
  286. /// The exception is thrown, if the year or the era are not valid.
  287. /// </exception>
  288. public override int GetMonthsInYear(int year, int era) {
  289. M_CheckYE(year, ref era);
  290. return 12;
  291. }
  292. /// <summary>
  293. /// Overridden. Gives the number of the year of the specified
  294. /// date.
  295. /// </summary>
  296. /// <param name="time">The
  297. /// <see cref="T:System.DateTime"/> that specifies a
  298. /// date.
  299. /// </param>
  300. /// <returns>An integer representing the year,
  301. /// starting with 1.</returns>
  302. public override int GetYear(DateTime time) {
  303. return CCGregorianCalendar.GetYear(time);
  304. }
  305. /// <summary>
  306. /// Overridden. Tells whether the given day
  307. /// is a leap day.
  308. /// </summary>
  309. /// <param name="year">An integer that specifies the year in the
  310. /// given era.
  311. /// </param>
  312. /// <param name="month">An integer that specifies the month.
  313. /// </param>
  314. /// <param name="day">An integer that specifies the day.
  315. /// </param>
  316. /// <param name="era">An integer that specifies the era.
  317. /// </param>
  318. /// <returns>A boolean that tells whether the given day is a leap
  319. /// day.
  320. /// </returns>
  321. /// <exception cref="T:System.ArgumentOutOfRangeException">
  322. /// The exception is thrown, if the year, month, day, or era is not
  323. /// valid.
  324. /// </exception>
  325. public override bool IsLeapDay(int year, int month, int day, int era)
  326. {
  327. M_CheckYMDE(year, month, day, ref era);
  328. return CCGregorianCalendar.IsLeapDay(year, month, day);
  329. }
  330. /// <summary>
  331. /// Overridden. Tells whether the given month
  332. /// is a leap month.
  333. /// </summary>
  334. /// <param name="year">An integer that specifies the year in the
  335. /// given era.
  336. /// </param>
  337. /// <param name="month">An integer that specifies the month.
  338. /// </param>
  339. /// <param name="era">An integer that specifies the era.
  340. /// </param>
  341. /// <returns>A boolean that tells whether the given month is a leap
  342. /// month.
  343. /// </returns>
  344. /// <exception cref="T:System.ArgumentOutOfRangeException">
  345. /// The exception is thrown, if the year, month, or era is not
  346. /// valid.
  347. /// </exception>
  348. public override bool IsLeapMonth(int year, int month, int era) {
  349. M_CheckYME(year, month, ref era);
  350. return false;
  351. }
  352. /// <summary>
  353. /// Overridden. Tells whether the given year
  354. /// is a leap year.
  355. /// </summary>
  356. /// <param name="year">An integer that specifies the year in the
  357. /// given era.
  358. /// </param>
  359. /// <param name="era">An integer that specifies the era.
  360. /// </param>
  361. /// <returns>A boolean that tells whether the given year is a leap
  362. /// year.
  363. /// </returns>
  364. /// <exception cref="T:System.ArgumentOutOfRangeException">
  365. /// The exception is thrown, if the year or era is not
  366. /// valid.
  367. /// </exception>
  368. public override bool IsLeapYear(int year, int era) {
  369. M_CheckYE(year, ref era);
  370. return CCGregorianCalendar.is_leap_year(year);
  371. }
  372. /// <summary>
  373. /// Overridden. Creates the
  374. /// <see cref="T:System.DateTime"/> from the parameters.
  375. /// </summary>
  376. /// <param name="year">An integer that gives the year in the
  377. /// <paramref name="era"/>.
  378. /// </param>
  379. /// <param name="month">An integer that specifies the month.
  380. /// </param>
  381. /// <param name="day">An integer that specifies the day.
  382. /// </param>
  383. /// <param name="hour">An integer that specifies the hour.
  384. /// </param>
  385. /// <param name="minute">An integer that specifies the minute.
  386. /// </param>
  387. /// <param name="second">An integer that gives the second.
  388. /// </param>
  389. /// <param name="milliseconds">An integer that gives the
  390. /// milliseconds.
  391. /// </param>
  392. /// <param name="era">An integer that specifies the era.
  393. /// </param>
  394. /// <returns>
  395. /// <see cref="T:system.DateTime"/> representig the date and time.
  396. /// </returns>
  397. /// <exception cref="T:System.ArgumentOutOfRangeException">
  398. /// The exception is thrown, if at least one of the parameters
  399. /// is out of range.
  400. /// </exception>
  401. public override DateTime ToDateTime(int year, int month, int day,
  402. int hour, int minute, int second, int milliseconds,
  403. int era)
  404. {
  405. M_CheckYMDE(year, month, day, ref era);
  406. M_CheckHMSM(hour, minute, second, milliseconds);
  407. return CCGregorianCalendar.ToDateTime(
  408. year, month, day,
  409. hour, minute, second, milliseconds);
  410. }
  411. [MonoTODO]
  412. public override int ToFourDigitYear(int year)
  413. {
  414. throw new NotImplementedException();
  415. }
  416. /// <summary>
  417. /// Constructor that sets the
  418. /// Gregorian calendar type (
  419. /// <see cref="T:System.Globalization.GregorianCalendarTypes"/>).
  420. /// </summary>
  421. /// <param name="type">The parameter specifies the Gregorian
  422. /// calendar type.
  423. /// </param>
  424. public GregorianCalendar(GregorianCalendarTypes type) {
  425. CalendarType = type;
  426. M_AbbrEraNames = new string[] {"C.E."};
  427. M_EraNames = new string[] {"Common Era"};
  428. if (M_TwoDigitYearMax == 99)
  429. M_TwoDigitYearMax = 2029;
  430. }
  431. /// <summary>
  432. /// Default constructor. Sets the Gregorian calendar type to
  433. /// <see
  434. /// cref="F:System.Globalization.GregorianCalendarTypes.Localized"/>.
  435. /// </summary>
  436. public GregorianCalendar() : this(GregorianCalendarTypes.Localized) {}
  437. } // class GregorianCalendar
  438. } // namespace System.Globalization