Commit 43c09c49 authored by Ronan's avatar Ronan

fix(MainDb): get correct UTC time with MySQL

parent 518dd2aa
......@@ -124,8 +124,8 @@ namespace Utils {
return object;
}
LINPHONE_PUBLIC std::tm getTimeTAsTm (time_t time);
LINPHONE_PUBLIC time_t getTmAsTimeT (const std::tm &time);
LINPHONE_PUBLIC std::tm getTimeTAsTm (time_t t);
LINPHONE_PUBLIC time_t getTmAsTimeT (const std::tm &t);
LINPHONE_PUBLIC std::string localeToUtf8 (const std::string &str);
LINPHONE_PUBLIC std::string utf8ToLocale (const std::string &str);
......
......@@ -44,8 +44,6 @@ private:
// Misc helpers.
// ---------------------------------------------------------------------------
static time_t getTmAsTimeT (const tm &t);
std::shared_ptr<AbstractChatRoom> findChatRoom (const ChatRoomId &chatRoomId) const;
// ---------------------------------------------------------------------------
......
......@@ -233,12 +233,6 @@ static string buildSqlEventFilter (
// Misc helpers.
// -----------------------------------------------------------------------------
time_t MainDbPrivate::getTmAsTimeT (const tm &t) {
tm t2 = t;
t2.tm_isdst = 0;
return Utils::getTmAsTimeT(t2);
}
shared_ptr<AbstractChatRoom> MainDbPrivate::findChatRoom (const ChatRoomId &chatRoomId) const {
L_Q();
shared_ptr<AbstractChatRoom> chatRoom = q->getCore()->findChatRoom(chatRoomId);
......@@ -629,7 +623,7 @@ shared_ptr<EventLog> MainDbPrivate::selectConferenceChatMessageEvent (
dChatMessage->forceFromAddress(IdentityAddress(row.get<string>(3)));
dChatMessage->forceToAddress(IdentityAddress(row.get<string>(4)));
dChatMessage->setTime(MainDbPrivate::getTmAsTimeT(row.get<tm>(5)));
dChatMessage->setTime(dbSession.getTime(row, 5));
dChatMessage->setImdnMessageId(row.get<string>(6));
dChatMessage->setPositiveDeliveryNotificationRequired(!!row.get<int>(14));
dChatMessage->setDisplayNotificationRequired(!!row.get<int>(15));
......@@ -2025,7 +2019,7 @@ list<MainDb::ParticipantState> MainDb::getChatMessageParticipantsByImdnState (
list<MainDb::ParticipantState> result;
for (const auto &row : rows)
result.emplace_back(IdentityAddress(row.get<string>(0)), state, MainDbPrivate::getTmAsTimeT(row.get<tm>(1)));
result.emplace_back(IdentityAddress(row.get<string>(0)), state, d->dbSession.getTime(row, 1));
return result;
};
}
......@@ -2377,8 +2371,8 @@ list<shared_ptr<AbstractChatRoom>> MainDb::getChatRooms () const {
continue;
}
tm creationTime = row.get<tm>(3);
tm lastUpdateTime = row.get<tm>(4);
time_t creationTime = d->dbSession.getTime(row, 3);
time_t lastUpdateTime = d->dbSession.getTime(row, 4);
int capabilities = row.get<int>(5);
string subject = row.get<string>(6, "");
unsigned int lastNotifyId = getBackend() == Backend::Mysql
......@@ -2475,8 +2469,8 @@ list<shared_ptr<AbstractChatRoom>> MainDb::getChatRooms () const {
continue; // Not fetched.
AbstractChatRoomPrivate *dChatRoom = chatRoom->getPrivate();
dChatRoom->setCreationTime(MainDbPrivate::getTmAsTimeT(creationTime));
dChatRoom->setLastUpdateTime(MainDbPrivate::getTmAsTimeT(lastUpdateTime));
dChatRoom->setCreationTime(creationTime);
dChatRoom->setLastUpdateTime(lastUpdateTime);
lInfo() << "Found chat room in DB: (peer=" <<
chatRoomId.getPeerAddress().asString() << ", local=" << chatRoomId.getLocalAddress().asString() << ").";
......
......@@ -130,17 +130,19 @@ string DbSession::currentTimestamp () const {
switch (d->backend) {
case DbSessionPrivate::Backend::Mysql:
return " CURRENT_TIMESTAMP";
case DbSessionPrivate::Backend::Sqlite3:
case DbSessionPrivate::Backend::Sqlite3: {
// Ugly hack but Sqlite3 does not allow table alteration where we add a date column using a default value
// of CURRENT_TIMESTAMP
{
const tm &now = Utils::getTimeTAsTm(std::time(nullptr));
const size_t bufSize = 22;
char buffer[bufSize];
snprintf(buffer, bufSize, "'%d-%02d-%02d %02d:%02d:%02d'",
now.tm_year + 1900, now.tm_mon + 1, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec);
return buffer;
}
// of CURRENT_TIMESTAMP.
const tm &now = Utils::getTimeTAsTm(std::time(nullptr));
char buffer[22];
snprintf(
buffer,
sizeof buffer,
"'%d-%02d-%02d %02d:%02d:%02d'",
now.tm_year + 1900, now.tm_mon + 1, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec
);
return buffer;
}
case DbSessionPrivate::Backend::None:
return "";
}
......@@ -253,4 +255,22 @@ long long DbSession::resolveId (const soci::row &row, int col) const {
return 0;
}
time_t DbSession::getTime (const soci::row &row, int col) const {
L_D();
tm t = row.get<tm>(col);
switch (d->backend) {
case DbSessionPrivate::Backend::Mysql:
return mktime(&t); // Local time to UTC. For Mysql the local time is used, not a problem.
case DbSessionPrivate::Backend::Sqlite3:
t.tm_isdst = 0; // Sqlite3 = UTC. Soci can set the DST field, it's not good for us...
return Utils::getTmAsTimeT(t);
case DbSessionPrivate::Backend::None:
return 0;
}
L_ASSERT(false);
return 0;
}
LINPHONE_END_NAMESPACE
......@@ -60,6 +60,8 @@ public:
long long resolveId (const soci::row &row, int col) const;
std::time_t getTime (const soci::row &row, int col) const;
private:
DbSessionPrivate *mPrivate;
......
......@@ -187,16 +187,16 @@ string Utils::trim (const string &str) {
// -----------------------------------------------------------------------------
tm Utils::getTimeTAsTm (time_t time) {
tm Utils::getTimeTAsTm (time_t t) {
#ifdef _WIN32
return *gmtime(&time);
return *gmtime(&t);
#else
tm result;
return *gmtime_r(&time, &result);
return *gmtime_r(&t, &result);
#endif
}
time_t Utils::getTmAsTimeT (const tm &time) {
time_t Utils::getTmAsTimeT (const tm &t) {
time_t result;
#if defined(LINPHONE_WINDOWS_UNIVERSAL) || defined(LINPHONE_MSC_VER_GREATER_19)
......@@ -206,11 +206,11 @@ time_t Utils::getTmAsTimeT (const tm &time) {
#endif
#if TARGET_IPHONE_SIMULATOR
result = timegm(&const_cast<tm &>(time));
result = timegm(&const_cast<tm &>(t));
adjustTimezone = 0;
#else
// mktime uses local time => It's necessary to adjust the timezone to get an UTC time.
result = mktime(&const_cast<tm &>(time));
result = mktime(&const_cast<tm &>(t));
#if defined(LINPHONE_WINDOWS_UNIVERSAL) || defined(LINPHONE_MSC_VER_GREATER_19)
_get_timezone(&adjustTimezone);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment