GregorianCalendar.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // ::MONO
  2. //
  3. // System.Globalization.GregorianCalendar.cs
  4. //
  5. // Copyright (C) Wictor Wilén 2001 ([email protected])
  6. //
  7. // Contributors: Wictor Wilén
  8. //
  9. // Revisions
  10. // 2001-09-15: First draft
  11. //
  12. //
  13. // TODO: testing
  14. //
  15. //
  16. using System;
  17. namespace System.Globalization
  18. {
  19. /// <summary>
  20. /// Represents the Gregorian calendar.
  21. /// </summary>
  22. /// <remarks>The Gregorian calendar recognizes two eras: B.C. (before Christ) or B.C.E. (before common era), and A.D. (Latin "Anno Domini", which means "in the year of the Lord") or C.E. (common era). This implementation of the GregorianCalendar class recognizes only the current era (A.D. or C.E.).</remarks>
  23. // TODO: implement the BC era
  24. public class GregorianCalendar : Calendar
  25. {
  26. // private members
  27. private GregorianCalendarTypes _CalendarType;
  28. // Public Constants
  29. public const int ADEra = 1;
  30. // Public Instance Constructors
  31. // DONE!
  32. public GregorianCalendar()
  33. {
  34. _CalendarType = GregorianCalendarTypes.Localized;
  35. }
  36. // DONE!
  37. public GregorianCalendar(GregorianCalendarTypes type)
  38. {
  39. _CalendarType = type;
  40. }
  41. // DONE!
  42. public GregorianCalendarTypes CalendarType
  43. {
  44. get
  45. {
  46. return _CalendarType;
  47. }
  48. set
  49. {
  50. _CalendarType = value;
  51. }
  52. }
  53. // Public Instance Properties
  54. // DONE!
  55. public override int[] Eras
  56. {
  57. get
  58. {
  59. return new int[] {1};
  60. }
  61. }
  62. // DONE!
  63. public override int TwoDigitYearMax
  64. {
  65. get
  66. {
  67. return _TwoDigitYearMax;
  68. }
  69. set
  70. {
  71. _TwoDigitYearMax = value;
  72. }
  73. }
  74. // Public Instance Methods
  75. // DONE!
  76. public override DateTime AddMonths ( DateTime time, int months )
  77. {
  78. if(months < -120000 || months > 120000)
  79. throw new System.ArgumentOutOfRangeException();
  80. DateTime dt = new DateTime(time.Ticks);
  81. dt = dt.AddMonths(months);
  82. return dt;
  83. }
  84. // DONE!
  85. public override DateTime AddYears ( DateTime time, int years )
  86. {
  87. DateTime dt = new DateTime(time.Ticks);
  88. return dt.AddYears(years);
  89. }
  90. // DONE!
  91. public override int GetDayOfMonth ( DateTime time )
  92. {
  93. return time.Day;
  94. }
  95. // DONE!
  96. public override DayOfWeek GetDayOfWeek ( DateTime time )
  97. {
  98. return time.DayOfWeek;
  99. }
  100. // DONE!
  101. public override int GetDayOfYear ( DateTime time )
  102. {
  103. return time.DayOfYear;
  104. }
  105. // DONE!
  106. public override int GetDaysInMonth ( int year, int month, int era )
  107. {
  108. if( year < _MinYear || year > _MaxYear ||
  109. month < _MinMonth || month > _MaxMonth )
  110. throw new System.ArgumentOutOfRangeException();
  111. if( era != ADEra)
  112. throw new System.ArgumentException();
  113. if(this.IsLeapYear(year))
  114. return _DaysInMonthLeap[month];
  115. else
  116. return _DaysInMonth[month];
  117. }
  118. // DONE!
  119. public override int GetDaysInYear ( int year, int era )
  120. {
  121. if(year < _MinYear || year > _MaxYear)
  122. throw new System.ArgumentOutOfRangeException();
  123. if( era != ADEra)
  124. throw new System.ArgumentException();
  125. return this.GetDaysInYear(year);
  126. }
  127. // DONE!
  128. public override int GetEra ( DateTime time )
  129. {
  130. return ADEra;
  131. }
  132. // DONE!
  133. public override int GetMonth ( DateTime time )
  134. {
  135. return time.Month;
  136. }
  137. // DONE!
  138. public override int GetMonthsInYear ( int year, int era )
  139. {
  140. if(year < _MinYear || year > _MaxYear || era != ADEra )
  141. throw new System.ArgumentOutOfRangeException();
  142. return _MaxMonth;
  143. }
  144. // DONE!
  145. public override int GetYear ( DateTime time )
  146. {
  147. return time.Year;
  148. }
  149. // DONE!
  150. public override bool IsLeapDay ( int year, int month, int day, int era )
  151. {
  152. int dim;
  153. if(day < _MinDay || month < _MinMonth || month > _MaxMonth)
  154. throw new System.ArgumentException();
  155. if(this.IsLeapYear(year,era))
  156. dim = _DaysInMonthLeap[month-1];
  157. else
  158. dim = _DaysInMonth[month-1];
  159. if( day > dim)
  160. throw new System.ArgumentException();
  161. if( month == 2 && day == 29)
  162. return true;
  163. return false;
  164. }
  165. // DONE!
  166. public override bool IsLeapMonth ( int year, int month )
  167. {
  168. if( year < _MinYear || year > _MaxYear || month < _MinMonth || month > _MaxMonth)
  169. throw new System.ArgumentException();
  170. return false;
  171. }
  172. // DONE!
  173. public override bool IsLeapMonth ( int year, int month, int era )
  174. {
  175. if( year < _MinYear || year > _MaxYear || month < _MinMonth || month > _MaxMonth || era != ADEra)
  176. throw new System.ArgumentException();
  177. return false;
  178. }
  179. // DONE!
  180. public override bool IsLeapYear ( int year, int era )
  181. {
  182. if(year < _MinYear || year > _MaxYear || era != ADEra)
  183. throw new System.ArgumentOutOfRangeException();
  184. if( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) )
  185. return true;
  186. return false;
  187. }
  188. // DONE!
  189. public override DateTime ToDateTime ( int year, int month, int day, int hour, int minute, int second, int millisecond, int era )
  190. {
  191. // INFO: year, era and month is checked by GetDaysInMonth()
  192. int dim;
  193. dim = GetDaysInMonth(year,month);
  194. if( day < _MinDay || day > dim ||
  195. hour < _MinHour || hour > _MaxHour ||
  196. minute < _MinMinute || minute > _MaxMinute ||
  197. second < _MinSecond || second > _MaxSecond ||
  198. millisecond < _MinMillisecond || millisecond > _MaxMillisecond)
  199. throw new System.ArgumentException();
  200. return new DateTime(year,month,day,hour,minute,second,millisecond,this);
  201. }
  202. // DONE!
  203. public override int ToFourDigitYear ( int year )
  204. {
  205. int y = _TwoDigitYearMax % 100;
  206. if( year > y )
  207. y = _TwoDigitYearMax - y - 100 + year;
  208. else
  209. y = _TwoDigitYearMax - y + year;
  210. if( y < _MinYear || y > _MaxYear)
  211. throw new System.ArgumentException();
  212. return y;
  213. }
  214. }
  215. }