1 | // |
2 | // time_traits.hpp |
3 | // ~~~~~~~~~~~~~~~ |
4 | // |
5 | // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
6 | // |
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) |
9 | // |
10 | |
11 | #ifndef BOOST_ASIO_TIME_TRAITS_HPP |
12 | #define BOOST_ASIO_TIME_TRAITS_HPP |
13 | |
14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) |
15 | # pragma once |
16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) |
17 | |
18 | #include <boost/asio/detail/socket_types.hpp> // Must come before posix_time. |
19 | |
20 | #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME) \ |
21 | || defined(GENERATING_DOCUMENTATION) |
22 | |
23 | #include <boost/date_time/posix_time/posix_time_types.hpp> |
24 | |
25 | #include <boost/asio/detail/push_options.hpp> |
26 | |
27 | namespace boost { |
28 | namespace asio { |
29 | |
30 | /// Time traits suitable for use with the deadline timer. |
31 | template <typename Time> |
32 | struct time_traits; |
33 | |
34 | /// Time traits specialised for posix_time. |
35 | template <> |
36 | struct time_traits<boost::posix_time::ptime> |
37 | { |
38 | /// The time type. |
39 | typedef boost::posix_time::ptime time_type; |
40 | |
41 | /// The duration type. |
42 | typedef boost::posix_time::time_duration duration_type; |
43 | |
44 | /// Get the current time. |
45 | static time_type now() |
46 | { |
47 | #if defined(BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK) |
48 | return boost::posix_time::microsec_clock::universal_time(); |
49 | #else // defined(BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK) |
50 | return boost::posix_time::second_clock::universal_time(); |
51 | #endif // defined(BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK) |
52 | } |
53 | |
54 | /// Add a duration to a time. |
55 | static time_type add(const time_type& t, const duration_type& d) |
56 | { |
57 | return t + d; |
58 | } |
59 | |
60 | /// Subtract one time from another. |
61 | static duration_type subtract(const time_type& t1, const time_type& t2) |
62 | { |
63 | return t1 - t2; |
64 | } |
65 | |
66 | /// Test whether one time is less than another. |
67 | static bool less_than(const time_type& t1, const time_type& t2) |
68 | { |
69 | return t1 < t2; |
70 | } |
71 | |
72 | /// Convert to POSIX duration type. |
73 | static boost::posix_time::time_duration to_posix_duration( |
74 | const duration_type& d) |
75 | { |
76 | return d; |
77 | } |
78 | }; |
79 | |
80 | } // namespace asio |
81 | } // namespace boost |
82 | |
83 | #include <boost/asio/detail/pop_options.hpp> |
84 | |
85 | #endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME) |
86 | // || defined(GENERATING_DOCUMENTATION) |
87 | |
88 | #endif // BOOST_ASIO_TIME_TRAITS_HPP |
89 | |