Opened 13 years ago

Closed 13 years ago

#3312 closed Bugs (invalid)

boost::asio::deadline_timer.expires_from_now() ignores local time

Reported by: victor.v.lukashov@… Owned by: chris_kohlhoff
Milestone: Boost 1.40.0 Component: asio
Version: Boost 1.39.0 Severity: Problem
Keywords: Cc:

Description

The expire_from_now() method assumes that 'now' is UTC time which is completely discouraging.

Consider the following sample program:

#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

int main(int argc, const char* argv[])
{
	using namespace std;
	using namespace boost::asio;
	using namespace boost::posix_time;

	io_service service;
	deadline_timer timer(service);

	cout << "current UTC time is '" << to_simple_string(microsec_clock::universal_time()) << "'" << endl;
	cout << "current local time is '" << to_simple_string(microsec_clock::local_time()) << "'" << endl;
	timer.expires_from_now(minutes(1));
	cout << "timer is set to expire at '" << to_simple_string(timer.expires_at()) << "'" << endl;

	return 0;
};

It produces the follwing output:

current UTC time is '2009-Aug-03 12:11:41.775770'
current local time is '2009-Aug-03 19:11:41.782770'
timer is set to expire at '2009-Aug-03 12:12:41.791770'

I believe the output should be:

current UTC time is '2009-Aug-03 12:11:41.775770'
current local time is '2009-Aug-03 19:11:41.782770'
timer is set to expire at '2009-Aug-03 19:12:41.791770'

Change History (1)

comment:1 by chris_kohlhoff, 13 years ago

Resolution: invalid
Status: newclosed

The deadline_timer typedef is based on a UTC clock, and all operations (expires_at, expires_from_now) work in UTC time. If you want it to use a different clock (such as the local time clock), you can use basic_deadline_timer<> with your own traits class. Please see the "Timers" example.

Note: See TracTickets for help on using tickets.