local_time seems not monotonic
The following snippet seems to generate non monotonic local_date. I'm using boost 1.55 on linux. #include <boost/date_time/local_time/local_time.hpp> #include <boost/ptr_container/ptr_vector.hpp> int main() { const boost::local_time::time_zone_ptr theTimeZone( new boost::local_time::posix_time_zone( "CET+01CEST+01,M3.5.0/02:00,M10.5.0/03:00") ); boost::local_time::local_date_time myOldValue( boost::local_time::local_microsec_clock::local_time(theTimeZone)); for (size_t i = 0; ; ++i) { const boost::local_time::local_date_time myLocalTime = boost::local_time::local_microsec_clock::local_time(theTimeZone); if (myLocalTime < myOldValue) { std::cout << myOldValue << std::endl; std::cout << myLocalTime << std::endl; std::cout << boost::local_time::local_microsec_clock::local_time(theTimeZone) << std::endl; std::cout << "====================" << std::endl; } myOldValue = myLocalTime; } } As you can see the program is not supposed to print nothing ever, however this is what I'm getting: 2014-Jul-31 00:24:56.005625 CEST 2014-Jul-31 00:24:55.005631 CEST 2014-Jul-31 00:24:56.005946 CEST ==================== 2014-Jul-31 00:24:58.005625 CEST 2014-Jul-31 00:24:57.005629 CEST 2014-Jul-31 00:24:58.005824 CEST ==================== 2014-Jul-31 00:25:02.005624 CEST 2014-Jul-31 00:25:01.005628 CEST 2014-Jul-31 00:25:02.005838 CEST ==================== 2014-Jul-31 00:25:04.005625 CEST 2014-Jul-31 00:25:03.005630 CEST 2014-Jul-31 00:25:04.005826 CEST ==================== 2014-Jul-31 00:25:06.005624 CEST 2014-Jul-31 00:25:05.005633 CEST 2014-Jul-31 00:25:06.005853 CEST ==================== 2014-Jul-31 00:25:07.005625 CEST 2014-Jul-31 00:25:06.005631 CEST 2014-Jul-31 00:25:07.005846 CEST ==================== 2014-Jul-31 00:25:12.005625 CEST 2014-Jul-31 00:25:11.005631 CEST 2014-Jul-31 00:25:12.005822 CEST ==================== as you can see when the local_date is near 0.005631 second fraction it goes back of one second and then forward again on the following call. Am I missing something ? Regards Gaetano Mendola
Hi Gaetano and all, Gaetano Mendola <mendola <at> gmail.com> writes:
The following snippet seems to generate non monotonic local_date.
I'm using boost 1.55 on linux. [CUT] Am I missing something ?
some further investigation on this issue seemed to show that the problem is the following: The static time_type local_time(shared_ptr<time_zone_type> tz_ptr) function, computes the local time by executing, in short sequence, these two instructions utc_time_type utc_time = second_clock::universal_time(); time_duration_type utc_offset = second_clock::local_time() - utc_time; Both the local_time() and universal_time() calls get a time information static time_type local_time() { ::std::time_t t; ::std::time(&t); ::std::tm curr, *curr_ptr; //curr_ptr = ::std::localtime(&t); curr_ptr = c_time::localtime(&t, &curr); return create_time(curr_ptr); } static time_type universal_time() { ::std::time_t t; ::std::time(&t); ::std::tm curr, *curr_ptr; //curr_ptr = ::std::gmtime(&t); curr_ptr = c_time::gmtime(&t, &curr); return create_time(curr_ptr); } and finally invoke the create_time() function. static time_type create_time(::std::tm* current) { date_type d(static_cast<unsigned short>(current->tm_year + 1900), static_cast<unsigned short>(current->tm_mon + 1), static_cast<unsigned short>(current->tm_mday)); time_duration_type td(current->tm_hour, current->tm_min, current->tm_sec); return time_type(d,td); } create_time() simply builds a time information with a sec granularity (ms are NOT considered). So, let's assume that universal_time() gets called at PM 1:00:00_989ms UTC local_time() gets called at PM 3:00:01_002ms UTC+2 (so actually 13 ms later) Then, create_time() truncates the milliseconds information and the utc_offset gets a +1 seconds offset with respect to the correct value. Antonio
On 7/31/2014 1:37 AM, Antonio Mancina wrote:
Hi Gaetano and all,
Gaetano Mendola <mendola <at> gmail.com> writes:
The following snippet seems to generate non monotonic local_date.
I'm using boost 1.55 on linux. [CUT] Am I missing something ?
some further investigation on this issue seemed to show that the problem is the following:
The
static time_type local_time(shared_ptr<time_zone_type> tz_ptr)
function, computes the local time by executing, in short sequence, these two instructions
utc_time_type utc_time = second_clock::universal_time(); time_duration_type utc_offset = second_clock::local_time() - utc_time;
Both the local_time() and universal_time() calls get a time information
static time_type local_time() { ::std::time_t t; ::std::time(&t); ::std::tm curr, *curr_ptr; //curr_ptr = ::std::localtime(&t); curr_ptr = c_time::localtime(&t, &curr); return create_time(curr_ptr); }
static time_type universal_time() {
::std::time_t t; ::std::time(&t); ::std::tm curr, *curr_ptr; //curr_ptr = ::std::gmtime(&t); curr_ptr = c_time::gmtime(&t, &curr); return create_time(curr_ptr); }
and finally invoke the create_time() function.
static time_type create_time(::std::tm* current) { date_type d(static_cast<unsigned short>(current->tm_year + 1900), static_cast<unsigned short>(current->tm_mon + 1), static_cast<unsigned short>(current->tm_mday)); time_duration_type td(current->tm_hour, current->tm_min, current->tm_sec); return time_type(d,td); }
create_time() simply builds a time information with a sec granularity (ms are NOT considered).
So, let's assume that
universal_time() gets called at PM 1:00:00_989ms UTC local_time() gets called at PM 3:00:01_002ms UTC+2 (so actually 13 ms later)
Then, create_time() truncates the milliseconds information and the utc_offset gets a +1 seconds offset with respect to the correct value.
Please file a bug in our tracker: https://svn.boost.org/trac/boost/. Thanks. -- Eric Niebler Boost.org http://www.boost.org
participants (3)
-
Antonio Mancina
-
Eric Niebler
-
Gaetano Mendola