123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- #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>
- #include <boost/date_time/filetime_functions.hpp>
- #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)
- winapi::file_time ft;
- winapi::get_system_time_as_file_time(ft);
- uint64_t micros = winapi::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(curr_ptr->tm_year + 1900,
- curr_ptr->tm_mon + 1,
- curr_ptr->tm_mday);
-
-
-
-
- int adjust = static_cast< int >(resolution_traits_type::res_adjust() / 1000000);
- time_duration_type td(curr_ptr->tm_hour,
- curr_ptr->tm_min,
- curr_ptr->tm_sec,
- sub_sec * adjust);
- return time_type(d,td);
- }
- };
- } }
- #endif
- #endif
|