conversion.hpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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: 2009-06-04 04:24:49 -0400 (Thu, 04 Jun 2009) $
  9. */
  10. #include "boost/date_time/posix_time/ptime.hpp"
  11. #include "boost/date_time/posix_time/posix_time_duration.hpp"
  12. #include "boost/date_time/filetime_functions.hpp"
  13. #include "boost/date_time/c_time.hpp"
  14. #include "boost/date_time/gregorian/conversion.hpp"
  15. namespace boost {
  16. namespace posix_time {
  17. //! Function that converts a time_t into a ptime.
  18. inline
  19. ptime from_time_t(std::time_t t)
  20. {
  21. ptime start(gregorian::date(1970,1,1));
  22. return start + seconds(static_cast<long>(t));
  23. }
  24. //! Convert a time to a tm structure truncating any fractional seconds
  25. inline
  26. std::tm to_tm(const boost::posix_time::ptime& t) {
  27. std::tm timetm = boost::gregorian::to_tm(t.date());
  28. boost::posix_time::time_duration td = t.time_of_day();
  29. timetm.tm_hour = td.hours();
  30. timetm.tm_min = td.minutes();
  31. timetm.tm_sec = td.seconds();
  32. timetm.tm_isdst = -1; // -1 used when dst info is unknown
  33. return timetm;
  34. }
  35. //! Convert a time_duration to a tm structure truncating any fractional seconds and zeroing fields for date components
  36. inline
  37. std::tm to_tm(const boost::posix_time::time_duration& td) {
  38. std::tm timetm = {};
  39. timetm.tm_hour = date_time::absolute_value(td.hours());
  40. timetm.tm_min = date_time::absolute_value(td.minutes());
  41. timetm.tm_sec = date_time::absolute_value(td.seconds());
  42. timetm.tm_isdst = -1; // -1 used when dst info is unknown
  43. return timetm;
  44. }
  45. //! Convert a tm struct to a ptime ignoring is_dst flag
  46. inline
  47. ptime ptime_from_tm(const std::tm& timetm) {
  48. boost::gregorian::date d = boost::gregorian::date_from_tm(timetm);
  49. return ptime(d, time_duration(timetm.tm_hour, timetm.tm_min, timetm.tm_sec));
  50. }
  51. #if defined(BOOST_HAS_FTIME)
  52. //! Function to create a time object from an initialized FILETIME struct.
  53. /*! Function to create a time object from an initialized FILETIME struct.
  54. * A FILETIME struct holds 100-nanosecond units (0.0000001). When
  55. * built with microsecond resolution the FILETIME's sub second value
  56. * will be truncated. Nanosecond resolution has no truncation.
  57. *
  58. * \note FILETIME is part of the Win32 API, so it is not portable to non-windows
  59. * platforms.
  60. *
  61. * \note The function is templated on the FILETIME type, so that
  62. * it can be used with both native FILETIME and the ad-hoc
  63. * boost::date_time::winapi::file_time type.
  64. */
  65. template< typename TimeT, typename FileTimeT >
  66. inline
  67. TimeT from_ftime(const FileTimeT& ft)
  68. {
  69. return boost::date_time::time_from_ftime<TimeT>(ft);
  70. }
  71. #endif // BOOST_HAS_FTIME
  72. } } //namespace boost::posix_time
  73. #endif