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  namespace time {
15 
16  typedef boost::date_time::subsecond_duration<boost::posix_time::time_duration, 1000000000> nanoseconds;
17 
18  // valid for all representations
19 
20  const Time_t kLowMask(0xFFFFFFFF);
21 
23  return cond::UnpackedTime(iValue >> 32, kLowMask & iValue);
24  }
25 
27  Time_t t = iValue.first;
28  return (t << 32) + iValue.second;
29  }
30 
31  // for real time
32  inline unsigned int itsNanoseconds(boost::posix_time::time_duration const& td) {
33  return boost::posix_time::time_duration::num_fractional_digits() == 6 ? 1000 * td.fractional_seconds()
34  : td.fractional_seconds();
35  }
36 
37  const boost::posix_time::ptime time0 = boost::posix_time::from_time_t(0);
38 
39  inline boost::posix_time::ptime to_boost(Time_t iValue) {
40  return time0 + boost::posix_time::seconds(iValue >> 32) + nanoseconds(kLowMask & iValue);
41  }
42 
43  inline Time_t from_boost(boost::posix_time::ptime bt) {
44  boost::posix_time::time_duration td = bt - time0;
45  Time_t t = td.total_seconds();
46  return (t << 32) + itsNanoseconds(td);
47  }
48 
49  inline ::timeval to_timeval(Time_t iValue) {
50  ::timeval stv;
51  // stv.tv_sec = static_cast<unsigned int>(iValue >> 32);
52  //stv.tv_usec = static_cast<unsigned int>(kLowMask & iValue);
53  stv.tv_sec = iValue >> 32;
54  stv.tv_usec = (kLowMask & iValue) / 1000;
55  return stv;
56  }
57 
58  inline Time_t from_timeval(::timeval stv) {
59  Time_t t = stv.tv_sec;
60  return (t << 32) + 1000 * stv.tv_usec;
61  }
62 
63  inline Time_t now() {
64  ::timeval stv;
65  ::gettimeofday(&stv, nullptr);
66  return from_timeval(stv);
67  }
68 
69  } // namespace time
70 
71 } // namespace cond
72 
73 #endif
boost::date_time::subsecond_duration< boost::posix_time::time_duration, 1000000000 > nanoseconds
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:14
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:18
std::pair< unsigned int, unsigned int > UnpackedTime
Definition: Time.h:15
boost::posix_time::ptime to_boost(Time_t iValue)
Time_t now()
cond::UnpackedTime unpack(cond::Time_t iValue)