Important: Please read the Qt Code of Conduct -

QTime formatting HH:MM:SS.s

  • Hi All,
    I am looking for a way to format QTime with 1 decimal second

    auto time = QDateTime::currentDateTimeUtc();
    QString sTime = time.toString("hh:mm:ss.zzz");

    this works fine, time is formatted nicely. I however assumed that hh:mm:ss.z and hh:mm:ss.zz was working too. But this give me an rather funny
    outcome with wrong milliseconds. When reading through the documentation I see that .zzz is supported but probably not .z and .zz. I recently upgraded from 5.3 to 5.8 and that may be related, not sure. So my question is, is there any way to format the time with one deicmal second and two decimal seconds?

    thanks, rich

  • Hi! Actually zzz and z are supported, with the latter producing the milliseconds without leading zeroes (0 to 999). Obviously not what you need or expected. To answer you question: no, unfortunately, there is no built-in support for the format you requested. You need to come up with your own little function for that.

  • @Wieland thanks for your answer. Yup, I read that too that .z is msec without leading 0. That is a bit of a silly one IMO.

  • Well, yes, maybe it's a bit silly, but at least it's consistent with the other formating options. But I agree, it would be nice to have more options, like the "variants" of z and zz you need now.

  • That certainly seems to be a foolish consistency! The output of "ss.z" might read "10.5" which you'd reasonably expect to mean "ten and one half seconds" but in fact means "ten and five thousandths of a second". I'd be interested to know in what context that format is ever used.

  • @Chris-Hennes E.g. someone might only need the milliseconds, like in time.toString("z"). But I don't want to defend the available set of formatting options too much ;)

  • @Wieland Good point -- it seems the real problem is not the consistency of Qt, but the inconsistency of humanity's time system. I think there is room in the world for both a standalone "z" and a formatting that shows tenths, hundredths, or thousandths of a second.

Log in to reply