time_traits.hpp 2.2 KB

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