<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.kde.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.6"
          urlbase="https://bugs.kde.org/"
          
          maintainer="sysadmin@kde.org"
>

    <bug>
          <bug_id>483060</bug_id>
          
          <creation_ts>2024-03-10 03:01:43 +0000</creation_ts>
          <short_desc>Akonadi sending UTC timestamps as local time to MySQL (and this failing around DST change) - QMYSQL bug?</short_desc>
          <delta_ts>2026-09-01 17:14:46 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>3</classification_id>
          <classification>Frameworks and Libraries</classification>
          <product>Akonadi</product>
          <component>server</component>
          <version>5.24.4</version>
          <rep_platform>Other</rep_platform>
          <op_sys>Linux</op_sys>
          <bug_status>UNCONFIRMED</bug_status>
          <resolution></resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>NOR</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>0</everconfirmed>
          <reporter name="Thiago Macieira">thiago</reporter>
          <assigned_to name="kdepim bugs">pim-bugs-null</assigned_to>
          <cc>carl</cc>
    
    <cc>Ch.Ehrlicher</cc>
    
    <cc>joshua</cc>
    
    <cc>kidakeh500</cc>
          
          <cf_commitlink>https://invent.kde.org/pim/akonadi/-/commit/c98b05b79aa849ebba2f4c309484428dd08df042</cf_commitlink>
          <cf_versionfixedin></cf_versionfixedin>
          <cf_sentryurl></cf_sentryurl>
          <votes>0</votes>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>2298084</commentid>
    <comment_count>0</comment_count>
    <who name="Thiago Macieira">thiago</who>
    <bug_when>2024-03-10 03:01:43 +0000</bug_when>
    <thetext>SUMMARY
KMail started failing to send emails for me with a message saying the item couldn&apos;t be stored. I did find this in the journal:

Mar 09 21:10:09 tjmaciei-mobl5 akonadiserver[745525]: org.kde.pim.akonadiserver: DATABASE ERROR:
Mar 09 21:10:09 tjmaciei-mobl5 akonadiserver[745525]: org.kde.pim.akonadiserver:   Error code: &quot;1292&quot;
Mar 09 21:10:09 tjmaciei-mobl5 akonadiserver[745525]: org.kde.pim.akonadiserver:   DB error:  &quot;Incorrect datetime value: &apos;2024-03-10 02:10:09.341000&apos; for column `akonadi`.`pimitemtable`.`atime` at row 1&quot;
Mar 09 21:10:09 tjmaciei-mobl5 akonadiserver[745525]: org.kde.pim.akonadiserver:   Error text: &quot;Incorrect datetime value: &apos;2024-03-10 02:10:09.341000&apos; for column `akonadi`.`pimitemtable`.`atime` at row 1 QMYSQL3: Unable to execute statement&quot;
Mar 09 21:10:09 tjmaciei-mobl5 akonadiserver[745525]: org.kde.pim.akonadiserver:   Values: QMap((&quot;:0&quot;, QVariant(QDateTime, QDateTime(2024-03-10 02:10:09.341 UTC Qt::UTC)))(&quot;:1&quot;, QVariant(qlonglong, 770803)))
Mar 09 21:10:09 tjmaciei-mobl5 akonadiserver[745525]: org.kde.pim.akonadiserver:   Query: &quot;UPDATE PimItemTable SET atime = :0 WHERE ( ( PimItemTable.id = :1 ) )&quot;
Mar 09 21:10:09 tjmaciei-mobl5 akonadiserver[745525]: org.kde.pim.akonadiserver: Unable to update item access time

After stressing a bit about whether this was a database or filesystem corruption, I figured out that the issue was the actual time in that timestamp. MySQL rejects it:

MariaDB [akonadi]&gt; UPDATE PimItemTable SET atime = timestamp(&apos;2024-03-10 02:10:55.625000&apos;) WHERE ( ( PimItemTable.id = 770804 ) );
ERROR 1292 (22007): Incorrect datetime value: &apos;2024-03-10 02:10:55.625000&apos; for column `akonadi`.`pimitemtable`.`atime` at row 1

