Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-45792

QDateTime does not respect QTimeZone if QTime is set

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P2: Important
    • Resolution: Done
    • Affects Version/s: 5.2.1, 5.4.1
    • Fix Version/s: 5.5.0 Alpha, 5.5.0
    • Component/s: Core: Date/Time
    • Labels:
      None
    • Environment:
      Linux Ubuntu 14.04 x86_64

      Description

      It is impossible to get correct timestamp from QDateTime if an instance is constructed in specific timezone and QTime is either provided to the constructor or set via setTime(). However if QDateTime is constructed using fromTime_t in explicit timezone, all fields seem to be correct.

      The following example demonstrates the issue

      #include <QDateTime>
      #include <QTimeZone>
      #include <QDebug>
      
      int main() {
      	QTimeZone tz6("UTC+06:00");
      	time_t timestamp20150415 = 1429034400;
      	QDateTime timePureFromTime_t = QDateTime::fromTime_t(timestamp20150415, tz6);
      	qDebug() << "pure fromTime_t: Hours (expected 0):"
      		<< timePureFromTime_t.time().hour();
      	qDebug() << "timestamp preserved:"
      		<< (timestamp20150415 == timePureFromTime_t.toTime_t());
      
      	time_t randomTimestamp = 1429072085;
      	// setTime results in wrong timestamp
      	QDateTime timeSetTime = QDateTime::fromTime_t(randomTimestamp, tz6);
      	timeSetTime.setTime(QTime(0, 0, 0));
      	QDateTime timeSetTimeFromTime_t
      		= QDateTime::fromTime_t(timeSetTime.toTime_t(), tz6);
      	qDebug() << "setTime(QTime(0, 0, 0)): Hours (expected 0):"
      		<< timeSetTimeFromTime_t.time().hour();
      
      	// incorrect timestamp for specified date and time
      	QDateTime timeConstruct
      		= QDateTime(QDate(2015, 4, 15), QTime(0, 0, 0), tz6);
      	QDateTime timeConstructFromTime_t
      		= QDateTime::fromTime_t(timeConstruct.toTime_t(), tz6);
      	qDebug() << "QDateTime(QDate, QTime, QTimeZone): Hours (expected 0):"
      		<< timeConstructFromTime_t.time().hour();
      	return 0;
      }
      

      Output:

      pure fromTime_t: Hours (expected 0): 0 
      timestamp preserved: true 
      setTime(QTime(0, 0, 0)): Hours (expected 0): 6 
      QDateTime(QDate, QTime, QTimeZone): Hours (expected 0): 6 
      

      I do not expect 6 hours since 0 hours is passed to QTime constructor.

      The origin of the bug may be similar to QTBUG-39734

        Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            thiago Thiago Macieira
            Reporter:
            nikulin Max
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Gerrit Reviews

                There are no open Gerrit changes