filetime_functions.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #ifndef DATE_TIME_FILETIME_FUNCTIONS_HPP__
  2. #define DATE_TIME_FILETIME_FUNCTIONS_HPP__
  3. /* Copyright (c) 2004 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. /*! @file filetime_functions.hpp
  11. * Function(s) for converting between a FILETIME structure and a
  12. * time object. This file is only available on systems that have
  13. * BOOST_HAS_FTIME defined.
  14. */
  15. #include <boost/date_time/compiler_config.hpp>
  16. #if defined(BOOST_HAS_FTIME) // skip this file if no FILETIME
  17. #include <boost/cstdint.hpp>
  18. #include <boost/date_time/time.hpp>
  19. #include <boost/date_time/date_defs.hpp>
  20. namespace boost {
  21. namespace date_time {
  22. //! Create a time object from an initialized FILETIME struct.
  23. /*!
  24. * Create a time object from an initialized FILETIME struct.
  25. * A FILETIME struct holds 100-nanosecond units (0.0000001). When
  26. * built with microsecond resolution the file_time's sub second value
  27. * will be truncated. Nanosecond resolution has no truncation.
  28. *
  29. * \note The function is templated on the FILETIME type, so that
  30. * it can be used with both native FILETIME and the ad-hoc
  31. * boost::detail::winapi::FILETIME_ type.
  32. */
  33. template< typename TimeT, typename FileTimeT >
  34. inline
  35. TimeT time_from_ftime(const FileTimeT& ft)
  36. {
  37. typedef typename TimeT::date_type date_type;
  38. typedef typename TimeT::date_duration_type date_duration_type;
  39. typedef typename TimeT::time_duration_type time_duration_type;
  40. // https://svn.boost.org/trac/boost/ticket/2523
  41. // Since this function can be called with arbitrary times, including ones that
  42. // are before 1970-Jan-01, we'll have to cast the time a bit differently,
  43. // than it is done in the microsec_clock::file_time_to_microseconds function. This allows to
  44. // avoid integer wrapping for dates before 1970-Jan-01.
  45. // 100-nanos since 1601-Jan-01
  46. uint64_t ft_as_integer = (static_cast< uint64_t >(ft.dwHighDateTime) << 32) | static_cast< uint64_t >(ft.dwLowDateTime);
  47. uint64_t sec = ft_as_integer / 10000000UL;
  48. uint32_t sub_sec = static_cast< uint32_t >(ft_as_integer % 10000000UL) // 100-nanoseconds since the last second
  49. #if !defined(BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG)
  50. / 10U; // microseconds since the last second
  51. #else
  52. * 100U; // nanoseconds since the last second
  53. #endif
  54. // split sec into usable chunks: days, hours, minutes, & seconds
  55. const uint32_t sec_per_day = 86400; // seconds per day
  56. uint32_t days = static_cast< uint32_t >(sec / sec_per_day);
  57. uint32_t tmp = static_cast< uint32_t >(sec % sec_per_day);
  58. uint32_t hours = tmp / 3600; // sec_per_hour
  59. tmp %= 3600;
  60. uint32_t minutes = tmp / 60; // sec_per_min
  61. tmp %= 60;
  62. uint32_t seconds = tmp; // seconds
  63. date_duration_type dd(days);
  64. date_type d = date_type(1601, Jan, 01) + dd;
  65. return TimeT(d, time_duration_type(hours, minutes, seconds, sub_sec));
  66. }
  67. }} // boost::date_time
  68. #endif // BOOST_HAS_FTIME
  69. #endif // DATE_TIME_FILETIME_FUNCTIONS_HPP__