If you look at the error message in the log, as well as the journalctl timestamp, 02:10:55 is an UTC time. But when this was sent to the DB server, there&apos;s no information that it is UTC. Thus the server rejects it thinking it&apos;s local time.


STEPS TO REPRODUCE
1. Set your system time such that the universal time, if computed as local time, would fall into the spring forward gap
2. Try to send an email

OBSERVED RESULT
Email sending fails.

EXPECTED RESULT
Email sending works.

SOFTWARE/OS VERSIONS
KDE Plasma Version: 5.27.10
KDE Frameworks Version: 5.115.0
Qt Version: 5.15.2

ADDITIONAL INFORMATION
This is probably a QMYSQL bug, because the QVariant as printed did have UTC time. The toMySqlDate() function was added for Qt 6.6, but I don&apos;t see a way to specify the timezone with MYSQL_TIME.

But maybe Akonadi can work around the issue by starting mysqld with TZ=UTC.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2298088</commentid>
    <comment_count>1</comment_count>
    <who name="Thiago Macieira">thiago</who>
    <bug_when>2024-03-10 03:04:25 +0000</bug_when>
    <thetext>Or use the SET GLOBAL time_zone command.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2299029</commentid>
    <comment_count>2</comment_count>
    <who name="Christophe Marin">christophe</who>
    <bug_when>2024-03-11 17:23:40 +0000</bug_when>
    <thetext>Duplicate of 483061?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2299136</commentid>
    <comment_count>3</comment_count>
    <who name="Thiago Macieira">thiago</who>
    <bug_when>2024-03-11 19:20:43 +0000</bug_when>
    <thetext>(In reply to Christophe Marin from comment #2)
&gt; Duplicate of 483061?

Looks like two people experienced the same problem at around the same time, yes.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2299139</commentid>
    <comment_count>4</comment_count>
    <who name="Thiago Macieira">thiago</who>
    <bug_when>2024-03-11 19:22:13 +0000</bug_when>
    <thetext>*** Bug 483061 has been marked as a duplicate of this bug. ***</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2299142</commentid>
    <comment_count>5</comment_count>
    <who name="Thiago Macieira">thiago</who>
    <bug_when>2024-03-11 19:24:15 +0000</bug_when>
    <thetext>My reading of the MySQL API is that there&apos;s no way to pass the timestamp in prepared queries as UTC. You can only pass it as a local timestamp. Feels so 1980s...</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2299155</commentid>
    <comment_count>6</comment_count>
    <who name="Joshua J. Kugler">joshua</who>
    <bug_when>2024-03-11 19:35:42 +0000</bug_when>
    <thetext>When I was debugging this, someone in the #maria IRC channel said:
&gt; As a workaround SQL mode flag ALLOW_INVALID_DATES should help, but it is better to store the UTC values properply as UTC.

So, there is that...but that does seem ugly. Can you even specify a TZ on a TIMESTAMP column in MySQL/MariaDB (it&apos;s been too long since I&apos;ve actively used MySQL).

Another idea: have Akonadi set the client time zone to UTC when it connects. BUT: I don&apos;t know what that might do to the times it returns in other queries, so would require investigation, and if code would have to be changed elsewhere.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2299160</commentid>
    <comment_count>7</comment_count>
    <who name="Joshua J. Kugler">joshua</who>
    <bug_when>2024-03-11 19:37:40 +0000</bug_when>
    <thetext>(In reply to Christophe Marin from comment #2)
&gt; Duplicate of 483061?

That&apos;s quite funny. Mine is even the very next bug filed after this one. :)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2299185</commentid>
    <comment_count>8</comment_count>
    <who name="Thiago Macieira">thiago</who>
    <bug_when>2024-03-11 20:03:42 +0000</bug_when>
    <thetext>&gt; Another idea: have Akonadi set the client time zone to UTC when it connects. BUT: I don&apos;t know what that might do to the times it returns in other queries, so would require investigation, and if code would have to be changed elsewhere.

Applying that fix to QMYSQL instead. https://codereview.qt-project.org/c/qt/qtbase/+/546954</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2299501</commentid>
    <comment_count>9</comment_count>
    <who name="Daniel Vrátil">dvratil</who>
    <bug_when>2024-03-12 07:50:45 +0000</bug_when>
    <thetext>Git commit c98b05b79aa849ebba2f4c309484428dd08df042 by Daniel Vrátil.
Committed on 12/03/2024 at 07:41.
Pushed by dvratil into branch &apos;master&apos;.

Fix handling of UTC vs. local time for database engines

Each database handles datetimes slightly differently. Our biggest
problem was MySQL, where the QMYSQL driver strips timezone, so
the MySQL server assumes the time is a local time. Akonadi was,
however, passing a UTC time, which then MySQL tried to interpret
as local time, which lead to two issues:
  1) it failed if the time was not a valid time in local timezone (DST)
  2) it required special handling when reading the time back from DB
     because MySQL returned UTC time but with local timezone set.

