PDA

View Full Version : Timestamp to time



anh5kor
19th March 2015, 15:39
Hello,

I getting a timesatamp from an Hardware and have to convert it in format (sss.zzz) where s is second and z is millsecond.
I tried with the format(h:m:sss.zzz) I am getting the correct data but I don't want minute to display it should be second and millisecond and it should be in 3 digit.
If I have a code


QTime time,time1;
QString timeString = time.toString();

time.setHMS(0,0,0,0);
time1 = time.addMSecs(Time_ecu2uss[1]);//where Time_ecu2uss[i] contain the timestamp
timeString = time1.toString("m:ss.zzz");
qDebug()<<timeString ;

It display the time in 0:00:000 format but I want in 000:000 format

I tired to display like below


QTime time,time1;
QString timeString = time.toString();

time.setHMS(0,0,0,0);
time1 = time.addMSecs(Time_ecu2uss[1]);//where Time_ecu2uss[i] contain the timestamp
timeString = time1.toString("sss.zzz");
qDebug()<<timeString ;

I am getting the format but wrong time.

Lesiok
19th March 2015, 16:31
Something like this :
QTime time = QDateTime::fromTime_t(Time_ecu2uss[1]).time();
QString timeString = time.toString("ss.zzz");Format string does not know the component sss.

ChrisW67
20th March 2015, 21:34
If I read your question correctly then your input is an integer number of milliseconds since some time and you want to display that as seconds to three decimal places


QString result = QString::number(timestamp/1000.0, 6, 'f', 3);
// Or this
QString result = QString("%1.%2").arg(timestamp/1000, 2, 10' '0').arg(timestamp % 1000, 3, 10, '0');

anh5kor
23rd March 2015, 08:24
Hello ChrisW67

You are correct about the display. I want seconds to three decimal places.
but when I am using
1.QString result = QString::number(timestamp/1000.0, 6, 'f', 3);
I am getting the error
error: no matching function for call to 'QString::number(double, int, char, int)'
QString result = QString::number(Time_ecu2uss[i]/1000.0, 6, 'f', 3);

and
QString result = QString("%1.%2").arg(timestamp/1000, 2, 10' '0').arg(timestamp % 1000, 3, 10, '0');
Qcompiler is not taking



^

jefftee
23rd March 2015, 15:42
QString result = QString::number(timestamp/1000.0, 6, 'f', 3);

That should just be:


QString result = QString::number(timestamp/1000.0, 'f', 3);

ChrisW67
23rd March 2015, 21:49
The perils of working from memory on a painful virtual keyboard.

Blind copy and paste is not a good way to understand.
The QString::arg() version contains an obvious typo. Fix that and you will be closer.
You may also need to express the last argument to arg() as QLatin1String('0').

anh5kor
27th March 2015, 06:41
I have used


QString result = QString("%1.%2").arg(Time_ecu2uss[0]/1000, 2,10,'0').arg(Time_ecu2uss[0] % 1000, 3,10, '0');
qDebug()<<"result"<<result;

O/p
result "0.000000000000000000000000000000000000000000000000 .0.00000000000000000000000000000000000000000000000 0"
result "0.000000000000000000000000000000000000000000000000 .0.00000000000000000000000000000000000000000000000 0"
result "0.000000000000000000000000000000000000000000000000 .0.00000000000000000000000000000000000000000000000 0"
result "0.000000000000000000000000000000000000000000000000 .0.00000000000000000000000000000000000000000000000 0
When I Tried


QString result = QString("%1.%2").arg(Time_ecu2uss[0]/1000, 2,1,'0').arg(Time_ecu2uss[0] % 1000, 3,1, '0');
qDebug()<<"result"<<result;

o/p
result "0.000000000000000000000000000000000000000000000000 .0.00000000000000000000000000000000000000000000000 0"
result "0.000000000000000000000000000000000000000000000000 .0.00000000000000000000000000000000000000000000000 0"
result "0.000000000000000000000000000000000000000000000000 .0.00000000000000000000000000000000000000000000000 0"
result "0.000000000000000000000000000000000000000000000000 .0.00000000000000000000000000000000000000000000000 0"
result "0.000000000000000000000000000000000000000000000000 .0.00000000000000000000000000000000000000000000000 0"
And I tried remove the thired argument .


QString result = QString("%1.%2").arg(Time_ecu2uss[0]/1000, 2,'0').arg(Time_ecu2uss[0] % 1000, 3, '0');
qDebug()<<"result"<<result;

o/p
result " 0. 0"
result " 0. 0"
result " 0. 0"
result " 0. 0"
result " 0. 0"

But i am not getting the format as 000.000,I am very new to Qt .

jefftee
27th March 2015, 07:03
But i am not getting the format as 000.000,I am very new to Qt .
Are you looking for something like either of the methods below?



int timestamp = 25333; // 25333 milliseconds as an example (25.333 seconds)

QString result = QString::number(timestamp/1000.0, 'f', 3);
qDebug("result=%s", qUtf8Printable(result));

result = QString("%1").arg(timestamp/1000.0, 0, 'f', 3);
qDebug("result=%s", qUtf8Printable(result));

output:

result=25.333
result=25.333


Both result in the same output and my preference would be the first one that uses QString::number as I personally don't use QString::arg() often.

anh5kor
27th March 2015, 07:12
I should get like below.


result = 025.333 sec
result = 060.333 sec
result = 106.333 sec

jefftee
27th March 2015, 07:16
I should get like below.


result = 025.333 sec
result = 060.333 sec
result = 106.333 sec

Are you sure your seconds will never exceed 999? If you always want 3 digits before, a period, then 3 digits, either of the following works:



int timestamp = 25333; // 25333 milliseconds as an example (25.333 seconds)

QString pad = "0000000";
QString result = QString::number(timestamp/1000.0, 'f', 3);
result = pad.left(qMax(pad.length()-result.length(),0)) + result;
qDebug("result=%s", qUtf8Printable(result));

result = QString("%1").arg(timestamp/1000.0, 7, 'f', 3, '0');
qDebug("result=%s", qUtf8Printable(result));

result.sprintf("%07.3f", timestamp/1000.0);
qDebug("result=%s", qUtf8Printable(result));


Edit: I prefer the 3rd case this time (sprintf) given your requirement for padding with zeroes if needed...

anh5kor
27th March 2015, 07:42
It's working thanks' for the information

jefftee
27th March 2015, 07:53
No problem. Did you see my question about being sure that your number of seconds ever exceeds 999?

Are you parsing this output or doing anything else with it that would cause a problem if you exceeded 999,999 milliseconds?

anh5kor
27th March 2015, 10:16
No actually I have a fixed time format from
000.000esc
till
119.999sec
and again it will start from 000.000

jefftee
27th March 2015, 16:21
Ok, any of the three ways I posted should work for you then... Good luck!