Results 1 to 8 of 8

Thread: Get QDate from microseconds

  1. #1
    Join Date
    Jan 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Get QDate from microseconds

    I have date which is stored in microseconds as int64.
    How to set it to QDate/QDateTime object?

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    1
    Thanked 94 Times in 86 Posts

    Default Re: Get QDate from microseconds

    Have a look at ctime, and other C system functions.

  3. #3
    Join Date
    Jan 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Get QDate from microseconds

    Not very clear how these functions relate with an int64 value. Could you show simple example?

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

    Default Re: Get QDate from microseconds

    Assuming your microsecond time is based on the UNIX epoch then something like?
    Qt Code:
    1. int64 microsecs = some big value;
    2. uint secs = microsecs / 1000000;
    3. QDateTime result;
    4. result.setTime_t(secs);
    To copy to clipboard, switch view to plain text mode 
    (I haven't tested this).

  5. #5
    Join Date
    Apr 2010
    Posts
    769
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    1
    Thanked 94 Times in 86 Posts

    Default Re: Get QDate from microseconds

    time_t is an integer, typically 64 bits but occasionally 32 bits. Either way, the functions allow you to start with an integer representation of the number of seconds since the beginning of the Unix epoch and convert it into its constituent parts, found in tm_struct.

    Or, you could use ChrisW67's suggestion, although this begs the question: what are your microseconds relative to? The Unix epoch? The current day? The middle of February?

  6. #6
    Join Date
    Jan 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Get QDate from microseconds

    Qt Code:
    1. void setTime_t(uint secsSince1Jan1970UTC);
    To copy to clipboard, switch view to plain text mode 
    But what if I have some dates before 1.Jan.1970 and some dates after 1970?

  7. #7
    Join Date
    Apr 2010
    Posts
    769
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    1
    Thanked 94 Times in 86 Posts

    Default Re: Get QDate from microseconds

    Quote Originally Posted by iddqd View Post
    Qt Code:
    1. void setTime_t(uint secsSince1Jan1970UTC);
    To copy to clipboard, switch view to plain text mode 
    But what if I have some dates before 1.Jan.1970 and some dates after 1970?
    Then your results will be system dependent. Assuming you're using the Unix epoch, dates prior to that point will be negative integers. Some systems accept these, other don't.

    If the system routines won't work, you can do the conversion yourself by using Julian dates. There are lots of references around that cover this.

  8. #8
    Join Date
    Jan 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Get QDate from microseconds

    Ok, thanks. I'll try to find a solution.

Similar Threads

  1. can't get Qdate
    By mmm286 in forum Newbie
    Replies: 1
    Last Post: 4th March 2010, 09:25
  2. QDate next day
    By AlexD in forum Qt Programming
    Replies: 2
    Last Post: 17th February 2010, 22:40
  3. Regarding QDate?
    By qtlinuxnewbie in forum Newbie
    Replies: 4
    Last Post: 5th February 2010, 07:02
  4. Getting QDate from string
    By yagabey in forum Qt Programming
    Replies: 10
    Last Post: 17th February 2009, 15:40
  5. QDate Help pls
    By munna in forum Qt Programming
    Replies: 6
    Last Post: 8th May 2006, 06:50

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
  •  
Qt is a trademark of The Qt Company.