📄 test_ncbitime.cpp
字号:
t.SetFormat(fmtstr); string s = t.AsString(); assert(s.compare("01/01/2003 00:00:00 GMT Wednesday") == 0); }} {{ CTime t(2002, 12, 13, 12, 34, 56, 0, CTime::eGmt); t.SetFormat(fmtstr); string s = t.AsString(); assert(s.compare("12/13/2002 12:34:56 GMT Friday") == 0); }}}//============================================================================//// TestGMT////============================================================================static void s_TestGMT(void){ cout << "---------------------------" << endl; cout << "Test GMT and Local time" << endl; cout << "---------------------------" << endl << endl; {{ cout << "Write time in timezone format" << endl; CTime::SetFormat("M/D/Y h:m:s Z"); CTime t1(2001, 3, 12, 11, 22, 33, 999, CTime::eGmt); cout << "[" << t1.AsString() << "]" << endl; assert(t1.AsString() == "03/12/2001 11:22:33 GMT"); CTime t2(2001, 3, 12, 11, 22, 33, 999, CTime::eLocal); cout << "[" << t2.AsString() << "]" << endl; assert(t2.AsString() == "03/12/2001 11:22:33 "); CTime t3(CTime::eCurrent, CTime::eLocal); cout << "Local time [" << t3.AsString() << "]" << endl; CTime t4(CTime::eCurrent, CTime::eGmt); cout << "GMT time [" << t4.AsString() << "]" << endl; cout << endl; }} {{ cout << "Process timezone string" << endl; CTime t; t.SetFormat("M/D/Y h:m:s Z"); t="03/12/2001 11:22:33 GMT"; cout << "[" << t.AsString() << "]" << endl; assert(t.AsString() == "03/12/2001 11:22:33 GMT"); t="03/12/2001 11:22:33 "; cout << "[" << t.AsString() << "]" << endl; assert(t.AsString() == "03/12/2001 11:22:33 "); cout << endl; }} {{ cout << "Day of week" << endl; CTime t(2001, 4, 1); t.SetFormat("M/D/Y h:m:s w"); int i; for (i = 0; t <= CTime(2001, 4, 10); t++,i++) { cout << t.AsString() << " is " << t.DayOfWeek() << endl; assert(t.DayOfWeek() == (i%7)); } cout << endl; }} //------------------------------------------------------------------------ {{ cout << "Test GetTimeT" << endl; time_t timer=time(0); CTime tg(CTime::eCurrent, CTime::eGmt, CTime::eTZPrecisionDefault); CTime tl(CTime::eCurrent, CTime::eLocal, CTime::eTZPrecisionDefault); CTime t(timer); cout << "[" << t.AsString() << "] " << endl; cout << tg.GetTimeT()/3600 << " - " << tl.GetTimeT()/3600 << " - "; cout << t.GetTimeT()/3600 << " - " << timer/3600 << endl; assert(timer == tg.GetTimeT()); assert(timer == tl.GetTimeT()); assert(timer == t.GetTimeT()); cout << endl; for (int i = 0; i < 2; i++) { CTime tt(2001, 4, 1, i>0 ? 2 : 1, i>0 ? (i-1) : 59, 0, 0, CTime::eLocal, CTime::eHour); cout << tt.AsString() << " - " << tt.GetTimeT() / 3600 << endl; } for (int i = 0; i < 2; i++) { CTime tt(2001, 10, 28, i > 0 ? 1 : 0, i > 0 ? (i-1) : 59, 0, 0, CTime::eLocal, CTime::eHour); cout << tt.AsString() << " - " << tt.GetTimeT() / 3600 << endl; } cout << endl; }} //------------------------------------------------------------------------ {{ cout << "Test TimeZoneDiff (1)" << endl; CTime tw(2001, 1, 1, 12); CTime ts(2001, 6, 1, 12); cout << "[" << tw.AsString() << "] diff from GMT = " << \ tw.TimeZoneDiff() / 3600 << endl; assert(tw.TimeZoneDiff() / 3600 == -5); cout << "[" << ts.AsString() << "] diff from GMT = " << \ ts.TimeZoneDiff() / 3600 << endl; assert(ts.TimeZoneDiff()/3600 == -4); for (; tw < ts; tw++) { if ((tw.TimeZoneDiff() / 3600) == -4) { cout << "First daylight saving day = [" << \ tw.AsString() << "]" << endl; break; } } cout << endl; }} //------------------------------------------------------------------------ {{ cout << "Test TimeZoneDiff (2)" << endl; CTime tw(2001, 6, 1, 12); CTime ts(2002, 1, 1, 12); cout << "[" << tw.AsString() << "] diff from GMT = " << \ tw.TimeZoneDiff() / 3600 << endl; assert(tw.TimeZoneDiff() / 3600 == -4); cout << "[" << ts.AsString() << "] diff from GMT = " << \ ts.TimeZoneDiff() / 3600 << endl; assert(ts.TimeZoneDiff() / 3600 == -5); for (; tw < ts; tw++) { if ((tw.TimeZoneDiff() / 3600) == -5) { cout << "First non daylight saving day = [" << \ tw.AsString() << "]" << endl; break; } } cout << endl; }} //------------------------------------------------------------------------ {{ cout << "Test AdjustTime" << endl; CTime::SetFormat("M/D/Y h:m:s"); CTime t("04/01/2001 01:01:00"); CTime tn; t.SetTimeZonePrecision(CTime::eTZPrecisionDefault); cout << "init [" << t.AsString() << "]" << endl; t.SetTimeZoneFormat(CTime::eGmt); cout << "GMT" << endl; tn = t + 5; cout << "+5d [" << tn.AsString() << "]" << endl; assert(tn.AsString() == "04/06/2001 01:01:00"); tn = t + 40; cout << "+40d [" << tn.AsString() << "]" << endl; assert(tn.AsString() == "05/11/2001 01:01:00"); t.SetTimeZoneFormat(CTime::eLocal); cout << "Local eNone" << endl; t.SetTimeZonePrecision(CTime::eNone); tn=t+5; cout << "+5d [" << tn.AsString() << "]" << endl; assert(tn.AsString() == "04/06/2001 01:01:00"); tn=t+40; cout << "+40d [" << tn.AsString() << "]" << endl; assert(tn.AsString() == "05/11/2001 01:01:00"); t.SetTimeZonePrecision(CTime::eMonth); cout << "Local eMonth" << endl; tn = t + 5; cout << "+5d [" << tn.AsString() << "]" << endl; tn = t; tn.AddMonth(-1); cout << "-1m [" << tn.AsString() << "]" << endl; assert(tn.AsString() == "03/01/2001 01:01:00"); tn = t; tn.AddMonth(+1); cout << "+1m [" << tn.AsString() << "]" << endl; assert(tn.AsString() == "05/01/2001 02:01:00"); t.SetTimeZonePrecision(CTime::eDay); cout << "Local eDay" << endl; tn = t - 1; cout << "-1d [" << tn.AsString() << "]" << endl; assert(tn.AsString() == "03/31/2001 01:01:00"); tn++; cout << "+0d [" << tn.AsString() << "]" << endl; assert(tn.AsString() == "04/01/2001 01:01:00"); tn = t + 1; cout << "+1d [" << tn.AsString() << "]" << endl; assert(tn.AsString() == "04/02/2001 02:01:00"); cout << "Local eHour" << endl; t.SetTimeZonePrecision(CTime::eHour); tn = t; tn.AddHour(-3); CTime te = t; te.AddHour(3); cout << "-3h [" << tn.AsString() << "]" << endl; assert(tn.AsString() == "03/31/2001 22:01:00"); cout << "+3h [" << te.AsString() << "]" << endl; assert(te.AsString() == "04/01/2001 05:01:00"); CTime th = tn; th.AddHour(49); cout << "+49h [" << th.AsString() << "]" << endl; assert(th.AsString() == "04/03/2001 00:01:00"); for (int i = 0; i < 8; i++, tn.AddHour()) { cout << (((tn.TimeZoneDiff()/3600) == -4) ? " ":"*") \ << " [" << tn.AsString() << "]" << endl; } cout << endl; tn.AddHour(-1); for (int i = 0; i < 8; i++, tn.AddHour(-1)) { cout << (((tn.TimeZoneDiff()/3600) == -4) ? " ":"*") \ << " [" << tn.AsString() << "]" << endl; } cout << endl; tn = "10/28/2001 00:01:00"; cout << "init [" << tn.AsString() << "]" << endl; tn.SetTimeZonePrecision(CTime::eHour); te = tn; tn.AddHour(-3); te.AddHour(9); cout << "-3h [" << tn.AsString() << "]" << endl; assert(tn.AsString() == "10/27/2001 21:01:00"); cout << "+9h [" << te.AsString() << "]" << endl; assert(te.AsString() == "10/28/2001 08:01:00"); th = tn; th.AddHour(49); cout << "+49h [" << th.AsString() << "]" << endl; assert(th.AsString() == "10/29/2001 21:01:00"); tn.AddHour(+2); for (int i = 0; i < 10; i++, tn.AddHour()) { cout << (((tn.TimeZoneDiff()/3600) == -4) ? " ":"*") \ << " [" << tn.AsString() << "]" << endl; } cout << endl; tn.AddHour(-1); for (int i = 0; i < 10; i++, tn.AddHour(-1)) { cout << (((tn.TimeZoneDiff()/3600) == -4) ? " ":"*") \ << " [" << tn.AsString() << "]" << endl; } cout << endl; tn = "10/28/2001 09:01:00"; cout << "init [" << tn.AsString() << "]" << endl; tn.SetTimeZonePrecision(CTime::eHour); te = tn; tn.AddHour(-10); te.AddHour(+10); cout << "-10h [" << tn.AsString() << "]" << endl; assert(tn.AsString() == "10/28/2001 00:01:00"); cout << "+10h [" << te.AsString() << "]" << endl; assert(te.AsString() == "10/28/2001 19:01:00"); cout << endl; cout << endl; }}}//============================================================================//// TestGMTSpeedRun////============================================================================static void s_TestGMTSpeedRun(string comment, CTime::ETimeZone tz, CTime::ETimeZonePrecision tzp){ CTime t(CTime::eCurrent, tz, tzp); CStopWatch timer; double duration;#if defined NCBI_OS_MSWIN const long kCount=100000L;#elif defined NCBI_OS_UNIX const long kCount=10000L;#else // NCBI_OS_MAC const long kCount=100000L;#endif t.SetFormat("M/D/Y h:m:s"); t = "03/31/2001 00:00:00"; cout << "Minute add, "<< comment << endl; cout << "Iterations = " << kCount << endl; timer.Start(); for (long i = 0; i < kCount; i++) { t.AddMinute(); } duration = timer.Elapsed(); cout << "Duration = " << duration << " sec." << endl; cout << endl;}//============================================================================//// TestGMTSpeed////============================================================================static void s_TestGMTSpeed(void){ cout << "---------------------------" << endl; cout << "Test CTime Speed" << endl; cout << "---------------------------" << endl << endl; s_TestGMTSpeedRun("eLocal - eMinute", CTime::eLocal, CTime::eMinute); s_TestGMTSpeedRun("eLocal - eHour" , CTime::eLocal, CTime::eHour); s_TestGMTSpeedRun("eLocal - eMonth" , CTime::eLocal, CTime::eMonth); s_TestGMTSpeedRun("eLocal - eNone" , CTime::eLocal, CTime::eNone); s_TestGMTSpeedRun("eGmt - eNone" , CTime::eGmt, CTime::eNone);}//============================================================================//// MAIN////============================================================================int main(){ // Set err.-posting and tracing to maximum SetDiagTrace(eDT_Enable); SetDiagPostFlag(eDPF_All); SetDiagPostLevel(eDiag_Info); // Reinit global timezone variables tzset(); // Run tests try { s_TestMisc(); s_TestFormats(); s_TestGMT(); s_TestGMTSpeed(); } catch (CException& e) { ERR_POST(Fatal << e); } // Success return 0;}/* * =========================================================================== * $Log: test_ncbitime.cpp,v $ * Revision 1000.3 2004/06/01 19:10:23 gouriano * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R6.22 * * Revision 6.22 2004/05/14 13:59:51 gorelenk * Added include of ncbi_pch.hpp * * Revision 6.21 2004/03/25 17:35:17 ivanov * Added tests for 'z' time format * * Revision 6.20 2003/11/25 20:03:56 ivanov * Fixed misspelled eTZPrecisionDefault * * Revision 6.19 2003/11/25 19:56:38 ivanov * Renamed eDefault to eTZPrecisionDefault. * Some cosmetic changes. * * Revision 6.18 2003/10/03 18:27:20 ivanov * Added tests for month and day of week names conversion functions * * Revision 6.17 2003/07/15 19:37:50 vakatov * Added test with weekday and timezone * * Revision 6.16 2003/04/16 20:29:26 ivanov * Using class CStopWatch instead clock() * * Revision 6.15 2003/02/10 17:46:25 lavr * Added more checks for well-known dates * * Revision 6.14 2002/10/17 16:56:03 ivanov * Added tests for 'b' and 'B' time format symbols * * Revision 6.13 2002/07/15 18:17:26 gouriano * renamed CNcbiException and its descendents * * Revision 6.12 2002/07/11 14:18:29 gouriano * exceptions replaced by CNcbiException-type ones * * Revision 6.11 2002/04/16 18:49:09 ivanov * Centralize threatment of assert() in tests. * Added #include <test/test_assert.h>. CVS log moved to end of file. * * Revision 6.10 2001/09/27 18:02:45 ivanov * Fixed division at zero in test of speed CTime class on fast computers. * * Revision 6.9 2001/07/23 15:51:46 ivanov * Changed test for work with DB-time formats * * Revision 6.8 2001/07/06 15:08:36 ivanov * Added tests for DataBase-time's * * Revision 6.7 2001/05/29 16:14:02 ivanov * Return to nanosecond-revision. Corrected mistake of the work with local * time on Linux. Polish and improvement source code. * Renamed AsTimeT() -> GetTimerT(). * * Revision 6.6 2001/04/30 22:01:31 lavr * Rollback to pre-nanosecond-revision due to necessity to use * configure to figure out names of global variables governing time zones * * Revision 6.5 2001/04/29 03:06:10 lavr * #include <time.h>" moved from .cpp to ncbitime.hpp * * Revision 6.4 2001/04/27 20:39:47 ivanov * Tests for check Local/UTC time and Nanoseconds added. * Also speed test added. * * Revision 6.3 2000/11/21 18:15:05 butanaev * Fixed bug in operator ++/-- (int) * * Revision 6.2 2000/11/21 15:22:57 vakatov * Do not enforce "_DEBUG" -- it messes up the MSVC++ compilation * * Revision 6.1 2000/11/20 22:17:49 vakatov * Added NCBI date/time class CTime ("ncbitime.[ch]pp") and * its test suite ("test/test_ncbitime.cpp") * * =========================================================================== */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -