5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #include <boost/asio.hpp>
18 typedef std::time_t time_type;
23 duration_type() : value(0) {}
24 duration_type(std::time_t v) : value(v) {}
28 // Get the current time.
29 static time_type now()
34 // Add a duration to a time.
35 static time_type add(const time_type& t, const duration_type& d)
40 // Subtract one time from another.
41 static duration_type subtract(const time_type& t1, const time_type& t2)
43 return duration_type(t1 - t2);
46 // Test whether one time is less than another.
47 static bool less_than(const time_type& t1, const time_type& t2)
52 // Convert to POSIX duration type.
53 static boost::posix_time::time_duration to_posix_duration(
54 const duration_type& d)
56 return boost::posix_time::seconds(d.value);
60 typedef boost::asio::basic_deadline_timer<
61 std::time_t, time_t_traits> time_t_timer;
63 void handle_timeout(const boost::system::error_code&)
65 std::cout << "handle_timeout\n";
72 boost::asio::io_service io_service;
74 time_t_timer timer(io_service);
76 timer.expires_from_now(5);
77 std::cout << "Starting synchronous wait\n";
79 std::cout << "Finished synchronous wait\n";
81 timer.expires_from_now(5);
82 std::cout << "Starting asynchronous wait\n";
83 timer.async_wait(&handle_timeout);
85 std::cout << "Finished asynchronous wait\n";
87 catch (std::exception& e)
89 std::cout << "Exception: " << e.what() << "\n";