PDA

View Full Version : Getting problems to convert from string to QDateTime



cesroc
30th March 2012, 13:58
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:



QString test = asctime(gmtime(&epoch));
QDateTime dt = QDateTime::fromString(test,"ddd MMM dd hh:mm:ss yyyy");
time_t testTime = dt.toTime_t();


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

Lykurg
30th March 2012, 14:38
Hi,

can't reproduce your problem.
uint epoc = 983748313;
QDateTime dt;
dt.setTime_t(epoc);

qWarning() << dt << dt.toTime_t();
qWarning() << (epoc == dt.toTime_t());behaves normal. So please post a minimal -runable- example, reproducing the problem.

cesroc
30th March 2012, 15:40
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:



void MainWindow::zoomInClicked()
{
if (yMinField->text()=="" || yMaxField->text()=="" || xMinField->text()=="" || xMaxField->text()=="" )
{
QMessageBox::critical(this, "", "Values to XY-min and XY-max must be setted.");
}
else
{
QDateTime dt = QDateTime::fromString(xMinField->text(),"ddd MMM dd hh:mm:ss yyyy");
time_t xMinEpoch = dt.toTime_t();

dt = QDateTime::fromString(xMaxField->text(),"ddd MMM dd hh:mm:ss yyyy");
time_t xMaxEpoch = dt.toTime_t();

plot[graphAreaSelection->currentIndex()]->setAxisScale(QwtPlot::xBottom, (xMinEpoch).toDouble(), (xMaxEpoch->text()).toDouble());
plot[graphAreaSelection->currentIndex()]->setAxisScale(channelSelection->currentIndex(), (yMinField->text()).toDouble() , (yMaxField->text()).toDouble() );
plot[graphAreaSelection->currentIndex()]->replot();
}
}


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?

Lykurg
30th March 2012, 16:42
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.

d_stranz
30th March 2012, 17:24
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?

cesroc
2nd April 2012, 08:56
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.

cesroc
2nd April 2012, 11:37
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