Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Can anybody plz tell me how to detect day changes using c++.My requirement is that the application should get notified when the system date is changed. because the application needs to do something when the date changes.

Thanks,
Dev.
Posted

There is no callback service of Windows that would give you an event at midnight. So you will have to ask regularly for the local time (e.g. by the time() function) and check for the change of the day number.

time_t t0 = time(0);
...

// detect midnight -- call regularly
time_t t1 = time(0);

int d0 = localtime (&t0)->tm_yday;
int d1 = localtime (&t1)->tm_yday;
t0 = t1;
if (d0 != d1)
{
   // detected midnight
   ...
}


localtime is a relatively time consuming function. If speed is a major issue for that test, you might want to optimize that code by deriving the the day-number manually from time_t by subtracting the time-zone offset in seconds and calculating the modulus by 86400 (the number of seconds per day). But this requires some care as the the time-zone and DST offset might change.
 
Share this answer
 
Comments
Sergey Chepurin 12-Apr-12 15:20pm    
When you talk about time registration or time changing events there is no such thing as "speed issue", but rather precision. There is no time (or processor) consuming issue either, as he can use Sleep() for 1 second as a timer.
Well, in general, you will need to manage noticing the transition yourself. The "gross" way to do it as suggested above, get an initial date value, periodically get another date value and check to see if it changed. The frequency of how often you check or in what application loop you check will have an impact on your code's performance.

The other way is to create a thread that determines when the next occurrance of "midnight" happens and then trigger / signal and event / semaphore. Or, have that thread do the work that is necessary when the day changes.

In Windows, you'd compute "next midnight" something like this
int midnight_sleep;
SYSTEMTIME cur_time;

// compute milliseconds until 1/2 second past midnight
GetLocalTime(&cur_time);
midnight_sleep = ((23 - cur_time.wHour) * 60 * 60 * 1000) +	
		 ((59 - cur_time.wMinute) * 60 * 1000) +
		 ((59 - cur_time.wSecond) * 1000) +
		 (1500 - cur_time.wMilliseconds);


Of course the above code doesn't get it exactly right if Daylight Savings Time happens (or stops happening) during the day or someone sets the system time.

In fact, if someone changes the system date, you wouldn't detect that immediately either.

So, if you care about "Exactly when the Date itself changes", you need to do the brute force method of frequently comparing the current date to a previously saved known date. And "Frequently" can be every minute or finer, depending on how much you care about it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900