So instead we make sure we handle datetime properly when insert it
into the database. This can be handled from a singular place in
QueryBuilder, so it&apos;s fully transparent to the caller.

There&apos;s no migration for the existing database, since at worst the
time is slightly off and shouldn&apos;t have actual impact on functionality.

This has also been fixed independently in Qt, so in the future
we might be able to drop this completely.

M  +2    -0    autotests/server/CMakeLists.txt
A  +139  -0    autotests/server/dbdatetimetest.cpp     [License: LGPL(v2.0+)]
A  +1    -0    autotests/server/unittestenv
M  +1    -1    src/server/dbmigrator/dbmigrator.cpp
M  +1    -1    src/server/handler/itemfetchhelper.cpp
M  +0    -3    src/server/storage/entities-source.xsl
M  +0    -3    src/server/storage/entities.xsl
M  +6    -1    src/server/storage/querybuilder.cpp
M  +34   -24   src/server/utils.h

https://invent.kde.org/pim/akonadi/-/commit/c98b05b79aa849ebba2f4c309484428dd08df042</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2543551</commentid>
    <comment_count>10</comment_count>
    <who name="kidakeh">kidakeh500</who>
    <bug_when>2026-09-01 15:48:15 +0000</bug_when>
    <thetext>fix (commit 2781c3b) introduced a new bug in QMYSQL driver.
The setUtcTimeZone() function added in that commit has a reversed condition:

if (mysql_stmt_prepare(stmt.get(), query, sizeof(query) - 1))  // prepare returns 0 on success
    mysql_stmt_execute(stmt.get());                            // so execute only runs when prepare fails
As a result, SET time_zone = &apos;+00:00&apos; is never actually executed. MySQL general query log shows only Prepare and Close stmt, with no Execute:
                    23 Query     SET NAMES utf8mb4
                    23 Init DB   digikam
                    23 Prepare   SELECT ? + ?
                    23 Close stmt
                    23 Prepare   SET time_zone = &apos;+00:00&apos;
                    23 Close stmt
                    23 Prepare   SELECT DISTINCT Images.id ... WHERE ...
                    23 Execute   ... TIMESTAMP&apos;2026-01-31 16:00:00&apos; ...</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2543558</commentid>
    <comment_count>11</comment_count>
    <who name="kidakeh">kidakeh500</who>
    <bug_when>2026-09-01 16:22:09 +0000</bug_when>
    <thetext>Sorry, this seems to be a bug in Qt, and I don&apos;t know where to submit it. I got here through a link.
https://github.com/qt/qtbase/commit/2781c3b6248fe4410a7afffd41bad72d8567fc95</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2543569</commentid>
    <comment_count>12</comment_count>
    <who name="Thiago Macieira">thiago</who>
    <bug_when>2026-09-01 17:14:46 +0000</bug_when>
    <thetext>(In reply to kidakeh from comment #11)
&gt; Sorry, this seems to be a bug in Qt, and I don&apos;t know where to submit it. I
&gt; got here through a link.
&gt; https://github.com/qt/qtbase/commit/2781c3b6248fe4410a7afffd41bad72d8567fc95

https://bugreports.qt.io/</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>