00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "BaBar/BaBar.hh"
00020
00021
00022
00023
00024
00025 #include <time.h>
00026 #include <iostream>
00027 #include <string>
00028
00029
00030
00031
00032 #include "BdbTime/BdbTimeConst.hh"
00033 #include "BdbTime/BdbTime.hh"
00034 using std::cout;
00035 using std::endl;
00036 using std::ostream;
00037
00038
00039
00040
00041
00042
00043
00044 ostream& operator<<( ostream& o, const tm& tm )
00045 {
00046 o << "{ " << tm.tm_year
00047 << ", " << tm.tm_mon
00048 << ", " << tm.tm_mday
00049 << ", " << tm.tm_hour
00050 << ", " << tm.tm_min
00051 << ", " << tm.tm_sec
00052 << "; " << tm.tm_wday
00053 << ", " << tm.tm_yday
00054 << "; " << tm.tm_isdst
00055 << " }";
00056 return o;
00057 }
00058
00059 time_t bdb_to_time_t( const BdbTime& bt )
00060 {
00061 return time_t( bt.getGmtSec() - BdbTimeConst::seconds_1901_to_1970 );
00062 }
00063
00064 void testString( const char* s )
00065 {
00066 std::string st(s);
00067 BdbTime bt_s(0,0);
00068 bool ok = BdbTime::parseTime( st, BdbTime::UTC, bt_s );
00069 if ( ok ) {
00070 cout << "Parse of \"" << s << "\" succeeded: " << bt_s << endl;
00071 }
00072 else {
00073 cout << "Parse of \"" << s << "\" failed." << endl;
00074 }
00075 }
00076
00077
00078
00079
00080
00081 int main( int, char** )
00082 {
00083 cout << "Test program for BdbTime/BdbTime,BdbDuration\n\n";
00084
00085 cout << "First, test difference between BdbTime epoch and POSIX epoch:\n";
00086
00087 cout << " BdbTimeConst::seconds_1901_to_1970: "
00088 << BdbTimeConst::seconds_1901_to_1970 << "\n";
00089 cout << " 2^31: " << (1U<<31) << "\n";
00090
00091
00092 BdbTime bt_zero( 0, 0 );
00093 cout << " BdbTime(0,0): " << bt_zero.getGmtSec() << " = " << bt_zero << "\n";
00094
00095 BdbTime bt_one( 1, 0 );
00096 cout << " BdbTime(1,0): " << bt_one.getGmtSec() << " = " << bt_one << "\n";
00097
00098 BdbTime bt_1901( 1901, 1, 1, 0, 0, 1 );
00099 cout << " BdbTime( 1901, 1, 1, 0, 0, 1 ): " << bt_1901.getGmtSec() << " = "
00100 << bt_1901 << "\n";
00101
00102 BdbTime bt_1902( 1902, 1, 1, 0, 0, 1 );
00103 cout << " BdbTime( 1902, 1, 1, 0, 0, 1 ): " << bt_1902.getGmtSec() << " = "
00104 << bt_1902 << "\n";
00105
00106 BdbTime bt_1970( 1970, 1, 1, 0, 0, 1 );
00107 cout << " BdbTime( 1970, 1, 1, 0, 0, 1 ): " << bt_1970.getGmtSec() << " = "
00108 << bt_1970 << "\n";
00109
00110 unsigned long bt_1901_1970 = bt_1970.getGmtSec() - bt_1901.getGmtSec();
00111
00112 signed long bt_vs_BdbTimeConst = bt_1901_1970 - BdbTimeConst::seconds_1901_to_1970;
00113
00114 cout << " BdbTime(1970.1.1 0:00:01)-BdbTime(1901.1.1 0:00:01): "
00115 << bt_1901_1970 << "\n";
00116 cout << " above - BdbTimeConst::seconds_1901_to_1970: "
00117 << bt_vs_BdbTimeConst << "\n";
00118
00119 cout << endl;
00120
00121 time_t t_POSIX_one = 1;
00122 struct tm tm_POSIX_one;
00123 gmtime_r( &t_POSIX_one, &tm_POSIX_one );
00124
00125 cout << " gmtime(1) = " << tm_POSIX_one << "\n";
00126
00127 cout << endl;
00128
00129 struct tm tm_bt_1970;
00130 bt_1970.tm( &tm_bt_1970, BdbTime::UTC );
00131 cout << " bt_1970.tm(,utc): " << tm_bt_1970 << "\n";
00132
00133 struct tm tm_bt_1901;
00134 bt_1901.tm( &tm_bt_1901, BdbTime::UTC );
00135 cout << " bt_1901.tm(,utc): " << tm_bt_1901 << "\n";
00136
00137 struct tm tm_bt_1902;
00138 bt_1902.tm( &tm_bt_1902, BdbTime::UTC );
00139 cout << " bt_1902.tm(,utc): " << tm_bt_1902 << "\n";
00140
00141 time_t t_POSIX_1902 = bdb_to_time_t( bt_1902 );
00142 struct tm tm_POSIX_1902;
00143 gmtime_r( &t_POSIX_1902, &tm_POSIX_1902 );
00144 cout << " gmtime(t_POSIX_1902) = " << tm_POSIX_1902 << "\n";
00145
00146
00147 struct tm tm_1901_01_01;
00148 tm_1901_01_01.tm_year = 1;
00149 tm_1901_01_01.tm_mon = 0;
00150 tm_1901_01_01.tm_mday = 1;
00151 tm_1901_01_01.tm_hour = 0;
00152 tm_1901_01_01.tm_min = 0;
00153 tm_1901_01_01.tm_sec = 1;
00154 tm_1901_01_01.tm_wday = 0;
00155 tm_1901_01_01.tm_yday = 0;
00156 tm_1901_01_01.tm_isdst = -1;
00157
00158 struct tm tm_1901_11_01;
00159 tm_1901_11_01.tm_year = 1;
00160 tm_1901_11_01.tm_mon = 10;
00161 tm_1901_11_01.tm_mday = 1;
00162 tm_1901_11_01.tm_hour = 0;
00163 tm_1901_11_01.tm_min = 0;
00164 tm_1901_11_01.tm_sec = 1;
00165 tm_1901_11_01.tm_wday = 0;
00166 tm_1901_11_01.tm_yday = 0;
00167 tm_1901_11_01.tm_isdst = -1;
00168
00169 struct tm tm_1901_12_31;
00170 tm_1901_12_31.tm_year = 1;
00171 tm_1901_12_31.tm_mon = 11;
00172 tm_1901_12_31.tm_mday = 31;
00173 tm_1901_12_31.tm_hour = 0;
00174 tm_1901_12_31.tm_min = 0;
00175 tm_1901_12_31.tm_sec = 1;
00176 tm_1901_12_31.tm_wday = 0;
00177 tm_1901_12_31.tm_yday = 0;
00178 tm_1901_12_31.tm_isdst = -1;
00179
00180 cout << "\n";
00181 cout << " tm_1901_01_01: " << tm_1901_01_01 << "\n";
00182 BdbTime bt_tm_1901_01_01( tm_1901_01_01 );
00183 cout << " BdbTime(tm_1901_01_01).getGmtSec() = " << bt_tm_1901_01_01.getGmtSec() << endl;
00184 cout << " tm_1901_01_01, after: " << tm_1901_01_01 << "\n";
00185
00186 cout << "\n";
00187 cout << " tm_1901_11_01: " << tm_1901_11_01 << "\n";
00188 BdbTime bt_tm_1901_11_01( tm_1901_11_01 );
00189 cout << " BdbTime(tm_1901_11_01).getGmtSec() = " << bt_tm_1901_11_01.getGmtSec() << endl;
00190 cout << " tm_1901_11_01, after: " << tm_1901_11_01 << "\n";
00191
00192 cout << "\n";
00193 cout << " tm_1901_12_31: " << tm_1901_12_31 << "\n";
00194 BdbTime bt_tm_1901_12_31( tm_1901_12_31 );
00195 cout << " BdbTime(tm_1901_12_31).getGmtSec() = " << bt_tm_1901_12_31.getGmtSec() << endl;
00196 cout << " BdbTime(tm_1901_12_31).getGmtSec() + 86400 = " << ( bt_tm_1901_12_31.getGmtSec() + 86400 ) << endl;
00197 cout << " tm_1901_12_31, after: " << tm_1901_12_31 << "\n";
00198
00199
00200
00201
00202
00203
00204 cout << "\n";
00205
00206 BdbDuration oneDay( 86400 );
00207 BdbDuration twoDay( oneDay + oneDay );
00208
00209 BdbTime bt_tm_1901_12_31_p2 = bt_tm_1901_12_31 + twoDay;
00210 cout << " BdbTime(tm_1901_12_31 + 2day).getGmtSec() = " << bt_tm_1901_12_31_p2.getGmtSec() << endl;
00211
00212
00213
00214
00215
00216
00217 BdbTime bt_tm_1901_12_31_p2m1 = bt_tm_1901_12_31_p2 - oneDay;
00218 cout << " BdbTime(tm_1901_12_31 + 2 - 1day).getGmtSec() = " << bt_tm_1901_12_31_p2m1.getGmtSec() << endl;
00219
00220
00221
00222
00223
00224
00225 cout << "\n";
00226
00227 int n = 0x7fffffff;
00228 n = -n;
00229 struct timespec ts_minPOSIXsigned;
00230 ts_minPOSIXsigned.tv_sec = n;
00231 ts_minPOSIXsigned.tv_nsec = 0;
00232
00233 BdbTime bt_minPOSIXsigned( ts_minPOSIXsigned );
00234
00235 struct tm tm_minPOSIXsigned;
00236 time_t t_minPOSIXsigned = n;
00237 gmtime_r( &t_minPOSIXsigned, &tm_minPOSIXsigned );
00238
00239 cout << "Minimum POSIX time = time_t(" << n << "):\n"
00240 << " gmtime() = " << tm_minPOSIXsigned << "\n"
00241 << " BdbTime().getGmtSec() = "
00242 << bt_minPOSIXsigned.getGmtSec() << "\n";
00243
00244 cout << endl;
00245
00246
00247 BdbTime tx( 2002, 5, 7, 16, 55, 1 );
00248 std::string stx( tx.asString( "Time is year %Y %D %T %r %A week number %V UTC",
00249 BdbTime::UTC ) );
00250 cout << "Test asString():\n";
00251 cout << " random: " << stx << endl;
00252
00253 BdbTime bt1( 1, 0 );
00254 cout << " smallest (UTC): "
00255 << bt1.asString( "Time is year %Y %D %T %r %A week number %V UTC",
00256 BdbTime::UTC ) << "\n";
00257
00258 cout << " smallest (local): "
00259 << bt1.asString( "Time is year %Y %D %T %r %A week number %V (local)",
00260 BdbTime::Local ) << "\n";
00261
00262 BdbTime bt0( 0, 0 );
00263 cout << " zero (UTC): "
00264 << bt0.asString( "Time is year %Y %D %T %r %A week number %V UTC",
00265 BdbTime::UTC ) << "\n";
00266
00267 cout << " zero (local): "
00268 << bt0.asString( "Time is year %Y %D %T %r %A week number %V (local)",
00269 BdbTime::Local ) << endl;
00270
00271
00272 testString( "01Mar2003 15:20" );
00273 testString( "01Mar2003 15:80" );
00274 testString( "01Mjo2003 15:20" );
00275 testString( "+Infinity" );
00276 testString( "-Infinity" );
00277 testString( "Infinity" );
00278
00279 return 0;
00280 }