
Class DateTimeTester

    Shared Function RoughlyEquals(ByVal time As DateTime, ByVal timeWithWindow As DateTime, ByVal windowInSeconds As Integer, ByVal frequencyInSeconds As Integer) As Boolean

        Dim delta As Long = (timeWithWindow.Subtract(time)).TotalSeconds Mod frequencyInSeconds

        If delta > windowInSeconds Then
            delta = frequencyInSeconds - delta
        End If


        Return Math.Abs(delta) < windowInSeconds

    End Function 'RoughlyEquals


    Public Shared Sub Main()

        Dim window As Integer = 10
        Dim freq As Integer = 60 * 60 * 2 ' 2 hours;
        Dim d1 As DateTime = DateTime.Now

        Dim d2 As DateTime = d1.AddSeconds((2 * window))
        Dim d3 As DateTime = d1.AddSeconds((-2 * window))
        Dim d4 As DateTime = d1.AddSeconds((window / 2))
        Dim d5 As DateTime = d1.AddSeconds((-window / 2))

        Dim d6 As DateTime = d1.AddHours(2).AddSeconds((2 * window))
        Dim d7 As DateTime = d1.AddHours(2).AddSeconds((-2 * window))
        Dim d8 As DateTime = d1.AddHours(2).AddSeconds((window / 2))
        Dim d9 As DateTime = d1.AddHours(2).AddSeconds((-window / 2))

        Console.WriteLine("d1 ~= d1 [true]: " + CStr(RoughlyEquals(d1, d1, window, freq)))
        Console.WriteLine("d1 ~= d2 [false]: " + CStr(RoughlyEquals(d1, d2, window, freq)))
        Console.WriteLine("d1 ~= d3 [false]: " + CStr(RoughlyEquals(d1, d3, window, freq)))
        Console.WriteLine("d1 ~= d4 [true]: " + CStr(RoughlyEquals(d1, d4, window, freq)))
        Console.WriteLine("d1 ~= d5 [true]: " + CStr(RoughlyEquals(d1, d5, window, freq)))

        Console.WriteLine("d1 ~= d6 [false]: " + CStr(RoughlyEquals(d1, d6, window, freq)))
        Console.WriteLine("d1 ~= d7 [false]: " + CStr(RoughlyEquals(d1, d7, window, freq)))
        Console.WriteLine("d1 ~= d8 [true]: " + CStr(RoughlyEquals(d1, d8, window, freq)))
        Console.WriteLine("d1 ~= d9 [true]: " + CStr(RoughlyEquals(d1, d9, window, freq)))

    End Sub 'Main 

End Class 'DateTimeTester
