Search code examples
c++qtdatedatetimedate-conversion

Convert QString date (RFC 822 format) to another QString format based on culture


I've a database with multiple strings, I get this records from query, and i receive this format data in QString:

"Mon, 13 Nov 2017 09:48:45 +0000"

So, I need to convert this based on culture, for example if I want convert in italian culture?

So the result will be:

"Lun, 13 Nov 2017 09:48:45"

Exist in qt this type of conversion or I have to proceed manually ?

Thanks to all


Solution

  • Qt provides the QLocale class to handle language/country specifics. It has overloads of QLocale::toString() that accept QDateTime.

    It also has a function QLocale::toDateTime() to do the reverse operation. But in your case the non local aware QDateTime::fromString() should work.

    Int the end you should have something like this (+ or - some parameters for the exact format you want).

    QString source = "Mon, 13 Nov 2017 09:48:45 +0000";
    QDateTime dt  = QDateTime::fromString(source, Qt::RFC2822Date);
    QString result = QLocale().toString(dt, Qt::RFC2822Date);
    

    Note that QLocale() constructs an instance based on the current user setting in the underlying OS. You can also force a specific language/country, for instance: QLocale(QLocale::Italian).