Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:50:54

0001 #ifndef CondCommon_TimeConversions_h
0002 #define CondCommon_TimeConversions_h
0003 
0004 #include "CondFormats/Common/interface/Time.h"
0005 #include <ctime>
0006 #include <sys/time.h>
0007 #include <string>
0008 // FIXME incompatible with coral
0009 // #define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
0010 #include "boost/date_time/posix_time/posix_time.hpp"
0011 
0012 namespace cond {
0013 
0014   namespace time {
0015 
0016     typedef boost::date_time::subsecond_duration<boost::posix_time::time_duration, 1000000000> nanoseconds;
0017 
0018     // valid for all representations
0019 
0020     const Time_t kLowMask(0xFFFFFFFF);
0021 
0022     inline cond::UnpackedTime unpack(cond::Time_t iValue) {
0023       return cond::UnpackedTime(iValue >> 32, kLowMask & iValue);
0024     }
0025 
0026     inline cond::Time_t pack(cond::UnpackedTime iValue) {
0027       Time_t t = iValue.first;
0028       return (t << 32) + iValue.second;
0029     }
0030 
0031     // for real time
0032     inline unsigned int itsNanoseconds(boost::posix_time::time_duration const& td) {
0033       return boost::posix_time::time_duration::num_fractional_digits() == 6 ? 1000 * td.fractional_seconds()
0034                                                                             : td.fractional_seconds();
0035     }
0036 
0037     const boost::posix_time::ptime time0 = boost::posix_time::from_time_t(0);
0038 
0039     inline boost::posix_time::ptime to_boost(Time_t iValue) {
0040       return time0 + boost::posix_time::seconds(iValue >> 32) + nanoseconds(kLowMask & iValue);
0041     }
0042 
0043     inline Time_t from_boost(boost::posix_time::ptime bt) {
0044       boost::posix_time::time_duration td = bt - time0;
0045       Time_t t = td.total_seconds();
0046       return (t << 32) + itsNanoseconds(td);
0047     }
0048 
0049     inline ::timeval to_timeval(Time_t iValue) {
0050       ::timeval stv;
0051       //  stv.tv_sec =  static_cast<unsigned int>(iValue >> 32);
0052       //stv.tv_usec = static_cast<unsigned int>(kLowMask & iValue);
0053       stv.tv_sec = iValue >> 32;
0054       stv.tv_usec = (kLowMask & iValue) / 1000;
0055       return stv;
0056     }
0057 
0058     inline Time_t from_timeval(::timeval stv) {
0059       Time_t t = stv.tv_sec;
0060       return (t << 32) + 1000 * stv.tv_usec;
0061     }
0062 
0063     inline Time_t now() {
0064       ::timeval stv;
0065       ::gettimeofday(&stv, nullptr);
0066       return from_timeval(stv);
0067     }
0068 
0069   }  // namespace time
0070 
0071 }  // namespace cond
0072 
0073 #endif