65.9K
CodeProject is changing. Read more.
Home

Midnight Timer - A Way to Detect When it is Midnight

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.85/5 (30 votes)

Mar 30, 2007

CPOL

4 min read

viewsIcon

93848

downloadIcon

1605

An article on how to detect when it is midnight

Introduction

I'm sure that a large percentage of developers have wished for a way to detect when it is midnight on a machine and execute code which updates something simple and non complex (i.e. display dates, instead of "Today" you would want it to show "Yesterday" and so forth).

I pondered over this for some time until I had inspiration one late night. Let's use a Timer to do it, but with some intelligence.

Background

While working on a larger project, I needed a way to ensure that an event would fire which would execute code to update the displayed days (much how Outlook displays emails by date) at midnight.

I thought of various methods to accomplish this. Check the datetime every second.. no, Check the time ever x hours... no... Just loop until midnight is reached... no!

Each way I thought of included heavy looping or polling in order to check the time to know when it turns midnight. So a new approach was needed.

I realised that I could use a system Timer to achieve what I wanted without reinventing the wheel.

Using the Code

I've designed the code in a simplistic way in order for you to use the code more effectively.

I have contained the logic in a single class, which has 3 public method, Start(), Restart() and Stop().

When the class is first executed via the Start() method, the code obtains a datetime of the current time, then gets a datetime object of midnight and subtracts the 2 times in order to give a time until it is midnight.

With this time, it sets the timer interval and starts the timer. Now when the interval is reached and the timer fires its event, I reset the timer using the same process. This will make the interval for 24. When this timer expires, it is then reset and repeated indefinitely.

To use the class in your code, simply add these lines.

Insert the following into the method you wish to call the Timer from:

// Create the instance of the timer
MidnightTimer m_MidnightTimer = new MidnightTimer();

// Set the Event Handler to be fired when it is midnight
m_MidnightTimer.TimeReached += new TimeReachedEventHandler(this.m_MidnightTimer_TimeReached);

// Start the Midnight Timer
m_MidnightTimer.Start();

Add the Event Handler method:

private void m_MidnightTimer_TimeReached(DateTime Time)
{
    // do something here...
}

That's it! The timer will now execute the method m_MidnightTimer_TimeReached at Midnight.

Behind the scenes

Let's look at the main piece behind this code:

public void Start()
{           
    // Subtract the current time, from midnight (tomorrow).
    // This will return a value, which will be used to set the Timer interval
    TimeSpan ts = this.GetMidnight(s_MinutesAfterMidnight).Subtract(DateTime.Now);

    // We only want the Hours, Minutes and Seconds until midnight
    TimeSpan tsMidnight = new TimeSpan(ts.Hours, ts.Minutes, ts.Seconds);

    // Create the Timer
    s_timer = new Timer(tsMidnight.TotalMilliseconds);

    // Set the event handler
    s_timer.Elapsed += new ElapsedEventHandler(this.timer_Elapsed);

    // Hook into when Windows Time changes
    // Thanks to Nicole1982 for the suggestion & BruceN for the help
    Microsoft.Win32.SystemEvents.TimeChanged += new EventHandler(this.WindowsTimeChangeHandler);

    // Start the timer
    s_timer.Start();
}

private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    // Stop the orginial timer
    s_timer.Stop(); // swapped order thanks to Jeremy

    // now raise a event that the timer has elapsed
    OnTimeReached(); // swapped order thanks to Jeremy

    // reset the timer
    this.Start();
}

 

Windows Time Change

Thanks to Nicole1982 and BruceN for this (see the comments).

When the time of Windows is changed, the timer picks it up and automatically resets for you.

This is done via the Microsoft.Win32.SystemEvents.TimeChanged event in the Start() method which invokes the WindowsTimeChangeHandlerWindowsTimeChangeHandler method, which simply calls the Restart() method.

private void WindowsTimeChangeHandler(object sender, EventArgs e)
{
    // Please see https://connect.microsoft.com/VisualStudio/feedback/details/776003/systemevent-timechanged-is-fired-twice
    // The event is fired twice.. I assume 'once' for the change from the old system time and 'once' when the time has been changed.
    // i.e Event is fired when System time has Changed and is Changing

    // Restart the timer -> note as above, this is called twice
    this.Restart();
}
    

Note: This is a known issue with the Microsoft.Win32.SystemEvents.TimeChanged firing twice when the system is changed as outlined at https://connect.microsoft.com/VisualStudio/feedback/details/776003/systemevent-timechanged-is-fired-twice

Inside GetMidnight

As you can guess, the GetMidnight, simply returns midnight. Additionally the method allows for the supply of how many minutes after midnight you have

private DateTime GetMidnight(int MinutesAfterMidnight)
{
    // Lets work out the next occurring midnight
    // Add 1 day and use hours 0, min 0 and second 0 (remember this is 24 hour time)

    // Thanks to Yashar for this code/fix
    DateTime Tomorrow = DateTime.Now.AddDays(1);

    // Return a datetime for Tomorrow, but with how many minutes after midnight
    return new DateTime(Tomorrow.Year, Tomorrow.Month, Tomorrow.Day, 0, MinutesAfterMidnight, 0);
}

What the Start() method actually does is obtain the current time and midnight of the next day (DateTime.Now.Day+1) and subtracts them to give us how many hours, minutes, seconds until midnight occurs. This is the value we use to set the initial timer to fire at midnight.

Now after a few hours pass by and it hits midnight, the timer fires and recalls Start() (as well as stops the original timer, etc.) and repeats the process of getting the time now, and the time of the next midnight (in this case, 24 hours) and resets the timer.

Minutes after Midnight

The timer also supports supplying how many minutes after midnights you would like it to fire at.

To enable this, simply supply how many minutes via an integer parameter in the midnight timer constructor overload.

// Create the instance of the timer which will fire 10 minutes after midnight
MidnightTimer m_MidnightTimer = new MidnightTimer(10);

// Set the Event Handler to be fired when it is midnight
m_MidnightTimer.TimeReached += new TimeReachedEventHandler(this.m_MidnightTimer_TimeReached);

// Start the Midnight Timer
m_MidnightTimer.Start();

Version 2.0 Note

Recently I was involved in a project in which I needed this code again and having read the comments I took them on board and applied them to the code. Thanks to everyone who left constructive feedback, bug fixes and pointed me in the right direction to enhance this small but simple solution.

8 years is a long time! o_0 Happy coding!

Feedback

Feedback is most welcome! In fact I insist on it!

If you have any improvements or ideas on how to improve this code, please speak up and voice your opinion! It's a great way to learn!

History

  • 1.0.0.0 - 30/03/2007 - Initial version
  • 2.0.0.0 31/03/2015 - Bug fixes, Windows Timer Hook, More comments, updated article to reflect code changes