| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- 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);
- }
- }
- }
- }
|