conversion.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #ifndef POSIX_TIME_CONVERSION_HPP___
  2. #define POSIX_TIME_CONVERSION_HPP___
  3. /* Copyright (c) 2002-2005 CrystalClear Software, Inc.
  4. * Use, modification and distribution is subject to the
  5. * Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  7. * Author: Jeff Garland, Bart Garst
  8. * $Date$
  9. */
  10. #include <cstring>
  11. #include <boost/cstdint.hpp>
  12. #include <boost/date_time/posix_time/ptime.hpp>
  13. #include <boost/date_time/posix_time/posix_time_duration.hpp>
  14. #include <boost/date_time/filetime_functions.hpp>
  15. #include <boost/date_time/c_time.hpp>
  16. #include <boost/date_time/time_resolution_traits.hpp> // absolute_value
  17. #include <boost/date_time/gregorian/conversion.hpp>
  18. namespace boost {
  19. namespace posix_time {
  20. //! Function that converts a time_t into a ptime.
  21. inline
  22. ptime from_time_t(std::time_t t)
  23. {
  24. return ptime(gregorian::date(1970,1,1)) + seconds(static_cast<long>(t));
  25. }
  26. //! Function that converts a ptime into a time_t
  27. inline
  28. std::time_t to_time_t(ptime pt)
  29. {
  30. return (pt - ptime(gregorian::date(1970,1,1))).total_seconds();
  31. }
  32. //! Convert a time to a tm structure truncating any fractional seconds
  33. inline
  34. std::tm to_tm(const boost::posix_time::ptime& t) {
  35. std::tm timetm = boost::gregorian::to_tm(t.date());
  36. boost::posix_time::time_duration td = t.time_of_day();
  37. timetm.tm_hour = static_cast<int>(td.hours());
  38. timetm.tm_min = static_cast<int>(td.minutes());
  39. timetm.tm_sec = static_cast<int>(td.seconds());
  40. timetm.tm_isdst = -1; // -1 used when dst info is unknown
  41. return timetm;
  42. }
  43. //! Convert a time_duration to a tm structure truncating any fractional seconds and zeroing fields for date components
  44. inline
  45. std::tm to_tm(const boost::posix_time::time_duration& td) {
  46. std::tm timetm;
  47. std::memset(&timetm, 0, sizeof(timetm));
  48. timetm.tm_hour = static_cast<int>(date_time::absolute_value(td.hours()));
  49. timetm.tm_min = static_cast<int>(date_time::absolute_value(td.minutes()));
  50. timetm.tm_sec = static_cast<int>(date_time::absolute_value(td.seconds()));
  51. timetm.tm_isdst = -1; // -1 used when dst info is unknown
  52. return timetm;
  53. }
  54. //! Convert a tm struct to a ptime ignoring is_dst flag
  55. inline
  56. ptime ptime_from_tm(const std::tm& timetm) {
  57. boost::gregorian::date d = boost::gregorian::date_from_tm(timetm);
  58. return ptime(d, time_duration(timetm.tm_hour, timetm.tm_min, timetm.tm_sec));
  59. }
  60. #if defined(BOOST_HAS_FTIME)
  61. //! Function to create a time object from an initialized FILETIME struct.
  62. /*! Function to create a time object from an initialized FILETIME struct.
  63. * A FILETIME struct holds 100-nanosecond units (0.0000001). When
  64. * built with microsecond resolution the FILETIME's sub second value
  65. * will be truncated. Nanosecond resolution has no truncation.
  66. *
  67. * \note FILETIME is part of the Win32 API, so it is not portable to non-windows
  68. * platforms.
  69. *
  70. * \note The function is templated on the FILETIME type, so that
  71. * it can be used with both native FILETIME and the ad-hoc
  72. * boost::detail::winapi::FILETIME_ type.
  73. */
  74. template< typename TimeT, typename FileTimeT >
  75. inline
  76. TimeT from_ftime(const FileTimeT& ft)
  77. {
  78. return boost::date_time::time_from_ftime<TimeT>(ft);
  79. }
  80. #endif // BOOST_HAS_FTIME
  81. } } //namespace boost::posix_time
  82. #endif