Results 1 to 7 of 7

Thread: Getting problems to convert from string to QDateTime

  1. #1
    Join Date
    Feb 2012
    Location
    Stuttgart / Germany
    Posts
    35
    Thanks
    6
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Question Getting problems to convert from string to QDateTime

    Hello,

    I have a string with my date and time and I need to convert this string to time_t.
    My string format is: Sun Mar 04 23:25:13 2001
    The epoch (time_t) for this string is: 983748313

    The code I wrote is:

    Qt Code:
    1. QString test = asctime(gmtime(&epoch));
    2. QDateTime dt = QDateTime::fromString(test,"ddd MMM dd hh:mm:ss yyyy");
    3. time_t testTime = dt.toTime_t();
    To copy to clipboard, switch view to plain text mode 

    I have the epoch/time_t (983748313), I'm using this code to check if I can convert to string (Sun Mar 04 23:25:13 2001) and then convert back to epoch, but when I print my convertion (testTime) gets: 4294967295.
    If I check my variables, epoch is ok, QString test is ok, but dt data there is nothing inside (0,0,0,-1). I don't know if I need to do something else and I have no idea why it's not converting.

    I would like to have the output:
    epoch: 983748313
    test: Sun Mar 04 23:25:13 2001
    testTime: 983748313

    Epoch and test are ok, but I still have problems to convert and have a testTime.

    Can anyone help me with that?

    Thanks in advance
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Getting problems to convert from string to QDateTime

    Hi,

    can't reproduce your problem.
    Qt Code:
    1. uint epoc = 983748313;
    2. dt.setTime_t(epoc);
    3.  
    4. qWarning() << dt << dt.toTime_t();
    5. qWarning() << (epoc == dt.toTime_t());
    To copy to clipboard, switch view to plain text mode 
    behaves normal. So please post a minimal -runable- example, reproducing the problem.

  3. #3
    Join Date
    Feb 2012
    Location
    Stuttgart / Germany
    Posts
    35
    Thanks
    6
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Getting problems to convert from string to QDateTime

    Hello,

    It was a misunderstood.
    Let me explain better.

    The epoc I have now it was just a test. The real situation is:

    The user is gonna write a date in a textfield: Sun Mar 04 21:55:29 2001.
    After the user writes he's gonna press a button.
    When this button is pressed the software gets the string (textfield): Sun Mar 04 21:55:29 2001, and converts this string to an epoc.

    The function that gets the user click is:

    Qt Code:
    1. void MainWindow::zoomInClicked()
    2. {
    3. if (yMinField->text()=="" || yMaxField->text()=="" || xMinField->text()=="" || xMaxField->text()=="" )
    4. {
    5. QMessageBox::critical(this, "", "Values to XY-min and XY-max must be setted.");
    6. }
    7. else
    8. {
    9. QDateTime dt = QDateTime::fromString(xMinField->text(),"ddd MMM dd hh:mm:ss yyyy");
    10. time_t xMinEpoch = dt.toTime_t();
    11.  
    12. dt = QDateTime::fromString(xMaxField->text(),"ddd MMM dd hh:mm:ss yyyy");
    13. time_t xMaxEpoch = dt.toTime_t();
    14.  
    15. plot[graphAreaSelection->currentIndex()]->setAxisScale(QwtPlot::xBottom, (xMinEpoch).toDouble(), (xMaxEpoch->text()).toDouble());
    16. plot[graphAreaSelection->currentIndex()]->setAxisScale(channelSelection->currentIndex(), (yMinField->text()).toDouble() , (yMaxField->text()).toDouble() );
    17. plot[graphAreaSelection->currentIndex()]->replot();
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    This is the code, the plot part I'm not sure if it's working because of the convertion to double, and I was not using, I put a break point before to check the time_t value. The problem is: the dt is always 0,0,0,-1, then the time_t is wrong.

    Thanks for the reply
    Any ideas?

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Getting problems to convert from string to QDateTime

    Ah, I see it now! fromString is local aware. So e.g. my environment is german, thus 'dec' for MMM will fail because December is Dezember in german and abbreviated with 'dez' instead with 'dec'. This is the reason why the convert fails. (Edit: Ehm, just saw you are also from Germany, so you know how Dezember is written...)

    From the docs (what I have also not read carefully enough)
    Note: Unlike the other version of this function, day and month names must be given in the user's local language. It is only possible to use the English names if the user's language is English.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,240
    Thanks
    303
    Thanked 865 Times in 852 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Getting problems to convert from string to QDateTime

    The user is gonna write a date in a textfield: Sun Mar 04 21:55:29 2001.
    After the user writes he's gonna press a button.
    When this button is pressed the software gets the string (textfield): Sun Mar 04 21:55:29 2001, and converts this string to an epoc.
    You are actually going to force your users to understand this date format and write it exactly correctly in a text field in order to get a date and time into your application? Sure hope your users are good with abbreviations and punctuation. Does it really matter that the time is correct to the second?

    And what happens if they enter something wrong? The only feedback you have now after conversion is that the QDateTime is empty. How are you going to be able to tell them what they are doing wrong?
    Last edited by d_stranz; 30th March 2012 at 17:30.

  6. #6
    Join Date
    Feb 2012
    Location
    Stuttgart / Germany
    Posts
    35
    Thanks
    6
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Getting problems to convert from string to QDateTime

    Hello,

    Thanks for your reply. I don't think it's because of the language. When I write in german I get the same error, no convertion. To test I'm using a constant value, I tried in english and german and nothing happens.

    If you have any other idea, please tell me. haha
    Thanks.

    Hello d_stranz,

    I know that maybe it's not a nice solution, but actually this is one of the requirements, I cannot change. I'm setting a first value as an example to show the user and I used an input mask to limitate the user input. But I have to do like that.

  7. #7
    Join Date
    Feb 2012
    Location
    Stuttgart / Germany
    Posts
    35
    Thanks
    6
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Getting problems to convert from string to QDateTime

    Hello d_stranz and Lykurg,

    Thanks for the help. I found the problem. My bad... there was a false space in my input mask... that's why it was not working...

    thanks again

Similar Threads

  1. How to convert string into const int ?
    By babygal in forum Qt Programming
    Replies: 3
    Last Post: 16th July 2010, 10:57
  2. convert decimal seconds to QDateTime and back
    By mcarter in forum Qt Programming
    Replies: 2
    Last Post: 18th March 2010, 12:06
  3. convert a string to hex
    By priceey in forum Qt Programming
    Replies: 7
    Last Post: 12th February 2009, 10:38
  4. How to convert unix time to QDateTime
    By lni in forum Qt Programming
    Replies: 3
    Last Post: 14th November 2007, 00:25
  5. Replies: 1
    Last Post: 30th June 2006, 05:24

Tags for this Thread

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.