00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef __SUNPRO_CC
00029 #define _STRPTIME_DONTZERO
00030 #endif
00031
00032 #include "BaBar/BaBar.hh"
00033
00034
00035
00036
00037 #include "BdbTime/BdbTimeInput.hh"
00038
00039
00040
00041
00042 #include <string.h>
00043 #include <iostream>
00044 using std::cerr;
00045 using std::endl;
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 BdbTimeInput::Status
00088 BdbTimeInput::parseTime( const char* string,
00089 struct tm& result )
00090 {
00091 return BdbTimeInput::parseString( string, timeFormats_, result, debug_ );
00092 }
00093
00094
00095 BdbTimeInput::Status
00096 BdbTimeInput::parseDate( const char* string,
00097 struct tm& result )
00098 {
00099 return BdbTimeInput::parseString( string, dateFormats_, result, debug_ );
00100 }
00101
00102
00103 BdbTimeInput::Status
00104 BdbTimeInput::parseString( const char* string,
00105 const char* const formats[],
00106 struct tm& result,
00107 bool debug )
00108 {
00109
00110
00111 if ( debug ) {
00112 cerr << "BdbTimeInput::parseString: strlen(\"" << string << "\") is "
00113 << strlen(string) << endl;
00114 }
00115 const char* const stringEnd = string + strlen(string);
00116
00117 BdbTimeInput::Status status = BdbTimeInput::NoMatch;
00118 for ( const char* const* fmt = formats; *fmt != 0; ++fmt ) {
00119 if ( debug ) {
00120 cerr << "Trying format \"" << *fmt << "\":";
00121 }
00122 const char* last = strptime( string, *fmt, &result );
00123 if ( last == stringEnd ) {
00124 status = BdbTimeInput::Success;
00125 if ( debug ) {
00126 cerr << " OK!" << endl;
00127 }
00128 break;
00129 }
00130 else if ( debug ) {
00131 if ( last == 0 ) {
00132 cerr << " returned null pointer; failure." << endl;
00133 }
00134 else {
00135 cerr << " stopped at position " << (last-string);
00136 if ( *last == '\0' ) {
00137 cerr << " = '\0'; strange!" << endl;
00138 }
00139 else {
00140 cerr << " = '" << *last << "'; failure." << endl;
00141 }
00142 }
00143 }
00144 }
00145 if ( debug && status != BdbTimeInput::Success ) {
00146 cerr << "Tried all patterns without success." << endl;
00147 }
00148
00149 return status;
00150 }
00151
00152 void
00153 BdbTimeInput::setDebugging( bool debug )
00154 {
00155 debug_ = debug;
00156 }
00157
00158 const char*
00159 BdbTimeInput::defaultTimeFormats_[] =
00160 {
00161 "%T"
00162 , "%R"
00163 , 0
00164 };
00165
00166 const char* const *
00167 BdbTimeInput::timeFormats_ = BdbTimeInput::defaultTimeFormats_;
00168
00169 const char*
00170 BdbTimeInput::defaultDateFormats_[] =
00171 {
00172 "%D"
00173 , "%d%b%Y"
00174 , 0
00175 };
00176
00177 const char* const *
00178 BdbTimeInput::dateFormats_ = BdbTimeInput::defaultDateFormats_;
00179
00180 bool
00181 BdbTimeInput::debug_ = false;
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193