CMS 3D CMS Logo

TimeConversions.h
Go to the documentation of this file.
1 #ifndef CondCommon_TimeConversions_h
2 #define CondCommon_TimeConversions_h
3 
5 #include <ctime>
6 #include <sys/time.h>
7 #include <string>
8 // FIXME incompatible with coral
9 // #define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
10 #include "boost/date_time/posix_time/posix_time.hpp"
11 
12 namespace cond {
13 
14 
15  namespace time {
16 
17 
18  typedef boost::date_time::subsecond_duration<boost::posix_time::time_duration,1000000000> nanoseconds;
19 
20 
21  // valid for all representations
22 
23  const Time_t kLowMask(0xFFFFFFFF);
24 
26  return cond::UnpackedTime(iValue >> 32, kLowMask & iValue);
27  }
28 
30  Time_t t = iValue.first;
31  return (t<< 32) + iValue.second;
32  }
33 
34 
35  // for real time
36  inline unsigned int itsNanoseconds(boost::posix_time::time_duration const & td) {
37  return boost::posix_time::time_duration::num_fractional_digits() == 6 ?
38  1000*td.fractional_seconds() : td.fractional_seconds();
39  }
40 
41 
42  const boost::posix_time::ptime time0 =
43  boost::posix_time::from_time_t(0);
44 
45  inline boost::posix_time::ptime to_boost(Time_t iValue) {
46  return time0 +
47  boost::posix_time::seconds( iValue >> 32) +
48  nanoseconds(kLowMask & iValue);
49  }
50 
51 
52  inline Time_t from_boost(boost::posix_time::ptime bt) {
53  boost::posix_time::time_duration td = bt - time0;
54  Time_t t = td.total_seconds();
55  return (t << 32) + itsNanoseconds(td);
56 
57  }
58 
59 
60  inline ::timeval to_timeval(Time_t iValue) {
61  ::timeval stv;
62  // stv.tv_sec = static_cast<unsigned int>(iValue >> 32);
63  //stv.tv_usec = static_cast<unsigned int>(kLowMask & iValue);
64  stv.tv_sec = iValue >> 32;
65  stv.tv_usec = (kLowMask & iValue)/1000;
66  return stv;
67  }
68 
69 
70  inline Time_t from_timeval( ::timeval stv) {
71  Time_t t = stv.tv_sec;
72  return (t << 32) + 1000*stv.tv_usec;
73  }
74 
75  inline Time_t now() {
76  ::timeval stv;
77  ::gettimeofday(&stv,0);
78  return from_timeval(stv);
79  }
80 
81 
82  } // ns time
83 
84 } // cond
85 
86 
87 #endif
double seconds()
const boost::posix_time::ptime time0
const Time_t kLowMask(0xFFFFFFFF)
cond::Time_t pack(cond::UnpackedTime iValue)
unsigned long long Time_t
Definition: Time.h:16
Time_t from_timeval(::timeval stv)
Time_t from_boost(boost::posix_time::ptime bt)
unsigned int itsNanoseconds(boost::posix_time::time_duration const &td)
inline::timeval to_timeval(Time_t iValue)
cond::Time_t Time_t
Definition: Time.h:17
std::pair< unsigned int, unsigned int > UnpackedTime
Definition: Time.h:17
Definition: plugin.cc:24
boost::date_time::subsecond_duration< boost::posix_time::time_duration, 1000000000 > nanoseconds
boost::posix_time::ptime to_boost(Time_t iValue)
Time_t now()
cond::UnpackedTime unpack(cond::Time_t iValue)