Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/Charts/Axes/DateTime/Strategies/ExtendedDaysStrategy.cs @ 12747

Last change on this file since 12747 was 12503, checked in by aballeit, 10 years ago

#2283 added GUI and charts; fixed MCTS

File size: 1.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace Microsoft.Research.DynamicDataDisplay.Charts
7{
8  public class ExtendedDaysStrategy : IDateTimeTicksStrategy
9  {
10    private static readonly DifferenceIn[] diffs = new DifferenceIn[] {
11      DifferenceIn.Year,
12      DifferenceIn.Day,
13      DifferenceIn.Hour,
14      DifferenceIn.Minute,
15      DifferenceIn.Second,
16      DifferenceIn.Millisecond
17    };
18
19    public DifferenceIn GetDifference(TimeSpan span)
20    {
21      span = span.Duration();
22
23      DifferenceIn diff;
24      if (span.Days > 365)
25        diff = DifferenceIn.Year;
26      else if (span.Days > 0)
27        diff = DifferenceIn.Day;
28      else if (span.Hours > 0)
29        diff = DifferenceIn.Hour;
30      else if (span.Minutes > 0)
31        diff = DifferenceIn.Minute;
32      else if (span.Seconds > 0)
33        diff = DifferenceIn.Second;
34      else
35        diff = DifferenceIn.Millisecond;
36
37      return diff;
38    }
39
40    public bool TryGetLowerDiff(DifferenceIn diff, out DifferenceIn lowerDiff)
41    {
42      lowerDiff = diff;
43
44      int index = Array.IndexOf(diffs, diff);
45      if (index == -1)
46        return false;
47
48      if (index == diffs.Length - 1)
49        return false;
50
51      lowerDiff = diffs[index + 1];
52      return true;
53    }
54
55    public bool TryGetBiggerDiff(DifferenceIn diff, out DifferenceIn biggerDiff)
56    {
57      biggerDiff = diff;
58
59      int index = Array.IndexOf(diffs, diff);
60      if (index == -1 || index == 0)
61        return false;
62
63      biggerDiff = diffs[index - 1];
64      return true;
65    }
66  }
67}
Note: See TracBrowser for help on using the repository browser.