class DateTimeTester { static bool RoughlyEquals(DateTime time, DateTime timeWithWindow, int windowInSeconds, int frequencyInSeconds) { long delta = (long)((TimeSpan)(timeWithWindow - time)).TotalSeconds % frequencyInSeconds; delta = delta > windowInSeconds ? frequencyInSeconds - delta : delta; return Math.Abs(delta) < windowInSeconds; } public static void Main() { int window = 10; int freq = 60 * 60 * 2; // 2 hours; DateTime d1 = DateTime.Now; DateTime d2 = d1.AddSeconds(2 * window); DateTime d3 = d1.AddSeconds(-2 * window); DateTime d4 = d1.AddSeconds(window / 2); DateTime d5 = d1.AddSeconds(-window / 2); DateTime d6 = (d1.AddHours(2)).AddSeconds(2 * window); DateTime d7 = (d1.AddHours(2)).AddSeconds(-2 * window); DateTime d8 = (d1.AddHours(2)).AddSeconds(window / 2); DateTime d9 = (d1.AddHours(2)).AddSeconds(-window / 2); Console.WriteLine("d1 ~= d1 [true]: " + RoughlyEquals(d1, d1, window, freq)); Console.WriteLine("d1 ~= d2 [false]: " + RoughlyEquals(d1, d2, window, freq)); Console.WriteLine("d1 ~= d3 [false]: " + RoughlyEquals(d1, d3, window, freq)); Console.WriteLine("d1 ~= d4 [true]: " + RoughlyEquals(d1, d4, window, freq)); Console.WriteLine("d1 ~= d5 [true]: " + RoughlyEquals(d1, d5, window, freq)); Console.WriteLine("d1 ~= d6 [false]: " + RoughlyEquals(d1, d6, window, freq)); Console.WriteLine("d1 ~= d7 [false]: " + RoughlyEquals(d1, d7, window, freq)); Console.WriteLine("d1 ~= d8 [true]: " + RoughlyEquals(d1, d8, window, freq)); Console.WriteLine("d1 ~= d9 [true]: " + RoughlyEquals(d1, d9, window, freq)); } }