I'm writing a software that is very sensitive to local time. I'm using boost::posix_time to get the local time of the computer. However, I noticed that if we change the time and the timezone during the execution, the local time gets totally wrong. Here is a small test program I wrote:
#include <boost/date_time/posix_time/posix_time.hpp>
#include <stdio.h>
int main(int argc, char* argv[])
{
while(1)
{
std::cout << "It's : " << boost::posix_time::to_simple_string(
boost::posix_time::second_clock::local_time()) << std::endl;
sleep(1);
}
return 0;
}
Then I start the application, the time is correct:
./testDate
It's : 2013-Apr-22 10:35:22
Then I change the time and the timezone during it's execution, and the time got totally wrong!
rm /etc/localtime ; ln -s /usr/share/zoneinfo/Europe/Belfast /etc/localtime ; date -s 10:35:00
It's : 2013-Apr-22 10:35:16
It's : 2013-Apr-22 10:35:17 <-- Time changed here
It's : 2013-Apr-22 04:35:00
It's : 2013-Apr-22 04:35:01
But if I manually restart the application, the time is correct again! I was wondering if there is a way to tell boost to refresh the date time from the system clock? I guess it reads it once at startup and stores the time in memory. And how could I detect that the timezone or the time was changed and I have to tell boost to update the time? Check if the time is significantly different from the last time I got the time by regularly asking the time?