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
cond::time::Time_t
cond::Time_t Time_t
Definition: Time.h:18
cond::time::to_boost
boost::posix_time::ptime to_boost(Time_t iValue)
Definition: TimeConversions.h:39
cond::time::nanoseconds
boost::date_time::subsecond_duration< boost::posix_time::time_duration, 1000000000 > nanoseconds
Definition: TimeConversions.h:16
Time.h
cond::time::from_timeval
Time_t from_timeval(::timeval stv)
Definition: TimeConversions.h:58
protons_cff.time
time
Definition: protons_cff.py:35
cond::time::itsNanoseconds
unsigned int itsNanoseconds(boost::posix_time::time_duration const &td)
Definition: TimeConversions.h:32
cond::time::pack
cond::Time_t pack(cond::UnpackedTime iValue)
Definition: TimeConversions.h:26
seconds
double seconds()
cond
Definition: plugin.cc:23
cond::time::from_boost
Time_t from_boost(boost::posix_time::ptime bt)
Definition: TimeConversions.h:43
cond::time::to_timeval
inline ::timeval to_timeval(Time_t iValue)
Definition: TimeConversions.h:49
cond::time::kLowMask
const Time_t kLowMask(0xFFFFFFFF)
cond::Time_t
unsigned long long Time_t
Definition: Time.h:14
cond::time::now
Time_t now()
Definition: TimeConversions.h:63
cond::time::time0
const boost::posix_time::ptime time0
Definition: TimeConversions.h:37
cond::UnpackedTime
std::pair< unsigned int, unsigned int > UnpackedTime
Definition: Time.h:15
cond::time::unpack
cond::UnpackedTime unpack(cond::Time_t iValue)
Definition: TimeConversions.h:22
submitPVValidationJobs.t
string t
Definition: submitPVValidationJobs.py:644