QDateTime usage in my Qt project causing issue during Daylight Savings?

628 views Asked by At

I have created a project to read data before and after an event.

For example: Let D1, D2, D3 be QDateTime objects:
Let D1 be one with a value of 2014-03-09T2:30:00.
Let D2 be one with a value of 2014-03-09T1:30:00.
Let D3 be one with a value of 2014-03-09T3:30:00.

D1.date() will return QDate(2014,3,9).
D1.time() will return QTime(2,30).
D1.toString() will return “”.
(D1 > D2) is true.
(D1 < D2) is false.
(D1 > D3) is false.
(D1 < D3) is true.
(D1 == D1) is true.
D1.secsTo(X) is always 0.
X.secsTo(D1) is always 0, for all X.

In this case, D1.secsTo(X) is used to calculate the reports from 1:00 AM. The DST effect occurs between 2-3 AM.

What do I need to do to correct for this transition?

1

There are 1 answers

1
AudioBubble On

The documentation claims that the QDateTime class should handle DST automatically, but this appears to be a bug in Qt. I just tried with inputs you provided, and got 0 for the secsTo() method if either time was between 2-3 AM. It provided a correct offset if I changed the times to be after 3 AM.

For example:

D1 = 2:30 AM
D2 = 2:45 AM
D1.secsTo(D2) gives 0

D1 = 3:30 AM
D2 = 3:45 AM
D1.secsTo(D2) gives 900

I filed a bug report.

Edit

Your current solution should work once the bug is fixed. Another solution for the meantime could be to convert your timestamps to UTC before doing any calculations. The QDateTime class has a toUTC() method. I think that should avoid DST problems completely.