Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/Charts/Axes/DateTimeTicksProviderBase.cs @ 13749

Last change on this file since 13749 was 12503, checked in by aballeit, 9 years ago

#2283 added GUI and charts; fixed MCTS

File size: 3.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
6
7namespace Microsoft.Research.DynamicDataDisplay.Charts.NewAxis
8{
9  public abstract class DateTimeTicksProviderBase : ITicksProvider<DateTime>
10  {
11    public abstract ITicksInfo<DateTime> GetTicks(Range<DateTime> range, int ticksCount);
12
13    public abstract int DecreaseTickCount(int ticksCount);
14
15    public abstract int IncreaseTickCount(int ticksCount);
16
17    protected static DifferenceIn GetDifference(TimeSpan span)
18    {
19      // for negative time spans
20      span = span.Duration();
21
22      DifferenceIn diff;
23      if (span.Days > 365)
24        diff = DifferenceIn.Year;
25      else if (span.Days > 30)
26        diff = DifferenceIn.Month;
27      else if (span.Days > 0)
28        diff = DifferenceIn.Day;
29      else if (span.Hours > 0)
30        diff = DifferenceIn.Hour;
31      else if (span.Minutes > 0)
32        diff = DifferenceIn.Minute;
33      else if (span.Seconds > 0)
34        diff = DifferenceIn.Second;
35      else
36        diff = DifferenceIn.Millisecond;
37
38      return diff;
39    }
40
41    protected static DateTime Shift(DateTime dateTime, DifferenceIn diff)
42    {
43      DateTime res = dateTime;
44
45      switch (diff)
46      {
47        case DifferenceIn.Year:
48          res = res.AddYears(1);
49          break;
50        case DifferenceIn.Month:
51          res = res.AddMonths(1);
52          break;
53        case DifferenceIn.Day:
54          res = res.AddDays(1);
55          break;
56        case DifferenceIn.Hour:
57          res = res.AddHours(1);
58          break;
59        case DifferenceIn.Minute:
60          res = res.AddMinutes(1);
61          break;
62        case DifferenceIn.Second:
63          res = res.AddSeconds(1);
64          break;
65        case DifferenceIn.Millisecond:
66          res = res.AddMilliseconds(1);
67          break;
68        default:
69          break;
70      }
71
72      return res;
73    }
74
75    protected static DateTime RoundDown(DateTime dateTime, DifferenceIn diff)
76    {
77      DateTime res = dateTime;
78
79      switch (diff)
80      {
81        case DifferenceIn.Year:
82          res = new DateTime(dateTime.Year, 1, 1);
83          break;
84        case DifferenceIn.Month:
85          res = new DateTime(dateTime.Year, dateTime.Month, 1);
86          break;
87        case DifferenceIn.Day:
88          res = dateTime.Date;
89          break;
90        case DifferenceIn.Hour:
91          res = dateTime.Date.AddHours(dateTime.Hour);
92          break;
93        case DifferenceIn.Minute:
94          res = dateTime.Date.AddHours(dateTime.Hour).AddMinutes(dateTime.Minute);
95          break;
96        case DifferenceIn.Second:
97          res = dateTime.Date.AddHours(dateTime.Hour).AddMinutes(dateTime.Minute).AddSeconds(dateTime.Second);
98          break;
99        case DifferenceIn.Millisecond:
100          res = dateTime.Date.AddHours(dateTime.Hour).AddMinutes(dateTime.Minute).AddSeconds(dateTime.Second).AddMilliseconds(dateTime.Millisecond);
101          break;
102        default:
103          break;
104      }
105
106      DebugVerify.Is(res <= dateTime);
107
108      return res;
109    }
110
111    protected static DateTime RoundUp(DateTime dateTime, DifferenceIn diff)
112    {
113      DateTime res = RoundDown(dateTime, diff);
114
115      switch (diff)
116      {
117        case DifferenceIn.Year:
118          res = res.AddYears(1);
119          break;
120        case DifferenceIn.Month:
121          res = res.AddMonths(1);
122          break;
123        case DifferenceIn.Day:
124          res = res.AddDays(1);
125          break;
126        case DifferenceIn.Hour:
127          res = res.AddHours(1);
128          break;
129        case DifferenceIn.Minute:
130          res = res.AddMinutes(1);
131          break;
132        case DifferenceIn.Second:
133          res = res.AddSeconds(1);
134          break;
135        case DifferenceIn.Millisecond:
136          res = res.AddMilliseconds(1);
137          break;
138        default:
139          break;
140      }
141
142      return res;
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.