namespace IndexFormula.Finance { using System; using System.ComponentModel; using System.Xml.Serialization; [TypeConverter(typeof(DataCycleConverter))] public class DataCycle { private DataCycleBase cycleBase; private int repeat; public int WeekAdjust; public DataCycle() { this.cycleBase = DataCycleBase.DAY; } public DataCycle(DataCycleBase CycleBase, int Repeat) { this.cycleBase = DataCycleBase.DAY; this.cycleBase = CycleBase; this.repeat = Repeat; } public override bool Equals(object obj) { return ((obj is DataCycle) && (((obj as DataCycle).cycleBase == this.cycleBase) && ((obj as DataCycle).repeat == this.repeat))); } public override int GetHashCode() { return base.GetHashCode(); } public int GetSequence(double D) { int year = 0; if (this.CycleBase == DataCycleBase.DAY) { year = (int) D; } else if (this.CycleBase == DataCycleBase.WEEK) { year = (((int) D) + this.WeekAdjust) / 7; } else if (this.CycleBase == DataCycleBase.YEAR) { year = DateTime.FromOADate(D).Year; } else if (this.CycleBase > DataCycleBase.WEEK) { DateTime time = DateTime.FromOADate(D); year = ((time.Year * 12) + time.Month) - 1; switch (this.CycleBase) { case DataCycleBase.QUARTER: year /= 3; break; case DataCycleBase.HALFYEAR: year /= 6; break; } } else { if (this.CycleBase == DataCycleBase.HOUR) { return (int) (((D * 24.0) / ((double) this.Repeat)) + 0.001); } if (this.CycleBase == DataCycleBase.MINUTE) { return (int) (((((((((int) D) % 100) + D) - ((int) D)) * 24.0) * 60.0) / ((double) this.Repeat)) + 0.001); } return (int) (((((((((int) D) % 100) + D) - ((int) D)) * 24.0) * 3600.0) / ((double) this.Repeat)) + 0.001); } return (year /= this.Repeat); } public static DateTime operator +(DateTime d, DataCycle dc) { switch (dc.CycleBase) { case DataCycleBase.SECOND: return d.AddSeconds((double) dc.Repeat); case DataCycleBase.MINUTE: return d.AddMinutes((double) dc.Repeat); case DataCycleBase.HOUR: return d.AddHours((double) dc.Repeat); case DataCycleBase.DAY: return d.AddDays((double) dc.Repeat); case DataCycleBase.WEEK: return d.AddDays((double) (dc.Repeat * 7)); case DataCycleBase.MONTH: return d.AddMonths(dc.Repeat); case DataCycleBase.QUARTER: return d.AddMonths(dc.Repeat * 3); case DataCycleBase.HALFYEAR: return d.AddMonths(dc.Repeat * 6); case DataCycleBase.YEAR: return d.AddYears(dc.Repeat); } return d; } public static DateTime operator -(DateTime d, DataCycle dc) { return (d + -dc); } public static DataCycle operator -(DataCycle dc) { return new DataCycle(dc.cycleBase, -dc.repeat); } public static DataCycle Parse(string s) { try { for (int i = 0; i < s.Length; i++) { if (char.IsDigit(s, i)) { return new DataCycle((DataCycleBase) Enum.Parse(typeof(DataCycleBase), s.Substring(0, i), true), int.Parse(s.Substring(i))); } } return new DataCycle((DataCycleBase) Enum.Parse(typeof(DataCycleBase), s, true), 1); } catch { return Day; } } public bool SameSequence(DateTime d1, DateTime d2) { return this.SameSequence(d1.ToOADate(), d2.ToOADate()); } public bool SameSequence(double d1, double d2) { if (this.cycleBase == DataCycleBase.TICK) { return (d1 == d2); } return (this.GetSequence(d1) == this.GetSequence(d2)); } public override string ToString() { return (this.CycleBase.ToString() + this.Repeat); } [XmlAttribute, DefaultValue(4), RefreshProperties(RefreshProperties.Repaint)] public DataCycleBase CycleBase { get { return this.cycleBase; } set { this.cycleBase = value; } } public static DataCycle Day { get { return new DataCycle(DataCycleBase.DAY, 1); } } public static DataCycle Hour { get { return new DataCycle(DataCycleBase.HOUR, 1); } } public static DataCycle Minute { get { return new DataCycle(DataCycleBase.MINUTE, 1); } } public static DataCycle Month { get { return new DataCycle(DataCycleBase.MONTH, 1); } } public static DataCycle Quarter { get { return new DataCycle(DataCycleBase.QUARTER, 1); } } [DefaultValue(1), RefreshProperties(RefreshProperties.Repaint), XmlAttribute] public int Repeat { get { return this.repeat; } set { this.repeat = value; } } public static DataCycle Tick { get { return new DataCycle(DataCycleBase.TICK, 1); } } public static DataCycle Week { get { return new DataCycle(DataCycleBase.WEEK, 1); } } public static DataCycle Year { get { return new DataCycle(DataCycleBase.YEAR, 1); } } } }