PostgreSQL store QDateTime UTC
-
wrote on 21 Jan 2020, 14:10 last edited by mk33
Hello, I try to solve the problem by storing time in the database. I need time in the database in UTC. I use database type timestamp without time zone.
I try to store two values (currentDateTime and curentDateTimeUtc) for local time 14:57.
QDateTime(2020-01-21 14:57:17.770 Central Europe Standard Time Qt::TimeSpec(LocalTime))
QDateTime(2020-01-21 13:57:17.775 UTC Qt::TimeSpec(UTC))const QString queryText = "INSERT INTO dd_records (\"time\", substation, author, text, note, type)\n" \ "VALUES (:time, :substation, :author, :text, :note, :type);"; QSqlQuery query(database_); query.prepare(queryText); query.bindValue(":time", rec.time_); query.bindValue(":substation", rec.subStation_); query.bindValue(":author", rec.author_); query.bindValue(":text", rec.event_); query.bindValue(":note", rec.note_); query.bindValue(":type", rec.type_); bool ok = query.exec();
But both values are in the database with the same time
"2020-01-21 14:57:17.775"
"2020-01-21 14:57:17.77"What is a problem, I need time 13:57.
PostgreSQL 11.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36), 64-bit
Qt 5.6.1 MSVC2010 -
wrote on 21 Jan 2020, 14:41 last edited by
can you try:
query.bindValue(":time", rec.time_.toUtc().toString(Qt::ISODateWithMs));
? -
wrote on 21 Jan 2020, 15:20 last edited by
@VRonin said in PostgreSQL store QDateTime UTC:
can you try:
query.bindValue(":time", rec.time_.toUtc().toString(Qt::ISODateWithMs));
?Thank you, problem solved.
1/3