KcFnMi
KcFnMi

Reputation: 6171

Qt QDateTime toString("h:m:s ap") ap/a/AP/a missing

I've noted the "ap/a/AP/a" is missing while converting a date to string. For "h:m:s ap", i.e., I get "11:5:42 ". The same happens for each of the "ap/a/AP/a" forms.

What I'm missing?

void DecoderBr1::recordOnFile(QDateTime dateTime, QByteArray ba)
{
    QString filename(dateTime.toString("yyyy MMMM [email protected] zzz ap"));
    filename.append(".log");

    Recorder recorder;
    recorder.recordFile(filename, ba);
}

Upvotes: 5

Views: 5644

Answers (2)

Miki
Miki

Reputation: 41775

It depends on your locale. Not every locale support AM/PM format. For example, my default locale is "it_IT" and does not print "AM/PM". Setting another locale (e.g. "en_EN") instead works as expected.

QDateTime t = QDateTime::fromString("2015-07-16T19:20:30+01:00", Qt::ISODate);
QString st = t.toString("yyyy MMMM [email protected] zzz ap");
QString locale_st_HH = QLocale("en_EN").toString(t, "yyyy MMMM [email protected] zzz ap");     
QString locale_st_hh = QLocale("en_EN").toString(t, "yyyy MMMM [email protected] zzz ap");

qDebug() << st; 
// With italian locale does not print AM/PM
// "2015 luglio [email protected] 000 "

qDebug() << locale_st_HH; 
// With en_EN locale it works
//"2015 July [email protected] 000 pm"

qDebug() << locale_st_hh; 
// With en_EN locale it works
// With hh it prints 07 pm instead of 19 pm // Credits to @t3ft3l--i
//"2015 July [email protected] 000 pm"

Upvotes: 6

t3ft3l--i
t3ft3l--i

Reputation: 1412

Not all locale support this format of QDateTime output.

For result you are need create variable QLocale with parameters locale(language, country), which support it. For example:

QLocale eng(QLocale::English, QLocale::UnitedStates);

Then you can use QLocale::toString() method with chosen locale like this:

qDebug() << eng.toString(datetime,  "yyyy MMMM [email protected] zzz ap");

Works for me at your example. And this way don't substitute your native locale.

Upvotes: 2

Related Questions