📄 test_ncbitime.cpp
字号:
/* * =========================================================================== * PRODUCTION $Log: test_ncbitime.cpp,v $ * PRODUCTION Revision 1000.3 2004/06/01 19:10:23 gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R6.22 * PRODUCTION * =========================================================================== *//* $Id: test_ncbitime.cpp,v 1000.3 2004/06/01 19:10:23 gouriano Exp $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Authors: Anton Butanayev, Denis Vakatov, Vladimir Ivanov * * File Description: * Test for CTime - the standard Date/Time class * */#include <ncbi_pch.hpp>#include <corelib/ncbitime.hpp>#include <stdio.h>#include <test/test_assert.h> /* This header must go last */USING_NCBI_SCOPE;//============================================================================//// TestMisc////============================================================================static void s_TestMisc(void){ cout << "---------------------------" << endl; cout << "Test Misc" << endl; cout << "---------------------------" << endl << endl; // Print current time {{ CTime t(CTime::eCurrent); cout << "[" << t.AsString() << "]" << endl; cout << "[" << (t++).AsString() << "]" << endl; cout << "[" << (t++).AsString() << "]" << endl; cout << "[" << (t++).AsString() << "]" << endl; cout << "[" << (++t).AsString() << "]" << endl; cout << "[" << (++t).AsString() << "]" << endl; }} // Month and Day name<->num conversion {{ assert(CTime::MonthNameToNum("Jan") == CTime::eJanuary); assert(CTime::MonthNameToNum("January") == 1); assert(CTime::MonthNameToNum("Dec") == CTime::eDecember); assert(CTime::MonthNameToNum("December") == 12); assert(CTime::MonthNumToName(CTime::eJanuary) == "January"); assert(CTime::MonthNumToName(1, CTime::eAbbr) == "Jan"); assert(CTime::MonthNumToName(CTime::eDecember, CTime::eFull) == "December"); assert(CTime::MonthNumToName(12,CTime::eAbbr) == "Dec"); assert(CTime::DayOfWeekNameToNum("Sun") == CTime::eSunday); assert(CTime::DayOfWeekNameToNum("Sunday") == 0); assert(CTime::DayOfWeekNameToNum("Sat") == CTime::eSaturday); assert(CTime::DayOfWeekNameToNum("Saturday") == 6); assert(CTime::DayOfWeekNumToName(CTime::eSunday) == "Sunday"); assert(CTime::DayOfWeekNumToName(0,CTime::eAbbr) == "Sun"); assert(CTime::DayOfWeekNumToName(CTime::eSaturday, CTime::eFull) == "Saturday"); assert(CTime::DayOfWeekNumToName(6,CTime::eAbbr) == "Sat"); cout << "Throw exception below:" << endl; try { CTime::MonthNameToNum("Month"); } catch (CTimeException& e) { NCBI_REPORT_EXCEPTION("", e); } }} // String <-> CTime conversion {{ {{ CTime t; cout << "[" << t.AsString() << "]" << endl; assert(t.AsString() == ""); }} {{ CTime t(2000, 365 / 2); CTime::SetFormat("M/D/Y h:m:s"); cout << "[" << t.AsString() << "]" << endl; assert(t.AsString() == "06/30/2000 00:00:00"); }} cout << endl; {{ CTime::SetFormat("M/D/Y"); cout << "Year 2000 problem:" << endl << endl; CTime t(1999, 12, 30); t++; cout << "[" << t.AsString() << "] " << endl; assert(t.AsString() == "12/31/1999"); t++; cout << "[" << t.AsString() << "] " << endl; assert(t.AsString() == "01/01/2000"); t++; cout << "[" << t.AsString() << "] " << endl; assert(t.AsString() == "01/02/2000"); t="02/27/2000"; t++; cout << "[" << t.AsString() << "] " << endl; assert(t.AsString() == "02/28/2000"); t++; cout << "[" << t.AsString() << "] " << endl; assert(t.AsString() == "02/29/2000"); t++; cout << "[" << t.AsString() << "] " << endl; assert(t.AsString() == "03/01/2000"); t++; cout << "[" << t.AsString() << "] " << endl; assert(t.AsString() == "03/02/2000"); }} cout << endl; {{ CTime::SetFormat("M/D/Y h:m:s"); cout << "String assignment:" << endl; try { CTime t("02/15/2000 01:12:33"); cout << "[" << t.AsString() << "]" << endl; assert(t.AsString() == "02/15/2000 01:12:33"); t = "3/16/2001 02:13:34"; cout << "[" << t.AsString() << "]" << endl; assert(t.AsString() == "03/16/2001 02:13:34"); } catch (CException& e) { NCBI_REPORT_EXCEPTION("",e); } }} cout << endl; }} // Addition {{ CTime::SetFormat("M/D/Y h:m:s.S"); {{ cout << "Adding Nanoseconds:" << endl; CTime t; for (CTime tmp(1999, 12, 31, 23, 59, 59, 999999995); tmp <= CTime(2000, 1, 1, 0, 0, 0, 000000003); t = tmp, tmp.AddNanoSecond(2)) { cout << "[" << tmp.AsString() << "] " << endl; } assert(t.AsString() == "01/01/2000 00:00:00.000000003"); }} cout << endl; {{ cout << "Current time with NS (10 cicles)" << endl; CTime t; for (int i = 0; i < 10; i++) { t.SetCurrent(); cout << "[" << t.AsString() << "] " << endl; } }} cout << endl; CTime::SetFormat("M/D/Y h:m:s"); {{ cout << "Adding seconds:" << endl; CTime t; for (CTime tmp(1999, 12, 31, 23, 59, 5); tmp <= CTime(2000, 1, 1, 0, 1, 20); t = tmp, tmp.AddSecond(11)) { cout << "[" << tmp.AsString() << "] " << endl; } assert(t.AsString() == "01/01/2000 00:01:17"); }} cout << endl; {{ cout << "Adding minutes:" << endl; for (CTime t(1999, 12, 31, 23, 45); t <= CTime(2000, 1, 1, 0, 15); t.AddMinute(11)) { cout << "[" << t.AsString() << "] " << endl; } }} cout << endl; {{ cout << "Adding hours:" << endl; for (CTime t(1999, 12, 31); t <= CTime(2000, 1, 1, 15); t.AddHour(11)) { cout << "[" << t.AsString() << "] " << endl; } }} cout << endl; {{ cout << "Adding months:" << endl; for (CTime t(1998, 12, 29); t <= CTime(1999, 4, 1); t.AddMonth()) { cout << "[" << t.AsString() << "] " << endl; } }} cout << endl; }} // Difference {{ CTime t7(2000, 10, 1, 12, 3, 45,1); CTime t8(2000, 10, 2, 14, 55, 1,2); cout << "[" << t7.AsString() << " - " << t8.AsString() << "]" << endl; printf("DiffDay = %.2f\n", t8.DiffDay (t7)); assert((t8.DiffDay(t7)-1.12) < 0.01); printf("DiffHour = %.2f\n", t8.DiffHour (t7)); assert((t8.DiffHour(t7)-26.85) < 0.01); printf("DiffMinute = %.2f\n", t8.DiffMinute(t7)); assert((t8.DiffMinute(t7)-1611.27) < 0.01); printf("DiffSecond = %d\n", t8.DiffSecond(t7)); assert(t8.DiffSecond(t7) == 96676); printf("DiffNanoSecond = %.0f\n", t8.DiffNanoSecond(t7)); }} // Datebase formats conversion {{ CTime t1(2000, 1, 1, 1, 1, 1, 10000000); CTime::SetFormat("M/D/Y h:m:s.S"); cout << endl << "DB time formats [" << t1.AsString() << "]" << endl; TDBTimeU dbu = t1.GetTimeDBU(); TDBTimeI dbi = t1.GetTimeDBI(); cout << "DBU days = " << dbu.days << endl; cout << "DBU time (min) = " << dbu.time << endl; cout << "DBI days = " << dbi.days << endl; cout << "DBI time (1/300 sec) = " << dbi.time << endl; cout << endl; CTime t2; t2.SetTimeDBU(dbu); cout << "Time from DBU = " << t2.AsString() << endl; t2.SetTimeDBI(dbi); cout << "Time from DBI = " << t2.AsString() << endl; CTime::SetFormat("M/D/Y h:m:s"); dbi.days = 37093; dbi.time = 12301381; t2.SetTimeDBI(dbi); cout << "Time from DBI = " << t2.AsString() << endl; assert(t2.AsString() == "07/23/2001 11:23:24"); }} // Set* functions {{ CTime::SetFormat("M/D/Y h:m:s"); CTime t(2000, 1, 31); t.SetMonth(2); assert(t.AsString() == "02/29/2000 00:00:00"); t.SetYear(2001); assert(t.AsString() == "02/28/2001 00:00:00"); t.SetMonth(4); assert(t.AsString() == "04/28/2001 00:00:00"); t.SetDay(31); assert(t.AsString() == "04/30/2001 00:00:00"); t.SetHour(6); assert(t.AsString() == "04/30/2001 06:00:00"); t.SetMinute(37); assert(t.AsString() == "04/30/2001 06:37:00"); t.SetSecond(59); assert(t.AsString() == "04/30/2001 06:37:59"); }} // Day of week {{ CTime t(1900, 1, 1); int i; for (i = 1; t <= CTime(2030, 12, 31); t++,i++) { assert(t.DayOfWeek() == (i%7)); } }} // Number of days in the month {{ CTime t(2000, 1, 31); assert(t.DaysInMonth() == 31); t.SetMonth(2); assert(t.DaysInMonth() == 29); t.SetYear(2001); assert(t.DaysInMonth() == 28); t.SetMonth(4); assert(t.DaysInMonth() == 30); }} // Week number in the year/month {{ CTime t(1970, 1, 1); int i; char buf[3]; time_t gt = t.GetTimeT(); for (i = 1; t <= CTime(2030, 12, 31); i++, t++, gt += 24*3600) { struct tm *today = gmtime(>); assert(today != 0); int week_num_rtl, week_num, month_week_num; // Sunday-based weeks strftime(buf, sizeof(buf), "%U", today); week_num_rtl = NStr::StringToInt(buf) + 1; week_num = t.YearWeekNumber(/*CTime::eSunday*/); assert(week_num_rtl == week_num); month_week_num = t.MonthWeekNumber(/*CTime::eSunday*/); assert(month_week_num >= 1 && month_week_num <= 6); // Monday-based weeks strftime(buf, sizeof(buf), "%W", today); week_num_rtl = NStr::StringToInt(buf) + 1; week_num = t.YearWeekNumber(CTime::eMonday); assert(week_num_rtl == week_num); month_week_num = t.MonthWeekNumber(CTime::eMonday); assert(month_week_num >= 1 && month_week_num <= 6); } }} cout << endl;}//============================================================================//// TestFormats////============================================================================static void s_TestFormats(void){ static const char* s_Fmt[] = { "w, D b Y h:m:s Z", "M/D/Y h:m:s", "M/D/Y h:m:s.S", "M/D/y h:m:s", "M/DY h:m:s", "M/Dy h:m:s", "M/D/Y hm:s", "M/D/Y h:ms", "M/D/Y hms", "MD/y h:m:s", "MD/Y h:m:s", "MYD m:h:s", "M/D/Y smh", "YMD h:sm", "yDM h:ms", "yMD h:ms", "D B Y h:m:s", "D b Y h:m:s", "M/D/Y h:m:s z", "M/D/Y Z h:m:s", "smhyMD", "y||||M++++D h===ms", " yM[][D h:,.,.,ms ", "\tkkkMy++D h:ms\n", 0 }; cout << "---------------------------" << endl; cout << "Test Formats" << endl; cout << "---------------------------" << endl << endl; for (const char** fmt = s_Fmt; *fmt; fmt++) { CTime t1(2001, 4, 2, 3, 4, 5, 0, strchr(*fmt, 'Z') ? CTime::eGmt : CTime::eLocal); CTime::SetFormat(*fmt); string t1_str = t1.AsString(); cout << "[" << t1_str << "]"; CTime::SetFormat("MDY__s"); CTime t2(t1_str, *fmt); cout << " --> [" << t1_str << "]" << endl; assert(t1 == t2); CTime::SetFormat(*fmt); string t2_str = t2; assert(t1_str.compare(t2_str) == 0); } // Check against well-known dates const char fmtstr[] = "M/D/Y h:m:s Z W"; {{ CTime t(2003, 2, 10, 20, 40, 30, 0, CTime::eGmt); t.SetFormat(fmtstr); string s = t.AsString(); assert(s.compare("02/10/2003 20:40:30 GMT Monday") == 0); }} {{ CTime t(1998, 2, 10, 20, 40, 30, 0, CTime::eGmt); t.SetFormat(fmtstr); string s = t.AsString(); assert(s.compare("02/10/1998 20:40:30 GMT Tuesday") == 0); }} {{ CTime t(2003, 3, 13, 15, 49, 30, 0, CTime::eGmt); t.SetFormat(fmtstr); string s = t.AsString(); assert(s.compare("03/13/2003 15:49:30 GMT Thursday") == 0); }} {{ CTime t(2001, 3, 13, 15, 49, 30, 0, CTime::eGmt); t.SetFormat(fmtstr); string s = t.AsString(); assert(s.compare("03/13/2001 15:49:30 GMT Tuesday") == 0); }} {{ CTime t(2002, 12, 31, 23, 59, 59, 0, CTime::eGmt); t.SetFormat(fmtstr); string s = t.AsString(); assert(s.compare("12/31/2002 23:59:59 GMT Tuesday") == 0); }} {{ CTime t(2003, 1, 1, 0, 0, 0, 0, CTime::eGmt);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -