Results 1 to 14 of 14

Thread: Timestamp to time

  1. #1
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Default Timestamp to time

    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
    Qt Code:
    1. QTime time,time1;
    2. QString timeString = time.toString();
    3.  
    4. time.setHMS(0,0,0,0);
    5. time1 = time.addMSecs(Time_ecu2uss[1]);//where Time_ecu2uss[i] contain the timestamp
    6. timeString = time1.toString("m:ss.zzz");
    7. qDebug()<<timeString ;
    To copy to clipboard, switch view to plain text mode 
    It display the time in 0:00:000 format but I want in 000:000 format

    I tired to display like below
    Qt Code:
    1. QTime time,time1;
    2. QString timeString = time.toString();
    3.  
    4. time.setHMS(0,0,0,0);
    5. time1 = time.addMSecs(Time_ecu2uss[1]);//where Time_ecu2uss[i] contain the timestamp
    6. timeString = time1.toString("sss.zzz");
    7. qDebug()<<timeString ;
    To copy to clipboard, switch view to plain text mode 
    I am getting the format but wrong time.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Timestamp to time

    Something like this :
    Qt Code:
    1. QTime time = QDateTime::fromTime_t(Time_ecu2uss[1]).time();
    2. QString timeString = time.toString("ss.zzz");
    To copy to clipboard, switch view to plain text mode 
    Format string does not know the component sss.

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

    Default Re: Timestamp to time

    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
    Qt Code:
    1. QString result = QString::number(timestamp/1000.0, 6, 'f', 3);
    2. // Or this
    3. QString result = QString("%1.%2").arg(timestamp/1000, 2, 10' '0').arg(timestamp % 1000, 3, 10, '0');
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Timestamp to time

    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



    ^

  5. #5
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Timestamp to time

    Quote Originally Posted by anh5kor View Post
    QString result = QString::number(timestamp/1000.0, 6, 'f', 3);
    That should just be:
    Qt Code:
    1. QString result = QString::number(timestamp/1000.0, 'f', 3);
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Timestamp to time

    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').

  7. #7
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Timestamp to time

    I have used
    Qt Code:
    1. QString result = QString("%1.%2").arg(Time_ecu2uss[0]/1000, 2,10,'0').arg(Time_ecu2uss[0] % 1000, 3,10, '0');
    2. qDebug()<<"result"<<result;
    To copy to clipboard, switch view to plain text mode 
    O/p
    result "0.00000000000000000000000000000000000000000000000 0.0.0000000000000000000000000000000000000000000000 00"
    result "0.00000000000000000000000000000000000000000000000 0.0.0000000000000000000000000000000000000000000000 00"
    result "0.00000000000000000000000000000000000000000000000 0.0.0000000000000000000000000000000000000000000000 00"
    result "0.00000000000000000000000000000000000000000000000 0.0.0000000000000000000000000000000000000000000000 00
    When I Tried
    Qt Code:
    1. QString result = QString("%1.%2").arg(Time_ecu2uss[0]/1000, 2,1,'0').arg(Time_ecu2uss[0] % 1000, 3,1, '0');
    2. qDebug()<<"result"<<result;
    To copy to clipboard, switch view to plain text mode 
    o/p
    result "0.00000000000000000000000000000000000000000000000 0.0.0000000000000000000000000000000000000000000000 00"
    result "0.00000000000000000000000000000000000000000000000 0.0.0000000000000000000000000000000000000000000000 00"
    result "0.00000000000000000000000000000000000000000000000 0.0.0000000000000000000000000000000000000000000000 00"
    result "0.00000000000000000000000000000000000000000000000 0.0.0000000000000000000000000000000000000000000000 00"
    result "0.00000000000000000000000000000000000000000000000 0.0.0000000000000000000000000000000000000000000000 00"
    And I tried remove the thired argument .
    Qt Code:
    1. QString result = QString("%1.%2").arg(Time_ecu2uss[0]/1000, 2,'0').arg(Time_ecu2uss[0] % 1000, 3, '0');
    2. qDebug()<<"result"<<result;
    To copy to clipboard, switch view to plain text mode 
    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 .

  8. #8
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Timestamp to time

    Quote Originally Posted by anh5kor View Post
    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?

    Qt Code:
    1. int timestamp = 25333; // 25333 milliseconds as an example (25.333 seconds)
    2.  
    3. QString result = QString::number(timestamp/1000.0, 'f', 3);
    4. qDebug("result=%s", qUtf8Printable(result));
    5.  
    6. result = QString("%1").arg(timestamp/1000.0, 0, 'f', 3);
    7. qDebug("result=%s", qUtf8Printable(result));
    8.  
    9. output:
    10.  
    11. result=25.333
    12. result=25.333
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by jefftee; 27th March 2015 at 06:57.

  9. #9
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Timestamp to time

    I should get like below.
    Qt Code:
    1. result = 025.333 sec
    2. result = 060.333 sec
    3. result = 106.333 sec
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Timestamp to time

    Quote Originally Posted by anh5kor View Post
    I should get like below.
    Qt Code:
    1. result = 025.333 sec
    2. result = 060.333 sec
    3. result = 106.333 sec
    To copy to clipboard, switch view to plain text mode 
    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:

    Qt Code:
    1. int timestamp = 25333; // 25333 milliseconds as an example (25.333 seconds)
    2.  
    3. QString pad = "0000000";
    4. QString result = QString::number(timestamp/1000.0, 'f', 3);
    5. result = pad.left(qMax(pad.length()-result.length(),0)) + result;
    6. qDebug("result=%s", qUtf8Printable(result));
    7.  
    8. result = QString("%1").arg(timestamp/1000.0, 7, 'f', 3, '0');
    9. qDebug("result=%s", qUtf8Printable(result));
    10.  
    11. result.sprintf("%07.3f", timestamp/1000.0);
    12. qDebug("result=%s", qUtf8Printable(result));
    To copy to clipboard, switch view to plain text mode 
    Edit: I prefer the 3rd case this time (sprintf) given your requirement for padding with zeroes if needed...
    Last edited by jefftee; 27th March 2015 at 06:51.

  11. The following user says thank you to jefftee for this useful post:

    anh5kor (27th March 2015)

  12. #11
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Timestamp to time

    It's working thanks' for the information

  13. #12
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Timestamp to time

    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?

  14. #13
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Timestamp to time

    No actually I have a fixed time format from
    000.000esc
    till
    119.999sec
    and again it will start from 000.000

  15. #14
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Timestamp to time

    Ok, any of the three ways I posted should work for you then... Good luck!

Similar Threads

  1. Replies: 1
    Last Post: 28th April 2011, 13:16
  2. Replies: 5
    Last Post: 19th November 2010, 02:25
  3. Event timestamp
    By jupi32000 in forum Newbie
    Replies: 1
    Last Post: 14th July 2009, 09:27
  4. Convertng FILETIME timestamp to QDateTime and back again
    By grabnerj in forum Qt Programming
    Replies: 3
    Last Post: 7th July 2008, 22:27
  5. QDateTime from non-standard timestamp
    By pdoria in forum Qt Programming
    Replies: 2
    Last Post: 11th April 2008, 15:09

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.