[Interest] Question about setting QLocale

Murphy, Sean smurphy at walbro.com
Tue Dec 1 22:20:43 CET 2015


> And then you have similar problems with QString::number, QDomDocument or
> any of the others that I have seen with this kind of problem.

I think I'm Ok here, as Thiago already pointed out, the locale issue has already been fixed for QString, and indeed the documentation for QString::number() (http://doc.qt.io/qt-5/qstring.html#number) says "The formatting always uses QLocale::C, i.e., English/UnitedStates. To get a localized string representation of a number, use QLocale::toString() with the appropriate locale." I will confess that I lucked out here, because as this thread clearly shows, I didn't appreciate any of these nuances when I wrote the reader and writer functionality.

If this wasn't the case, my .csv file would have most likely failed earlier on because a value of 1.5 would have gotten written out as 1,5 and since this is a comma separated variable file that would have thrown me off because the number of tokens (I do a QString::split(',')) on a line of data would have been off from the expected count, which is something I check prior to worrying about whether I can convert the timestamp token to a QDateTime.

> Sure, you might test yourself all the way out of this to a working solution. And
> then in three years someone else uses QDate::toString the wrong way and you
> release the new version. And then you are ****ed again.
> 
> If you write something in a file that you later parse, you absolutely must set a
> known locale before the writing is done. Whether this is C (which is probably
> best) or US or anything else is irrelevant. As long as you do it.
> 
> Next step I'd do is to unit test this with something like
> 
> 1) Set some weird locale
> 2) Call the write function
> 3) Set another weird locale
> 4) Call the read function
> 
> And repeat with a bunch of locale combinations. I'd also throw in a set of
> known good and bad files to test with.
> 
> With this, you will be fairly sure that your application won't ever bitrot in this
> area. Without it, there's pain waiting for you.

I think as soon as I change my writer/reader use dates formatted via Qt::RFC2822Date, then my log file becomes locale agnostic. I'll still be happy to run the tests you're suggesting, but changing the locale should no longer be a factor.

Sean



More information about the Interest mailing list