PageRenderTime 53ms CodeModel.GetById 11ms app.highlight 37ms RepoModel.GetById 1ms app.codeStats 1ms

/Main/src/DynamicDataDisplay/Charts/Axes/DateTime/DateTimeTicksProvider.cs

#
C# | 307 lines | 249 code | 52 blank | 6 comment | 10 complexity | b1b893da38146e2d715a681e1cd84667 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Text;
  5using System.Diagnostics;
  6using System.Globalization;
  7using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
  8
  9namespace Microsoft.Research.DynamicDataDisplay.Charts
 10{
 11	/// <summary>
 12	/// Represents a ticks provider for ticks of <see cref="T:System.DateTime"/> type.
 13	/// </summary>
 14	public class DateTimeTicksProvider : TimeTicksProviderBase<DateTime>
 15	{
 16		/// <summary>
 17		/// Initializes a new instance of the <see cref="DateTimeTicksProvider"/> class.
 18		/// </summary>
 19		public DateTimeTicksProvider() { }
 20
 21		static DateTimeTicksProvider()
 22		{
 23			Providers.Add(DifferenceIn.Year, new YearDateTimeProvider());
 24			Providers.Add(DifferenceIn.Month, new MonthDateTimeProvider());
 25			Providers.Add(DifferenceIn.Day, new DayDateTimeProvider());
 26			Providers.Add(DifferenceIn.Hour, new HourDateTimeProvider());
 27			Providers.Add(DifferenceIn.Minute, new MinuteDateTimeProvider());
 28			Providers.Add(DifferenceIn.Second, new SecondDateTimeProvider());
 29			Providers.Add(DifferenceIn.Millisecond, new MillisecondDateTimeProvider());
 30
 31			MinorProviders.Add(DifferenceIn.Year, new MinorDateTimeProvider(new YearDateTimeProvider()));
 32			MinorProviders.Add(DifferenceIn.Month, new MinorDateTimeProvider(new MonthDateTimeProvider()));
 33			MinorProviders.Add(DifferenceIn.Day, new MinorDateTimeProvider(new DayDateTimeProvider()));
 34			MinorProviders.Add(DifferenceIn.Hour, new MinorDateTimeProvider(new HourDateTimeProvider()));
 35			MinorProviders.Add(DifferenceIn.Minute, new MinorDateTimeProvider(new MinuteDateTimeProvider()));
 36			MinorProviders.Add(DifferenceIn.Second, new MinorDateTimeProvider(new SecondDateTimeProvider()));
 37			MinorProviders.Add(DifferenceIn.Millisecond, new MinorDateTimeProvider(new MillisecondDateTimeProvider()));
 38		}
 39
 40		protected sealed override TimeSpan GetDifference(DateTime start, DateTime end)
 41		{
 42			return end - start;
 43		}
 44	}
 45
 46	internal static class DateTimeArrayExtensions
 47	{
 48		internal static int GetIndex(this DateTime[] array, DateTime value)
 49		{
 50			for (int i = 0; i < array.Length - 1; i++)
 51			{
 52				if (array[i] <= value && value < array[i + 1])
 53					return i;
 54			}
 55
 56			return array.Length - 1;
 57		}
 58	}
 59
 60	internal sealed class MinorDateTimeProvider : MinorTimeProviderBase<DateTime>
 61	{
 62		public MinorDateTimeProvider(ITicksProvider<DateTime> owner) : base(owner) { }
 63
 64		protected override bool IsInside(DateTime value, Range<DateTime> range)
 65		{
 66			return range.Min < value && value < range.Max;
 67		}
 68	}
 69
 70	internal sealed class YearDateTimeProvider : DatePeriodTicksProvider
 71	{
 72		protected override DifferenceIn GetDifferenceCore()
 73		{
 74			return DifferenceIn.Year;
 75		}
 76
 77		protected override int[] GetTickCountsCore()
 78		{
 79			return new int[] { 20, 10, 5, 4, 2, 1 };
 80		}
 81
 82		protected override int GetSpecificValue(DateTime start, DateTime dt)
 83		{
 84			return dt.Year;
 85		}
 86
 87		protected override DateTime GetStart(DateTime start, int value, int step)
 88		{
 89			int year = start.Year;
 90			int newYear = (year / step) * step;
 91			if (newYear == 0) newYear = 1;
 92
 93			return new DateTime(newYear, 1, 1);
 94		}
 95
 96		protected override bool IsMinDate(DateTime dt)
 97		{
 98			return dt.Year == DateTime.MinValue.Year;
 99		}
100
101		protected override DateTime AddStep(DateTime dt, int step)
102		{
103			if (dt.Year + step > DateTime.MaxValue.Year)
104				return DateTime.MaxValue;
105
106			return dt.AddYears(step);
107		}
108	}
109
110	internal sealed class MonthDateTimeProvider : DatePeriodTicksProvider
111	{
112		protected override DifferenceIn GetDifferenceCore()
113		{
114			return DifferenceIn.Month;
115		}
116
117		protected override int[] GetTickCountsCore()
118		{
119			return new int[] { 12, 6, 4, 3, 2, 1 };
120		}
121
122		protected override int GetSpecificValue(DateTime start, DateTime dt)
123		{
124			return dt.Month + (dt.Year - start.Year) * 12;
125		}
126
127		protected override DateTime GetStart(DateTime start, int value, int step)
128		{
129			return new DateTime(start.Year, 1, 1);
130		}
131
132		protected override bool IsMinDate(DateTime dt)
133		{
134			return dt.Month == DateTime.MinValue.Month;
135		}
136
137		protected override DateTime AddStep(DateTime dt, int step)
138		{
139			return dt.AddMonths(step);
140		}
141	}
142
143	internal sealed class DayDateTimeProvider : DatePeriodTicksProvider
144	{
145		protected override DifferenceIn GetDifferenceCore()
146		{
147			return DifferenceIn.Day;
148		}
149
150		protected override int[] GetTickCountsCore()
151		{
152			return new int[] { 30, 15, 10, 5, 2, 1 };
153		}
154
155		protected override int GetSpecificValue(DateTime start, DateTime dt)
156		{
157			return (dt - start).Days;
158		}
159
160		protected override DateTime GetStart(DateTime start, int value, int step)
161		{
162			return start.Date;
163		}
164
165		protected override bool IsMinDate(DateTime dt)
166		{
167			return dt.Day == 1;
168		}
169
170		protected override DateTime AddStep(DateTime dt, int step)
171		{
172			return dt.AddDays(step);
173		}
174	}
175
176	internal sealed class HourDateTimeProvider : DatePeriodTicksProvider
177	{
178		protected override DifferenceIn GetDifferenceCore()
179		{
180			return DifferenceIn.Hour;
181		}
182
183		protected override int[] GetTickCountsCore()
184		{
185			return new int[] { 24, 12, 6, 4, 3, 2, 1 };
186		}
187
188		protected override int GetSpecificValue(DateTime start, DateTime dt)
189		{
190			return (int)(dt - start).TotalHours;
191		}
192
193		protected override DateTime GetStart(DateTime start, int value, int step)
194		{
195			return start.Date;
196		}
197
198		protected override bool IsMinDate(DateTime dt)
199		{
200			return false;
201		}
202
203		protected override DateTime AddStep(DateTime dt, int step)
204		{
205			return dt.AddHours(step);
206		}
207	}
208
209	internal sealed class MinuteDateTimeProvider : DatePeriodTicksProvider
210	{
211		protected override DifferenceIn GetDifferenceCore()
212		{
213			return DifferenceIn.Minute;
214		}
215
216		protected override int[] GetTickCountsCore()
217		{
218			return new int[] { 60, 30, 20, 15, 10, 5, 4, 3, 2 };
219		}
220
221		protected override int GetSpecificValue(DateTime start, DateTime dt)
222		{
223			return (int)(dt - start).TotalMinutes;
224		}
225
226		protected override DateTime GetStart(DateTime start, int value, int step)
227		{
228			return start.Date.AddHours(start.Hour);
229		}
230
231		protected override bool IsMinDate(DateTime dt)
232		{
233			return false;
234		}
235
236		protected override DateTime AddStep(DateTime dt, int step)
237		{
238			return dt.AddMinutes(step);
239		}
240	}
241
242	internal sealed class SecondDateTimeProvider : DatePeriodTicksProvider
243	{
244		protected override DifferenceIn GetDifferenceCore()
245		{
246			return DifferenceIn.Second;
247		}
248
249		protected override int[] GetTickCountsCore()
250		{
251			return new int[] { 60, 30, 20, 15, 10, 5, 4, 3, 2 };
252		}
253
254		protected override int GetSpecificValue(DateTime start, DateTime dt)
255		{
256			return (int)(dt - start).TotalSeconds;
257		}
258
259		protected override DateTime GetStart(DateTime start, int value, int step)
260		{
261			return start.Date.AddHours(start.Hour).AddMinutes(start.Minute);
262		}
263
264		protected override bool IsMinDate(DateTime dt)
265		{
266			return false;
267		}
268
269		protected override DateTime AddStep(DateTime dt, int step)
270		{
271			return dt.AddSeconds(step);
272		}
273	}
274
275	internal sealed class MillisecondDateTimeProvider : DatePeriodTicksProvider
276	{
277		protected override DifferenceIn GetDifferenceCore()
278		{
279			return DifferenceIn.Millisecond;
280		}
281
282		protected override int[] GetTickCountsCore()
283		{
284			return new int[] { 100, 50, 40, 25, 20, 10, 5, 4, 2 };
285		}
286
287		protected override int GetSpecificValue(DateTime start, DateTime dt)
288		{
289			return (int)(dt - start).TotalMilliseconds;
290		}
291
292		protected override DateTime GetStart(DateTime start, int value, int step)
293		{
294			return start.Date.AddHours(start.Hour).AddMinutes(start.Minute).AddSeconds(start.Second);
295		}
296
297		protected override bool IsMinDate(DateTime dt)
298		{
299			return false;
300		}
301
302		protected override DateTime AddStep(DateTime dt, int step)
303		{
304			return dt.AddMilliseconds(step);
305		}
306	}
307}