Results 1 to 7 of 7

Thread: QDateTimeEdit as a time in unix?

  1. #1
    Join Date
    Oct 2010
    Posts
    58
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default QDateTimeEdit as a time in unix?

    Hi,

    I want to have a user enter a QDateTimeEdit time and then take that info and convert it to unix time that I can store in a double so that I can load up some data. Any Idea how this can be done?

    I was thinking I could get the info then convert it to a char * and then maybe convert it to unix? But that is probably totally wrong:
    Qt Code:
    1. QDateTimeEdit * startTime;
    2. startTime = new QDateTimeEdit;
    3.  
    4. //slot activated when user clicks a pushbutton)
    5. void myWidget::getStartTime
    6. {
    7. QString stEntered =startTime->text();
    8. char * Start = qstrdup( stEntered.toLatin1() );
    9.  
    10. /// then convert to unix...maybe
    11. }
    To copy to clipboard, switch view to plain text mode 

    Is there a different/better way to convert some QDateTimeEdit to a double that will store the time in seconds since Epoch?

    Thanks for the help.

  2. #2
    Join Date
    Oct 2010
    Posts
    58
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDateTimeEdit as a time in unix?

    OK so I am getting closer but I think I am making it harder than it is.

    I'm using secsTo() to get the difference between the beginning of unix time Utc and my start and end times. This works ok, but secsTo automatically converts my start and end times to utc which messes up my code because they are already in utc time (which I want). To get around this I add the difference between my time and Utc time to epoch.setTime. There has got to be a better way....


    Qt Code:
    1. void MyWidget::getDates(){
    2.  
    3. double diff = 60*60*7; // sec*min*hours - offset for local time
    4.  
    5. QDateTime stEntered = startTime->dateTime();
    6. QDateTime epoch;
    7. epoch.setTime_t(diff);
    8. time_t st = epoch.secsTo(stEntered);//secsTo converts both epoch and stEntered
    9. //to utc time then finds the diff between them, so problem is that my stEntered is
    10. //already in utc (result ends up being 7 hrs ahead of utc)
    11.  
    12. stT = double(st);
    13.  
    14. QDateTime endEntered = EnterEnd->dateTime();
    15. time_t et = epoch.secsTo(endEntered);
    16. endT = double(et);
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

    thanks for the help

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

    Default Re: QDateTimeEdit as a time in unix?

    Is there something wrong with QDateTime::toTime_t()?

  4. The following user says thank you to SixDegrees for this useful post:

    kja (20th November 2010)

  5. #4
    Join Date
    Oct 2010
    Posts
    58
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDateTimeEdit as a time in unix?

    When I use QDateTime::toTime_t() it seems to take my data and convert it to utc time again, which gives me a wrong value because the time was originally in utc

    this is how i used it:

    Qt Code:
    1. .........
    2. EnterSt = new QDateTimeEdit( QDateTime::currentDateTimeUtc().addDays(-10));
    3. EnterEnd = new QDateTimeEdit( QDateTime::currentDateTimeUtc(), this);
    4. .........
    5.  
    6. void MyWidget::getDates(){
    7.  
    8. QDateTime stEntered =EnterSt->dateTime();
    9. time_t st = stEntered.toTime_t();
    10. stT = double(st);
    11.  
    12. QDateTime endEntered = EnterEnd->dateTime();
    13. time_t et = endEntered.toTime_t();
    14. endT = double(et);
    To copy to clipboard, switch view to plain text mode 

    stT and entT are now 7 hours later than EnterSt and EnterEnd (7hrs is my difference from UTC time)

    ????

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDateTimeEdit as a time in unix?

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #6
    Join Date
    Oct 2010
    Posts
    58
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDateTimeEdit as a time in unix?

    I think toTimeSpec() and toLocalTime() will convert my info. All I want to do it turn a QDateTime into a time_T with no changes made to the data. It starts out in UTC and I want it to stay in UTC just as a time_t (actually I want it to be a double but I can easily convert the time_t to a double)

    this is what I am doing now:

    Qt Code:
    1. time_t rawtime2;
    2. time(&rawtime2);
    3. double diff = (double)(mktime(gmtime(&rawtime2)) - mktime(localtime((&rawtime2))));//offset for local time
    4.  
    5. QDateTime stEntered = EnterSt->dateTime();
    6. QDateTime endEntered = EnterEnd->dateTime();
    7. time_t st = stEntered.toTime_t()-diff;
    8. time_t et = endEntered.toTime_t()-diff;
    9. stT = double(st);
    10. endT = double(et);
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDateTimeEdit as a time in unix?

    And how should Qt know it is in UTC if you don't tell it that? Tell Qt the time is in UTC and all will be fine.

    Why don't you use Qt's methods for querying time? You wouldn't have all those problems at all.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 5
    Last Post: 19th November 2010, 02:25
  2. QDateTimeEdit
    By doberkofler in forum Qt Programming
    Replies: 8
    Last Post: 2nd November 2009, 15:10
  3. QDateTimeEdit
    By coderbob in forum Qt Programming
    Replies: 3
    Last Post: 5th January 2008, 19:54
  4. How to convert unix time to QDateTime
    By lni in forum Qt Programming
    Replies: 3
    Last Post: 14th November 2007, 00:25
  5. QDateTimeEdit
    By dkite in forum Qt Programming
    Replies: 2
    Last Post: 10th September 2006, 19:12

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.