time_traits.hpp 2.3 KB

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