1 | #ifndef DATE_TIME_HIGHRES_TIME_CLOCK_HPP___ |
---|
2 | #define DATE_TIME_HIGHRES_TIME_CLOCK_HPP___ |
---|
3 | |
---|
4 | /* Copyright (c) 2002,2003,2005 CrystalClear Software, Inc. |
---|
5 | * Use, modification and distribution is subject to the |
---|
6 | * Boost Software License, Version 1.0. (See accompanying |
---|
7 | * file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0) |
---|
8 | * Author: Jeff Garland, Bart Garst |
---|
9 | * $Date: 2006/03/18 16:47:55 $ |
---|
10 | */ |
---|
11 | |
---|
12 | |
---|
13 | /*! @file microsec_time_clock.hpp |
---|
14 | This file contains a high resolution time clock implementation. |
---|
15 | */ |
---|
16 | |
---|
17 | #include <boost/detail/workaround.hpp> |
---|
18 | #include "boost/date_time/c_time.hpp" |
---|
19 | #include "boost/cstdint.hpp" |
---|
20 | #include "boost/shared_ptr.hpp" |
---|
21 | |
---|
22 | #ifdef BOOST_HAS_FTIME |
---|
23 | #include <windows.h> |
---|
24 | #endif |
---|
25 | |
---|
26 | #ifdef BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK |
---|
27 | |
---|
28 | namespace boost { |
---|
29 | namespace date_time { |
---|
30 | |
---|
31 | |
---|
32 | //! A clock providing microsecond level resolution |
---|
33 | /*! A high precision clock that measures the local time |
---|
34 | * at a resolution up to microseconds and adjusts to the |
---|
35 | * resolution of the time system. For example, for the |
---|
36 | * a library configuration with nano second resolution, |
---|
37 | * the last 3 places of the fractional seconds will always |
---|
38 | * be 000 since there are 1000 nano-seconds in a micro second. |
---|
39 | */ |
---|
40 | template<class time_type> |
---|
41 | class microsec_clock |
---|
42 | { |
---|
43 | public: |
---|
44 | typedef typename time_type::date_type date_type; |
---|
45 | typedef typename time_type::time_duration_type time_duration_type; |
---|
46 | typedef typename time_duration_type::rep_type resolution_traits_type; |
---|
47 | |
---|
48 | //! return a local time object for the given zone, based on computer clock |
---|
49 | //JKG -- looks like we could rewrite this against universal_time |
---|
50 | template<class time_zone_type> |
---|
51 | static time_type local_time(shared_ptr<time_zone_type> tz_ptr) { |
---|
52 | typedef typename time_type::utc_time_type utc_time_type; |
---|
53 | typedef second_clock<utc_time_type> second_clock; |
---|
54 | // we'll need to know the utc_offset this machine has |
---|
55 | // in order to get a utc_time_type set to utc |
---|
56 | utc_time_type utc_time = second_clock::universal_time(); |
---|
57 | time_duration_type utc_offset = second_clock::local_time() - utc_time; |
---|
58 | // use micro clock to get a local time with sub seconds |
---|
59 | // and adjust it to get a true utc time reading with sub seconds |
---|
60 | utc_time = microsec_clock<utc_time_type>::local_time() - utc_offset; |
---|
61 | return time_type(utc_time, tz_ptr); |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | private: |
---|
66 | // we want this enum available for both platforms yet still private |
---|
67 | enum TZ_FOR_CREATE { LOCAL, GMT }; |
---|
68 | |
---|
69 | public: |
---|
70 | |
---|
71 | #ifdef BOOST_HAS_GETTIMEOFDAY |
---|
72 | //! Return the local time based on computer clock settings |
---|
73 | static time_type local_time() { |
---|
74 | return create_time(LOCAL); |
---|
75 | } |
---|
76 | |
---|
77 | //! Get the current day in universal date as a ymd_type |
---|
78 | static time_type universal_time() |
---|
79 | { |
---|
80 | return create_time(GMT); |
---|
81 | } |
---|
82 | |
---|
83 | private: |
---|
84 | static time_type create_time(TZ_FOR_CREATE tz) { |
---|
85 | timeval tv; |
---|
86 | gettimeofday(&tv, 0); //gettimeofday does not support TZ adjust on Linux. |
---|
87 | std::time_t t = tv.tv_sec; |
---|
88 | boost::uint32_t fs = tv.tv_usec; |
---|
89 | std::tm curr, *curr_ptr = 0; |
---|
90 | if (tz == LOCAL) { |
---|
91 | curr_ptr = c_time::localtime(&t, &curr); |
---|
92 | } else { |
---|
93 | curr_ptr = c_time::gmtime(&t, &curr); |
---|
94 | } |
---|
95 | date_type d(curr_ptr->tm_year + 1900, |
---|
96 | curr_ptr->tm_mon + 1, |
---|
97 | curr_ptr->tm_mday); |
---|
98 | //The following line will adjusts the fractional second tick in terms |
---|
99 | //of the current time system. For example, if the time system |
---|
100 | //doesn't support fractional seconds then res_adjust returns 0 |
---|
101 | //and all the fractional seconds return 0. |
---|
102 | int adjust = resolution_traits_type::res_adjust()/1000000; |
---|
103 | |
---|
104 | time_duration_type td(curr_ptr->tm_hour, |
---|
105 | curr_ptr->tm_min, |
---|
106 | curr_ptr->tm_sec, |
---|
107 | fs*adjust); |
---|
108 | return time_type(d,td); |
---|
109 | |
---|
110 | } |
---|
111 | #endif // BOOST_HAS_GETTIMEOFDAY |
---|
112 | |
---|
113 | #ifdef BOOST_HAS_FTIME |
---|
114 | //! Return the local time based on computer clock settings |
---|
115 | static time_type local_time() { |
---|
116 | FILETIME ft; |
---|
117 | #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205)) |
---|
118 | // Some runtime library implementations expect local times as the norm for ctime. |
---|
119 | FILETIME ft_utc; |
---|
120 | GetSystemTimeAsFileTime(&ft_utc); |
---|
121 | FileTimeToLocalFileTime(&ft_utc,&ft); |
---|
122 | #else |
---|
123 | GetSystemTimeAsFileTime(&ft); |
---|
124 | #endif |
---|
125 | return create_time(ft, LOCAL); |
---|
126 | } |
---|
127 | |
---|
128 | //! Return the UTC time based on computer settings |
---|
129 | static time_type universal_time() { |
---|
130 | FILETIME ft; |
---|
131 | #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205)) |
---|
132 | // Some runtime library implementations expect local times as the norm for ctime. |
---|
133 | FILETIME ft_utc; |
---|
134 | GetSystemTimeAsFileTime(&ft_utc); |
---|
135 | FileTimeToLocalFileTime(&ft_utc,&ft); |
---|
136 | #else |
---|
137 | GetSystemTimeAsFileTime(&ft); |
---|
138 | #endif |
---|
139 | return create_time(ft, GMT); |
---|
140 | } |
---|
141 | |
---|
142 | private: |
---|
143 | static time_type create_time(FILETIME& ft, TZ_FOR_CREATE tz) { |
---|
144 | // offset is difference (in 100-nanoseconds) from |
---|
145 | // 1970-Jan-01 to 1601-Jan-01 |
---|
146 | boost::uint64_t c1 = 27111902; |
---|
147 | boost::uint64_t c2 = 3577643008UL; // 'UL' removes compiler warnings |
---|
148 | const boost::uint64_t OFFSET = (c1 << 32) + c2; |
---|
149 | |
---|
150 | boost::uint64_t filetime = ft.dwHighDateTime; |
---|
151 | filetime = filetime << 32; |
---|
152 | filetime += ft.dwLowDateTime; |
---|
153 | filetime -= OFFSET; |
---|
154 | // filetime now holds 100-nanoseconds since 1970-Jan-01 |
---|
155 | |
---|
156 | // microseconds -- static casts supress warnings |
---|
157 | boost::uint32_t sub_sec = static_cast<boost::uint32_t>((filetime % 10000000) / 10); |
---|
158 | |
---|
159 | std::time_t t = static_cast<time_t>(filetime / 10000000); // seconds since epoch |
---|
160 | |
---|
161 | std::tm curr, *curr_ptr = 0; |
---|
162 | if (tz == LOCAL) { |
---|
163 | curr_ptr = c_time::localtime(&t, &curr); |
---|
164 | } |
---|
165 | else { |
---|
166 | curr_ptr = c_time::gmtime(&t, &curr); |
---|
167 | } |
---|
168 | date_type d(curr_ptr->tm_year + 1900, |
---|
169 | curr_ptr->tm_mon + 1, |
---|
170 | curr_ptr->tm_mday); |
---|
171 | |
---|
172 | //The following line will adjusts the fractional second tick in terms |
---|
173 | //of the current time system. For example, if the time system |
---|
174 | //doesn't support fractional seconds then res_adjust returns 0 |
---|
175 | //and all the fractional seconds return 0. |
---|
176 | int adjust = static_cast<int>(resolution_traits_type::res_adjust()/1000000); |
---|
177 | |
---|
178 | time_duration_type td(curr_ptr->tm_hour, |
---|
179 | curr_ptr->tm_min, |
---|
180 | curr_ptr->tm_sec, |
---|
181 | sub_sec * adjust); |
---|
182 | //st.wMilliseconds * adjust); |
---|
183 | return time_type(d,td); |
---|
184 | |
---|
185 | } |
---|
186 | #endif // BOOST_HAS_FTIME |
---|
187 | }; |
---|
188 | |
---|
189 | |
---|
190 | } } //namespace date_time |
---|
191 | |
---|
192 | #endif //BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK |
---|
193 | |
---|
194 | |
---|
195 | #endif |
---|
196 | |
---|