Leading zeroes for time with QString
-
The goal is to format time to display in a QLineEdit and using QString. Hours, minutes, and seconds look much better when 2 hours 3 minutes is displayed as 02:03. Here is my best shot so far:
Time_hms = QString( “%1 %2:%3:%4” ) .arg( days,3) .arg( hours:2 ) .arg( minutes:2 ) .arg( seconds );So far QString does not honor the field width. What needs to be changed?
-
The goal is to format time to display in a QLineEdit and using QString. Hours, minutes, and seconds look much better when 2 hours 3 minutes is displayed as 02:03. Here is my best shot so far:
Time_hms = QString( “%1 %2:%3:%4” ) .arg( days,3) .arg( hours:2 ) .arg( minutes:2 ) .arg( seconds );So far QString does not honor the field width. What needs to be changed?
You should try this overload.
Time_hms = QString( “%1 %2:%3:%4” ) .arg( days,3) .arg( hours, 2, 10, QChar('0')) .arg( minutes, 2, 10, QChar('0')) .arg( seconds, 2, 10, QChar('0'));If your date is in a
QDate, you can use the toString() function, which allows easy formatting too.Regards
-
Hi
auto days = 33; auto hours = 2; auto minutes = 2; auto seconds = 14; auto Time_hms = QString( "%1 %2:%3:%4" ).arg( days, 3, 10, QChar('0')) .arg( hours, 2, 10, QChar('0') ) .arg( minutes, 2, 10, QChar('0') ) .arg( seconds,2,10, QChar('0') ); qDebug() << Time_hms;output
033 02:02:14Hehe. double post in exact moment :)
-
Hi
auto days = 33; auto hours = 2; auto minutes = 2; auto seconds = 14; auto Time_hms = QString( "%1 %2:%3:%4" ).arg( days, 3, 10, QChar('0')) .arg( hours, 2, 10, QChar('0') ) .arg( minutes, 2, 10, QChar('0') ) .arg( seconds,2,10, QChar('0') ); qDebug() << Time_hms;output
033 02:02:14Hehe. double post in exact moment :)
@mrjj Yeah, double post right on the money.
We are stuck with Qt version 3 and I suspect that matters. Simplifying to a single field, this almost works
*time_hms = QString( “x$1y” ) .arg( hours, 2, 10 );Producing: x 6y There is a space rather than the character zero.
Add in a bit more*time_hms = QString( “x$1y” ) .arg( hours, 2, 10, QChar(‘0’) );To produce: x6.0000
Actually there are about 48 zeroes after the 6. The decimal point was not present in the first shortened example.
Edit: So I got tired of muckin around and wrote this concept four timesif( hours < 10 ) qhours = QString( “0%1:” ) .arg( hours ); else qhours = QString( “%1:” ) .arg( hours );Then combined the strings to make the final. That’s ugly but now I get to move on.
If there is a better solution I will use it.
Thanks the taking the time to reply. -
Hi
So in Qt3 (wow!) the
..2, 10, QChar(‘0’) ); part doesn't really do the same?
What types are hours, minutes etc ? -
@mrjj Yeah, double post right on the money.
We are stuck with Qt version 3 and I suspect that matters. Simplifying to a single field, this almost works
*time_hms = QString( “x$1y” ) .arg( hours, 2, 10 );Producing: x 6y There is a space rather than the character zero.
Add in a bit more*time_hms = QString( “x$1y” ) .arg( hours, 2, 10, QChar(‘0’) );To produce: x6.0000
Actually there are about 48 zeroes after the 6. The decimal point was not present in the first shortened example.
Edit: So I got tired of muckin around and wrote this concept four timesif( hours < 10 ) qhours = QString( “0%1:” ) .arg( hours ); else qhours = QString( “%1:” ) .arg( hours );Then combined the strings to make the final. That’s ugly but now I get to move on.
If there is a better solution I will use it.
Thanks the taking the time to reply.@BKBK
Qt 3 should have QDateTime right ?does this work:
int year(2018),months(1),days(29),hours(8),minutes(22),seconds(0); QDateTime dateTime(QDate(year,months,days), QTime(hours,minutes,seconds)); QString dateTimeString = dateTime.toString(QString("ddd hh:mm:ss")); qDebug () << dateTimeString; -
@BKBK
Qt 3 should have QDateTime right ?does this work:
int year(2018),months(1),days(29),hours(8),minutes(22),seconds(0); QDateTime dateTime(QDate(year,months,days), QTime(hours,minutes,seconds)); QString dateTimeString = dateTime.toString(QString("ddd hh:mm:ss")); qDebug () << dateTimeString;@J.Hilk
In the example code I provided hours, minutes, etc, are all integers. Time in nanoseconds is fetched and the values extracted.
This version does not have QtDateTime but does have QTime. I noticed that the underlying format is not explicitly stated.
This is a strip chart function and most, probably all, the parameters to be charted arrive with a time stamp as a double. That time may be in millisecond, micros, or nanos so I wrote the basic code to work with any. Simple switch statement. Formatting was easy once each part was handled separately. Just a bit ugly 'cause there are four sets of if statements. Then just concat them:
qtime = qdays + qhours + qmin + qseconds
I am going to leave that code in place for now. This time I'll remember to mark the thread as resolved.
Thanks again for your time.