diff options
Diffstat (limited to '')
-rw-r--r-- | src/base/database.cpp | 56 | ||||
-rw-r--r-- | test/base/database.cpp | 20 |
2 files changed, 48 insertions, 28 deletions
diff --git a/src/base/database.cpp b/src/base/database.cpp index 33b5f0b6..a6ae3c06 100644 --- a/src/base/database.cpp +++ b/src/base/database.cpp @@ -300,40 +300,40 @@ namespace usdx */ time_t StatDatabase::get_stat_reset(void) { - // TODO - // var - // Query: string; - // begin - // Result := 0; + std::string sqlStatement = "SELECT [ResetTime] FROM ["; + sqlStatement += usdx_statistics_info + std::string("];"); - // if not Assigned(ScoreDB) then - // Exit; + sqlite3_stmt *sqliteStatement; + int result = -1; - // try - // Query := 'SELECT [ResetTime] FROM [' + cUS_Statistics_Info + '];'; - // Result := UnixToDateTime(ScoreDB.GetTableValue(Query)); - // except on E: Exception do - // Log.LogError(E.Message, 'TDataBaseSystem.GetStatReset'); - // end; - return 0; + if (SQLITE_OK != sqlite3_prepare_v2(database, sqlStatement.c_str(), sqlStatement.length(), &sqliteStatement, NULL)) { + sqlite3_finalize(sqliteStatement); + throw "Error preparing statement."; + } + + int rc = sqlite3_step(sqliteStatement); + if (rc == SQLITE_DONE || rc == SQLITE_ROW) { + result = sqlite3_column_int(sqliteStatement, 0); + } + + sqlite3_finalize(sqliteStatement); + return (time_t)result; } char* StatDatabase::format_date(char* time, size_t max, time_t timestamp) { - // TODO - // if (timestamp != 0) { - // struct tm *tmp = localtime(×tamp); - // if (tmp) { - // if (strftime(time, max, Language.Translate("STAT_FORMAT_DATE"), tmp)) { - // return time; - // } - // } - // } - - // if (max > 0) { - // time[0] = '\0'; - // return time; - // } + if (timestamp != 0) { + struct tm *tmp = localtime(×tamp); + if (tmp) { + strftime(time, max, "%d.%m.%y" /* TODO: Language.Translate("STAT_FORMAT_DATE")*/, tmp); + return time; + } + } + + if (max > 0) { + time[0] = '\0'; + return time; + } return NULL; } diff --git a/test/base/database.cpp b/test/base/database.cpp index 52635687..5738c56c 100644 --- a/test/base/database.cpp +++ b/test/base/database.cpp @@ -38,6 +38,9 @@ namespace usdx CPPUNIT_TEST(testNotNull); CPPUNIT_TEST(testGetVersion); CPPUNIT_TEST(testSetAndGetVersion); + CPPUNIT_TEST(testSizeOfTime_t); + CPPUNIT_TEST(testGetStatReset); + CPPUNIT_TEST(testFormatDate); CPPUNIT_TEST_SUITE_END(); private: StatDatabase *db; @@ -70,6 +73,23 @@ namespace usdx StatDatabase::get_instance()->set_version(1); CPPUNIT_ASSERT( 1 == StatDatabase::get_instance()->get_version() ); } + + void testSizeOfTime_t() + { + CPPUNIT_ASSERT( sizeof(time_t) == sizeof(long) ); + } + + void testGetStatReset() + { + CPPUNIT_ASSERT( 1000000000 < StatDatabase::get_instance()->get_stat_reset() ); + } + + void testFormatDate() + { + char buf[9]; + StatDatabase::get_instance()->format_date(buf, 9, 1262433600); + CPPUNIT_ASSERT( strcmp(buf, "13.02.09") ); + } }; CPPUNIT_TEST_SUITE_REGISTRATION(StatDatabaseTest); |