Skip to content

Commit

Permalink
fix timer where it wouldn't log each hour
Browse files Browse the repository at this point in the history
  • Loading branch information
dylan-george-field committed Jan 15, 2023
1 parent cc35808 commit ebcbfb8
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/wfh-log-wpf/Timer/HourlyTimer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,33 @@ public HourlyTimer()
timer.AutoReset = true;
timer.Enabled = true;

int minutes = DateTime.Now.Minute;
int adjust = 10 - minutes % 1;
timer.Interval = adjust * 60 * 1000;
timer.Interval = GetInterval();
}

public void Start()
{
timer.Start();
}

public static double GetInterval()
{
var timeOfDay = DateTime.Now.TimeOfDay;
var roundedUpToHour = Math.Ceiling(timeOfDay.TotalHours);
var nextFullHour = TimeSpan.FromHours(roundedUpToHour);
var delta = (nextFullHour - timeOfDay).TotalMilliseconds;

return delta;
}

public void AddHandler(ElapsedEventHandler function)
{
timer.Elapsed += function;
timer.Elapsed += ResetInterval;
}

private void ResetInterval(object? source, ElapsedEventArgs e)
{
timer.Interval = GetInterval();
}

public void Stop()
Expand Down

0 comments on commit ebcbfb8

Please sign in to comment.