File size: 2,649 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;

namespace Microsoft.Management.UI.Internal
{
    /// <summary>
    /// The DateTimeApproximationComparer is responsible for comparing two
    /// DateTime objects at a level of precision determined by
    /// the first object. The comparison either compares at the
    /// date level or the date and time (down to Seconds precision).
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.MSInternal", "CA903:InternalNamespaceShouldNotContainPublicTypes")]
    public class DateTimeApproximationComparer : IComparer<DateTime>
    {
        /// <summary>
        /// Compares two objects and returns a value indicating
        /// whether one is less than, equal to, or greater than the other.
        /// </summary>
        /// <param name="value1">
        /// The first object to compare.
        /// </param>
        /// <param name="value2">
        /// The second object to compare.
        /// </param>
        /// <returns>
        /// If value1 is less than value2, then a value less than zero is returned.
        /// If value1 equals value2, than zero is returned.
        /// If value1 is greater than value2, then a value greater than zero is returned.
        /// </returns>
        public int Compare(DateTime value1, DateTime value2)
        {
            DateTime roundedX;
            DateTime roundedY;
            GetRoundedValues(value1, value2, out roundedX, out roundedY);

            return roundedX.CompareTo(roundedY);
        }

        private static void GetRoundedValues(DateTime value1, DateTime value2, out DateTime roundedValue1, out DateTime roundedValue2)
        {
            roundedValue1 = value1;
            roundedValue2 = value2;

            bool hasTimeComponent = HasTimeComponent(value1);

            int hour = hasTimeComponent ? value1.Hour : value2.Hour;
            int minute = hasTimeComponent ? value1.Minute : value2.Minute;
            int second = hasTimeComponent ? value1.Second : value2.Second;

            roundedValue1 = new DateTime(value1.Year, value1.Month, value1.Day, hour, minute, second);
            roundedValue2 = new DateTime(value2.Year, value2.Month, value2.Day, value2.Hour, value2.Minute, value2.Second);
        }

        private static bool HasTimeComponent(DateTime value)
        {
            bool hasNoTimeComponent = value.Hour == 0
                && value.Minute == 0
                && value.Second == 0
                && value.Millisecond == 0;

            return !hasNoTimeComponent;
        }
    }
}