Important: Please read the Qt Code of Conduct -

PostgreSQL store QDateTime UTC

  • 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.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

  • can you try:
    query.bindValue(":time", rec.time_.toUtc().toString(Qt::ISODateWithMs));?

  • @VRonin said in PostgreSQL store QDateTime UTC:

    can you try:
    query.bindValue(":time", rec.time_.toUtc().toString(Qt::ISODateWithMs));?

    Thank you, problem solved.

Log in to reply