CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/CondFormats/Common/interface/TimeConversions.h

Go to the documentation of this file.
00001 #ifndef CondCommon_TimeConversions_h
00002 #define CondCommon_TimeConversions_h 
00003 
00004 #include "CondFormats/Common/interface/Time.h"
00005 #include <ctime>
00006 #include <sys/time.h>
00007 #include <string>
00008 // FIXME incompatible with coral
00009 // #define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
00010 #include "boost/date_time/posix_time/posix_time.hpp"
00011 
00012 namespace cond {
00013 
00014 
00015   namespace time {
00016 
00017     
00018     typedef boost::date_time::subsecond_duration<boost::posix_time::time_duration,1000000000> nanoseconds;
00019 
00020 
00021     // valid for all representations
00022 
00023     const Time_t kLowMask(0xFFFFFFFF);
00024  
00025     inline cond::UnpackedTime unpack(cond::Time_t iValue) {
00026       return cond::UnpackedTime(iValue >> 32, kLowMask & iValue);
00027     }
00028 
00029     inline cond::Time_t pack(cond::UnpackedTime iValue) {
00030       Time_t t = iValue.first;
00031       return (t<< 32) + iValue.second;
00032     }
00033 
00034 
00035     // for real time 
00036     inline unsigned int itsNanoseconds(boost::posix_time::time_duration const & td) {
00037       return boost::posix_time::time_duration::num_fractional_digits() == 6 ? 
00038         1000*td.fractional_seconds() : td.fractional_seconds();
00039     }
00040 
00041 
00042     const boost::posix_time::ptime time0 =
00043       boost::posix_time::from_time_t(0);
00044     
00045     inline boost::posix_time::ptime to_boost(Time_t iValue) {
00046       return time0 +  
00047         boost::posix_time::seconds( iValue >> 32) + 
00048         nanoseconds(kLowMask & iValue);
00049     }
00050     
00051     
00052     inline Time_t from_boost(boost::posix_time::ptime bt) {
00053       boost::posix_time::time_duration td = bt - time0;
00054       Time_t t = td.total_seconds();
00055       return (t << 32) + itsNanoseconds(td);
00056 
00057     } 
00058 
00059     
00060     inline ::timeval to_timeval(Time_t iValue) {
00061       ::timeval stv;
00062       //  stv.tv_sec =  static_cast<unsigned int>(iValue >> 32);
00063       //stv.tv_usec = static_cast<unsigned int>(kLowMask & iValue);
00064       stv.tv_sec =  iValue >> 32;
00065       stv.tv_usec = (kLowMask & iValue)/1000;
00066       return stv;
00067     }
00068     
00069     
00070     inline Time_t from_timeval( ::timeval stv) {
00071       Time_t t = stv.tv_sec;
00072       return (t << 32) + 1000*stv.tv_usec;
00073     }
00074     
00075     inline Time_t now() {
00076       ::timeval stv;
00077       ::gettimeofday(&stv,0);
00078       return  from_timeval(stv);
00079     }
00080     
00081     
00082   } // ns time
00083 
00084 } // cond
00085 
00086 
00087 #endif