DataCycle.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. namespace IndexFormula.Finance
  2. {
  3. using System;
  4. using System.ComponentModel;
  5. using System.Xml.Serialization;
  6. [TypeConverter(typeof(DataCycleConverter))]
  7. public class DataCycle
  8. {
  9. private DataCycleBase cycleBase;
  10. private int repeat;
  11. public int WeekAdjust;
  12. public DataCycle()
  13. {
  14. this.cycleBase = DataCycleBase.DAY;
  15. }
  16. public DataCycle(DataCycleBase CycleBase, int Repeat)
  17. {
  18. this.cycleBase = DataCycleBase.DAY;
  19. this.cycleBase = CycleBase;
  20. this.repeat = Repeat;
  21. }
  22. public override bool Equals(object obj)
  23. {
  24. return ((obj is DataCycle) && (((obj as DataCycle).cycleBase == this.cycleBase) && ((obj as DataCycle).repeat == this.repeat)));
  25. }
  26. public override int GetHashCode()
  27. {
  28. return base.GetHashCode();
  29. }
  30. public int GetSequence(double D)
  31. {
  32. int year = 0;
  33. if (this.CycleBase == DataCycleBase.DAY)
  34. {
  35. year = (int) D;
  36. }
  37. else if (this.CycleBase == DataCycleBase.WEEK)
  38. {
  39. year = (((int) D) + this.WeekAdjust) / 7;
  40. }
  41. else if (this.CycleBase == DataCycleBase.YEAR)
  42. {
  43. year = DateTime.FromOADate(D).Year;
  44. }
  45. else if (this.CycleBase > DataCycleBase.WEEK)
  46. {
  47. DateTime time = DateTime.FromOADate(D);
  48. year = ((time.Year * 12) + time.Month) - 1;
  49. switch (this.CycleBase)
  50. {
  51. case DataCycleBase.QUARTER:
  52. year /= 3;
  53. break;
  54. case DataCycleBase.HALFYEAR:
  55. year /= 6;
  56. break;
  57. }
  58. }
  59. else
  60. {
  61. if (this.CycleBase == DataCycleBase.HOUR)
  62. {
  63. return (int) (((D * 24.0) / ((double) this.Repeat)) + 0.001);
  64. }
  65. if (this.CycleBase == DataCycleBase.MINUTE)
  66. {
  67. return (int) (((((((((int) D) % 100) + D) - ((int) D)) * 24.0) * 60.0) / ((double) this.Repeat)) + 0.001);
  68. }
  69. return (int) (((((((((int) D) % 100) + D) - ((int) D)) * 24.0) * 3600.0) / ((double) this.Repeat)) + 0.001);
  70. }
  71. return (year /= this.Repeat);
  72. }
  73. public static DateTime operator +(DateTime d, DataCycle dc)
  74. {
  75. switch (dc.CycleBase)
  76. {
  77. case DataCycleBase.SECOND:
  78. return d.AddSeconds((double) dc.Repeat);
  79. case DataCycleBase.MINUTE:
  80. return d.AddMinutes((double) dc.Repeat);
  81. case DataCycleBase.HOUR:
  82. return d.AddHours((double) dc.Repeat);
  83. case DataCycleBase.DAY:
  84. return d.AddDays((double) dc.Repeat);
  85. case DataCycleBase.WEEK:
  86. return d.AddDays((double) (dc.Repeat * 7));
  87. case DataCycleBase.MONTH:
  88. return d.AddMonths(dc.Repeat);
  89. case DataCycleBase.QUARTER:
  90. return d.AddMonths(dc.Repeat * 3);
  91. case DataCycleBase.HALFYEAR:
  92. return d.AddMonths(dc.Repeat * 6);
  93. case DataCycleBase.YEAR:
  94. return d.AddYears(dc.Repeat);
  95. }
  96. return d;
  97. }
  98. public static DateTime operator -(DateTime d, DataCycle dc)
  99. {
  100. return (d + -dc);
  101. }
  102. public static DataCycle operator -(DataCycle dc)
  103. {
  104. return new DataCycle(dc.cycleBase, -dc.repeat);
  105. }
  106. public static DataCycle Parse(string s)
  107. {
  108. try
  109. {
  110. for (int i = 0; i < s.Length; i++)
  111. {
  112. if (char.IsDigit(s, i))
  113. {
  114. return new DataCycle((DataCycleBase) Enum.Parse(typeof(DataCycleBase), s.Substring(0, i), true), int.Parse(s.Substring(i)));
  115. }
  116. }
  117. return new DataCycle((DataCycleBase) Enum.Parse(typeof(DataCycleBase), s, true), 1);
  118. }
  119. catch
  120. {
  121. return Day;
  122. }
  123. }
  124. public bool SameSequence(DateTime d1, DateTime d2)
  125. {
  126. return this.SameSequence(d1.ToOADate(), d2.ToOADate());
  127. }
  128. public bool SameSequence(double d1, double d2)
  129. {
  130. if (this.cycleBase == DataCycleBase.TICK)
  131. {
  132. return (d1 == d2);
  133. }
  134. return (this.GetSequence(d1) == this.GetSequence(d2));
  135. }
  136. public override string ToString()
  137. {
  138. return (this.CycleBase.ToString() + this.Repeat);
  139. }
  140. [XmlAttribute, DefaultValue(4), RefreshProperties(RefreshProperties.Repaint)]
  141. public DataCycleBase CycleBase
  142. {
  143. get
  144. {
  145. return this.cycleBase;
  146. }
  147. set
  148. {
  149. this.cycleBase = value;
  150. }
  151. }
  152. public static DataCycle Day
  153. {
  154. get
  155. {
  156. return new DataCycle(DataCycleBase.DAY, 1);
  157. }
  158. }
  159. public static DataCycle Hour
  160. {
  161. get
  162. {
  163. return new DataCycle(DataCycleBase.HOUR, 1);
  164. }
  165. }
  166. public static DataCycle Minute
  167. {
  168. get
  169. {
  170. return new DataCycle(DataCycleBase.MINUTE, 1);
  171. }
  172. }
  173. public static DataCycle Month
  174. {
  175. get
  176. {
  177. return new DataCycle(DataCycleBase.MONTH, 1);
  178. }
  179. }
  180. public static DataCycle Quarter
  181. {
  182. get
  183. {
  184. return new DataCycle(DataCycleBase.QUARTER, 1);
  185. }
  186. }
  187. [DefaultValue(1), RefreshProperties(RefreshProperties.Repaint), XmlAttribute]
  188. public int Repeat
  189. {
  190. get
  191. {
  192. return this.repeat;
  193. }
  194. set
  195. {
  196. this.repeat = value;
  197. }
  198. }
  199. public static DataCycle Tick
  200. {
  201. get
  202. {
  203. return new DataCycle(DataCycleBase.TICK, 1);
  204. }
  205. }
  206. public static DataCycle Week
  207. {
  208. get
  209. {
  210. return new DataCycle(DataCycleBase.WEEK, 1);
  211. }
  212. }
  213. public static DataCycle Year
  214. {
  215. get
  216. {
  217. return new DataCycle(DataCycleBase.YEAR, 1);
  218. }
  219. }
  220. }
  221. }