123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- #ifndef DATE_TIME_HIGHRES_TIME_CLOCK_HPP___
- #define DATE_TIME_HIGHRES_TIME_CLOCK_HPP___
- #include <boost/cstdint.hpp>
- #include <boost/shared_ptr.hpp>
- #include <boost/detail/workaround.hpp>
- #include <boost/date_time/compiler_config.hpp>
- #include <boost/date_time/c_time.hpp>
- #include <boost/date_time/time_clock.hpp>
- #if defined(BOOST_HAS_FTIME)
- #include <boost/winapi/time.hpp>
- #endif
- #ifdef BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK
- namespace boost {
- namespace date_time {
-
-
- template<class time_type>
- class microsec_clock
- {
- private:
-
- typedef std::tm* (*time_converter)(const std::time_t*, std::tm*);
- public:
- typedef typename time_type::date_type date_type;
- typedef typename time_type::time_duration_type time_duration_type;
- typedef typename time_duration_type::rep_type resolution_traits_type;
-
-
- template<class time_zone_type>
- static time_type local_time(shared_ptr<time_zone_type> tz_ptr)
- {
- typedef typename time_type::utc_time_type utc_time_type;
- typedef second_clock<utc_time_type> second_clock;
-
-
- utc_time_type utc_time = second_clock::universal_time();
- time_duration_type utc_offset = second_clock::local_time() - utc_time;
-
-
- utc_time = microsec_clock<utc_time_type>::local_time() - utc_offset;
- return time_type(utc_time, tz_ptr);
- }
-
- static time_type local_time()
- {
- return create_time(&c_time::localtime);
- }
-
- static time_type universal_time()
- {
- return create_time(&c_time::gmtime);
- }
- private:
- static time_type create_time(time_converter converter)
- {
- #ifdef BOOST_HAS_GETTIMEOFDAY
- timeval tv;
- gettimeofday(&tv, 0);
- std::time_t t = tv.tv_sec;
- boost::uint32_t sub_sec = tv.tv_usec;
- #elif defined(BOOST_HAS_FTIME)
- boost::winapi::FILETIME_ ft;
- boost::winapi::GetSystemTimeAsFileTime(&ft);
- #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205))
-
- {
- boost::winapi::FILETIME_ local_ft;
- boost::winapi::FileTimeToLocalFileTime(&ft, &local_ft);
- ft = local_ft;
- }
- #endif
- boost::uint64_t micros = file_time_to_microseconds(ft);
-
- std::time_t t = static_cast<std::time_t>(micros / 1000000UL);
-
- boost::uint32_t sub_sec = static_cast<boost::uint32_t>(micros % 1000000UL);
- #else
- #error Internal Boost.DateTime error: BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK is defined, however neither gettimeofday nor FILETIME support is detected.
- #endif
- std::tm curr;
- std::tm* curr_ptr = converter(&t, &curr);
- date_type d(static_cast< typename date_type::year_type::value_type >(curr_ptr->tm_year + 1900),
- static_cast< typename date_type::month_type::value_type >(curr_ptr->tm_mon + 1),
- static_cast< typename date_type::day_type::value_type >(curr_ptr->tm_mday));
-
-
-
-
- int adjust = static_cast< int >(resolution_traits_type::res_adjust() / 1000000);
- time_duration_type td(static_cast< typename time_duration_type::hour_type >(curr_ptr->tm_hour),
- static_cast< typename time_duration_type::min_type >(curr_ptr->tm_min),
- static_cast< typename time_duration_type::sec_type >(curr_ptr->tm_sec),
- sub_sec * adjust);
- return time_type(d,td);
- }
- #if defined(BOOST_HAS_FTIME)
-
- static boost::uint64_t file_time_to_microseconds(boost::winapi::FILETIME_ const& ft)
- {
-
-
- const boost::uint64_t shift = 116444736000000000ULL;
-
- boost::uint64_t ft_as_integer = (static_cast< boost::uint64_t >(ft.dwHighDateTime) << 32) | static_cast< boost::uint64_t >(ft.dwLowDateTime);
- ft_as_integer -= shift;
- return (ft_as_integer / 10U);
- }
- #endif
- };
- } }
- #endif
- #endif
|