Results 1 to 3 of 3

Thread: QDateTime UTC Time_t problem

  1. #1
    Join Date
    Apr 2011
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QDateTime UTC Time_t problem

    Hello,
    shouldn't line 2 and line 4 below print the same UTC time?
    Line 2 prints the correct UTC time, but line 4 prints local time.

    The variable utc is holding UTC time, then calling utc.toTime_t() should return number of seconds from epoch to UTC, is this right ?
    Then on line 4, a new QDateTime is built using the number of seconds from epoch to UTC, so shouldn't this print UTC time again?

    Qt Code:
    1. QDateTime utc = QDateTime::currentDateTimeUtc();
    2. qDebug() << utc;
    3. uint time = utc.toTime_t();
    4. qDebug() << QDateTime::fromTime_t(time); // this prints local time which is UTC + 9 hours
    To copy to clipboard, switch view to plain text mode 

    output:
    Qt Code:
    1. QDateTime("Mon Feb 6 04:00:48 2012")
    2. QDateTime("Mon Feb 6 13:00:48 2012")
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QDateTime UTC Time_t problem

    Line 2 and 4 do print the same time, i.e they have the same time_t value which is always UTC, they are just formatted for different time zones.

    QDateTime::fromTime_t() returns a QDateTime set to the local time corresponding to specified seconds after the epoch, i.e. it has Qt::LocalTime as its time spec, and this drives the string representation.

    Try this:
    Qt Code:
    1. qDebug() << "Local:" << QDateTime::currentDateTime();
    2. QDateTime utc = QDateTime::currentDateTimeUtc();
    3. qDebug() << "UTC:" << utc;
    4. uint time = utc.toTime_t();
    5.  
    6. QDateTime result(QDate(), QTime(), Qt::UTC); // a null UTC time
    7. result.setTime_t(time);
    8. qDebug() << "Result:" << result;
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. Local: QDateTime("Mon Feb 6 15:29:30 2012")
    2. UTC: QDateTime("Mon Feb 6 05:29:30 2012")
    3. Result: QDateTime("Mon Feb 6 05:29:30 2012")
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2011
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDateTime UTC Time_t problem

    I see, thanks Chris,

    as I feel that local time is a special case of UTC time, I would have used Qt::UTC as (default) time spec rather than Qt::LocalTime.

    anyway thanks for the clarification
    cheers

Similar Threads

  1. QDateTime .setTime problem
    By sergiofil in forum Newbie
    Replies: 1
    Last Post: 28th April 2011, 16:40
  2. Why can I not generate a valid Time_t?
    By philwinder in forum Qt Programming
    Replies: 2
    Last Post: 11th March 2009, 16:19
  3. QDateTime problem
    By Majdi in forum Newbie
    Replies: 11
    Last Post: 28th January 2009, 15:57
  4. Problem with comparing two QDateTime objects
    By NoRulez in forum Qt Programming
    Replies: 1
    Last Post: 15th May 2008, 11:27
  5. What are clock_t and time_t?
    By jamadagni in forum General Programming
    Replies: 7
    Last Post: 12th January 2006, 15:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.