[Interest] Question about setting QLocale

Murphy, Sean smurphy at walbro.com
Mon Nov 30 18:43:27 CET 2015


I'm having an issue with multiple locales in my application and I'm trying to understand what the best fix should be. Our application is being used in two places right now: China & the US. One of the functions of the application is to log data samples to a csv file. Another function is to be able to load that log file back in later to look at the data. 

As I'm writing out the samples, in addition to the other parameters,  I timestamp each one with the following line:
    logString += sample.sampleTime().toString("ddd MM/dd/yyyy HH:mm:ss.zzz"); // where sample.sampleTime() returns a QDateTime object.

This all works fine when a user logs a file and later reloads it on the same machine (or at least one set to the same locale). But if someone in China sends me a file they logged, my reader fails. The issue is the "ddd" in the QDateTime::toString() string format. According to the docs that does this:
    the abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses the system locale to localize the name, i.e. QLocale::system(). 
And since QDateTime::fromString(data, "ddd MM/dd/yyyy hh:mm:ss.zzz") also uses system locale to localize the name, it fails to convert because what's been written in the file for data recorded on a Friday is "ÖÜÎå", but when I'm reading it on my US localized machine, fromString() expects to see "Fri", and I end up with an invalid QDateTime object.

So I'm trying to figure out what the best solution is to fix this. Options I see:
1. Change the log file format to not include the abbreviated localized day name
2. Add code in both the writing and reading classes to set the locale to English, United States. Is there an easy way to localize (pun intended) the change of locale to just the QDateTime object? I don't really want to effect other parts of the application, only the logging capabilities. It looks like maybe QLocale::toDateTime() might help?
3. Since I know what countries I'm dealing with, I could try to read in the default locale first, and if that fails, then try to read in the other locale. The nice thing about this option is that it doesn't abandon log files that already exist out there in the world, but if we add more countries down the road, that means I'd have to keep updating this. 
4. Change my reader to first remove the abbreviated localized day name characters (regardless of locale) from the string, then do a QDateTime::fromString(data, "MM/dd/yyyy hh:mm:ss.zzz"). The localized day name is redundant information anyways and just helps to make the .csv file more readable if someone opens it in a text editor, but otherwise really doesn't add any value.

Am I missing any other options? Or is there a better way to deal with cross-locale data files?
Sean



More information about the Interest mailing list