[Qt-interest] QDateTime and force localization

Nick Panin nick.panin at gmail.com
Tue Nov 3 15:28:48 CET 2009


Hello All

I have a problem parsing datetime string on computer with locale different
than English.

It means that I can't parse datetime strings which are using english for
months, etc.. without changing locale on computer where it executes.
Not sure it matters, I'm using Qt 4.5.3 on winXp and mingw 5.1.6

Here is the sample:
#include <QtCore>
#include <QCoreApplication>

#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    QLocale::setDefault(QLocale::English);
    QLocale myLoc;

    cout << "Default application locale lang: " <<
myLoc.name().toStdString() << endl;
    cout << "System locale lang: " << QLocale::system().name().toStdString()
<< endl;

    QDateTime   dt = QDateTime::fromString("20-Oct-2009 08:30:10.220",
"dd-MMM-yyyy hh:mm:ss.zzz");

    if (dt.isValid())
    {
        cout << "Parsed datetime: " << dt.toString().toStdString() << endl;
    }
    else
    {
        cout << "Non valid date" << endl <<
            "Current datetime: " <<
QDateTime::currentDateTime().toString().toStdString() << endl;
    }

    return 0;
}

And here is the output:

Default application locale lang: en_US
System locale lang: ru_RU
Non valid date
Current datetime: ?? 3. ??? 17:23:47 2009


Thanks,
Nick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091103/4466a005/attachment.html 


More information about the Qt-interest-old mailing list