JulianCalendar.cs 4.8 KB

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