time_traits.hpp 2.5 KB

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