Chris@16: #ifndef DATE_TIME_HIGHRES_TIME_CLOCK_HPP___ Chris@16: #define DATE_TIME_HIGHRES_TIME_CLOCK_HPP___ Chris@16: Chris@16: /* Copyright (c) 2002,2003,2005 CrystalClear Software, Inc. Chris@16: * Use, modification and distribution is subject to the Chris@16: * Boost Software License, Version 1.0. (See accompanying Chris@16: * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Author: Jeff Garland, Bart Garst Chris@101: * $Date$ Chris@16: */ Chris@16: Chris@16: Chris@16: /*! @file microsec_time_clock.hpp Chris@16: This file contains a high resolution time clock implementation. Chris@16: */ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #ifdef BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK Chris@16: Chris@16: namespace boost { Chris@16: namespace date_time { Chris@16: Chris@16: //! A clock providing microsecond level resolution Chris@16: /*! A high precision clock that measures the local time Chris@16: * at a resolution up to microseconds and adjusts to the Chris@16: * resolution of the time system. For example, for the Chris@16: * a library configuration with nano second resolution, Chris@16: * the last 3 places of the fractional seconds will always Chris@16: * be 000 since there are 1000 nano-seconds in a micro second. Chris@16: */ Chris@16: template Chris@16: class microsec_clock Chris@16: { Chris@16: private: Chris@16: //! Type for the function used to convert time_t to tm Chris@16: typedef std::tm* (*time_converter)(const std::time_t*, std::tm*); Chris@16: Chris@16: public: Chris@16: typedef typename time_type::date_type date_type; Chris@16: typedef typename time_type::time_duration_type time_duration_type; Chris@16: typedef typename time_duration_type::rep_type resolution_traits_type; Chris@16: Chris@16: //! return a local time object for the given zone, based on computer clock Chris@16: //JKG -- looks like we could rewrite this against universal_time Chris@16: template Chris@16: static time_type local_time(shared_ptr tz_ptr) Chris@16: { Chris@16: typedef typename time_type::utc_time_type utc_time_type; Chris@16: typedef second_clock second_clock; Chris@16: // we'll need to know the utc_offset this machine has Chris@16: // in order to get a utc_time_type set to utc Chris@16: utc_time_type utc_time = second_clock::universal_time(); Chris@16: time_duration_type utc_offset = second_clock::local_time() - utc_time; Chris@16: // use micro clock to get a local time with sub seconds Chris@16: // and adjust it to get a true utc time reading with sub seconds Chris@16: utc_time = microsec_clock::local_time() - utc_offset; Chris@16: return time_type(utc_time, tz_ptr); Chris@16: } Chris@16: Chris@16: //! Returns the local time based on computer clock settings Chris@16: static time_type local_time() Chris@16: { Chris@16: return create_time(&c_time::localtime); Chris@16: } Chris@16: Chris@16: //! Returns the UTC time based on computer settings Chris@16: static time_type universal_time() Chris@16: { Chris@16: return create_time(&c_time::gmtime); Chris@16: } Chris@16: Chris@16: private: Chris@16: static time_type create_time(time_converter converter) Chris@16: { Chris@16: #ifdef BOOST_HAS_GETTIMEOFDAY Chris@16: timeval tv; Chris@16: gettimeofday(&tv, 0); //gettimeofday does not support TZ adjust on Linux. Chris@16: std::time_t t = tv.tv_sec; Chris@16: boost::uint32_t sub_sec = tv.tv_usec; Chris@16: #elif defined(BOOST_HAS_FTIME) Chris@16: winapi::file_time ft; Chris@16: winapi::get_system_time_as_file_time(ft); Chris@16: uint64_t micros = winapi::file_time_to_microseconds(ft); // it will not wrap, since ft is the current time Chris@16: // and cannot be before 1970-Jan-01 Chris@16: std::time_t t = static_cast(micros / 1000000UL); // seconds since epoch Chris@101: // microseconds -- static casts suppress warnings Chris@16: boost::uint32_t sub_sec = static_cast(micros % 1000000UL); Chris@16: #else Chris@16: #error Internal Boost.DateTime error: BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK is defined, however neither gettimeofday nor FILETIME support is detected. Chris@16: #endif Chris@16: Chris@16: std::tm curr; Chris@16: std::tm* curr_ptr = converter(&t, &curr); Chris@16: date_type d(static_cast< typename date_type::year_type::value_type >(curr_ptr->tm_year + 1900), Chris@16: static_cast< typename date_type::month_type::value_type >(curr_ptr->tm_mon + 1), Chris@16: static_cast< typename date_type::day_type::value_type >(curr_ptr->tm_mday)); Chris@16: Chris@16: //The following line will adjust the fractional second tick in terms Chris@16: //of the current time system. For example, if the time system Chris@16: //doesn't support fractional seconds then res_adjust returns 0 Chris@16: //and all the fractional seconds return 0. Chris@16: int adjust = static_cast< int >(resolution_traits_type::res_adjust() / 1000000); Chris@16: Chris@16: time_duration_type td(static_cast< typename time_duration_type::hour_type >(curr_ptr->tm_hour), Chris@16: static_cast< typename time_duration_type::min_type >(curr_ptr->tm_min), Chris@16: static_cast< typename time_duration_type::sec_type >(curr_ptr->tm_sec), Chris@16: sub_sec * adjust); Chris@16: Chris@16: return time_type(d,td); Chris@16: } Chris@16: }; Chris@16: Chris@16: Chris@16: } } //namespace date_time Chris@16: Chris@16: #endif //BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK Chris@16: Chris@16: Chris@16: #endif Chris@16: