Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

CdbTimeUtils.cc

Go to the documentation of this file.
00001 // File and Version Information:
00002 //      $Id: CdbTimeUtils.cc,v 1.5 2005/10/25 17:25:29 gapon Exp $
00003 
00004 /// The implementation of the CdbTimeUtils class.
00005 /**
00006   * @see CdbTimeUtils
00007   */
00008 
00009 #include "BaBar/BaBar.hh"
00010 
00011 #include "CdbBase/CdbTimeUtils.hh"
00012 
00013 #include <stdio.h>      // sscanf(), sprintf()
00014 #include <stdlib.h>     // getenv()
00015 #include <sys/time.h>
00016 
00017 #include <sstream>
00018 using std::ostringstream;
00019 #include <iostream>
00020 using std::cout;
00021 using std::endl;
00022 
00023 bool
00024 CdbTimeUtils::string2time( BdbTime&    theTime,
00025                            const char* theString )
00026 {
00027   // A non-zero pointer is expected
00028 
00029     if( 0 != theString ) {
00030 
00031       // Try the standard translator first
00032 
00033         BdbTime value;
00034         if( BdbTime::parseTime( std::string( theString ),
00035                                 BdbTime::Local,
00036                                 value )) {
00037             theTime = value;
00038             return true;
00039         }
00040 
00041       // Try to see if the time is in the internal format
00042 
00043         unsigned int seconds;
00044         unsigned int nanoseconds;
00045 
00046         if( 2 == sscanf( theString,
00047                          "%u.%u",
00048                          &seconds,
00049                          &nanoseconds )) {
00050 
00051             theTime = BdbTime( seconds,
00052                                nanoseconds );
00053             return true;
00054         }
00055     }
00056     return false;
00057 }
00058 
00059 bool
00060 CdbTimeUtils::string2time( BdbTime&           theTime,
00061                            const std::string& theString )
00062 {
00063     return string2time( theTime,
00064                         theString.c_str( ));
00065 }
00066 
00067 std::string
00068 CdbTimeUtils::time2string( const BdbTime& theTime,
00069                            bool           forceInternalFormat,
00070                            bool           packedFormat )
00071 {
00072     unsigned int sec  = theTime.getGmtSec( );
00073     unsigned int nsec = theTime.getGmtNsec( );
00074 
00075     char buf[80];
00076 
00077     if( forceInternalFormat ) {
00078         if( packedFormat ) sprintf( buf, "%u.%u",           sec, nsec );
00079         else               sprintf( buf, "%10.10u.%10.10u", sec, nsec );
00080     } else {
00081         if( packedFormat ) {
00082             if(      BdbTime::minusInfinity == theTime ) sprintf( buf, "-Infinity" );
00083             else if( BdbTime::plusInfinity  == theTime ) sprintf( buf, "+Infinity" );
00084             else                                         sprintf( buf, "%u.%u", sec, nsec );
00085         } else {
00086             if(      BdbTime::minusInfinity == theTime ) sprintf( buf, "      -Infinity      " );
00087             else if( BdbTime::plusInfinity  == theTime ) sprintf( buf, "      +Infinity      " );
00088             else                                         sprintf( buf, "%10.10u.%10.10u", sec, nsec );
00089         }
00090     }
00091     return std::string( buf );
00092 }
00093 
00094 std::string
00095 CdbTimeUtils::time2string2( const BdbTime& theTime,
00096                             BdbTime::Zone  theTimeZone,
00097                             bool           forceInternalFormat,
00098                             bool           packedFormat )
00099 {
00100     ostringstream s;
00101     s << time2string( theTime,
00102                       forceInternalFormat,
00103                       packedFormat )
00104       << " : "
00105       << theTime.asString( "%c",
00106                            theTimeZone ) << " (local time) " << theTime.getGmtNsec( ) << " ns";
00107     const std::string result = s.str( );
00108     return result;
00109 }
00110 
00111 std::string
00112 CdbTimeUtils::get_current_timestamp( )
00113 {
00114     struct timeval tv;
00115     gettimeofday( &tv, 0 );
00116 
00117     struct tm *t = localtime( &( tv.tv_sec ));
00118 
00119     char buf[24];
00120     sprintf( buf,
00121              "%02d%02d %02d:%02d:%02d.%03ld",
00122              1 + t->tm_mon,
00123              t->tm_mday, 
00124              t->tm_hour,
00125              t->tm_min,
00126              t->tm_sec,
00127              tv.tv_usec / 1000 );
00128 
00129     return std::string( buf );
00130 }
00131 
00132 BdbTime
00133 CdbTimeUtils::from_nsec( unsigned long long theNumber )
00134 {
00135     const unsigned long long plusInfinityNanoseconds = (unsigned long long) BdbTimeConst::nsecInASec * BdbTimeConst::plusInfinity;
00136 
00137 //    cout << "theNumber: " << theNumber << ", plusInfinityNanoseconds: " << plusInfinityNanoseconds << ", BdbTimeConst::nsecInASec: " << BdbTimeConst::nsecInASec << ", seconds: " << theNumber / BdbTimeConst::nsecInASec << ", nanoseconds: " << theNumber % BdbTimeConst::nsecInASec << endl;
00138 
00139     if( theNumber > plusInfinityNanoseconds ) return BdbTime::plusInfinity;
00140 
00141     return BdbTime( theNumber / BdbTimeConst::nsecInASec,
00142                     theNumber % BdbTimeConst::nsecInASec );
00143 }
00144 
00145 unsigned long long
00146 CdbTimeUtils::to_nsec( const BdbTime& theTime )
00147 {
00148     return BdbTimeConst::nsecInASec * (unsigned long long) theTime.getGmtSec( ) + theTime.getGmtNsec( );
00149 }
00150 
00151 /////////////////
00152 // End Of File //
00153 /////////////////

Generated on Mon Dec 5 18:22:12 2005 for CDB by doxygen1.3-rc3