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

QDateTime::toUTC().toTime_t() returns seconds from epoch to local time not to UTC

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 4.7.4
    • Core: Date/Time
    • None
    • Qt 4.7.4 on Ubuntu 11.10 Using Qt Creator.

    Description

      QDateTime ja = QDateTime::currentDateTime(); // local time (Japan)
      qDebug() << "Local time :" << ja.toMSecsSinceEpoch() / 1000 << "\t" << ja.toString("ddd MMM d hh:mm:ss yyyy");
      
      QDateTime empty;
      // This, according to documentation should be UTC but is the same as above.
      qDebug() << "UTC time   :" << ja.toUTC().toTime_t() << "\t" << empty.fromTime_t(ja.toUTC().toTime_t()).toString("ddd MMM d hh:mm:ss yyyy");
      
      // Using time.h
      time_t local;
      time_t utc;
      memset(&local, 0, sizeof(time_t));
      memset(&utc, 0, sizeof(time_t));
      struct tm *putc = 0;
      struct tm *plocal = 0;
      
      time(&local);
      plocal = localtime(&local);
      printf("Local time : %ld \t %s", local, asctime(plocal));
      
      time(&utc);
      putc = gmtime(&utc);
      printf("UTC time   : %d \t %s", mktime(putc), asctime(putc));
      
      

      The output of the above is:

      Local time : 1323233093 	 "Wed Dec 7 13:44:53 2011" 
      UTC time   : 1323233093 	 "Wed Dec 7 13:44:53 2011"
      Local time : 1323233093 	 Wed Dec  7 13:44:53 2011
      UTC time   : 1323200693 	 Wed Dec  7 04:44:53 2011
      

      My system (Ubuntu 11.10) is configured properly and the "date" command on terminal returns the correct date and time: Wed Dec 7 13:47:04 JST 2011

      QDateTime::toUTC() converts the date to UTC time, but then calling toTime_t() on the converted date gives seconds in local time

      Attachments

        1. main.cpp
          1.0 kB
          andrea belvedere
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            Unassigned Unassigned
            andrea andrea belvedere
            Votes:
            2 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes