PDA

View Full Version : QDate an locale setting



Boron
9th October 2008, 19:14
Hello,
I need to display the application build date in an application.
So I'm using the following construction. From the date object I can later extract weekday, month an year easily.

QString dateString = __DATE__;
QDate date = QDate::fromString( dateString.simplified(), "MMM d yyyy" );
My problem is that the MS C++ compiler (Visual Studio 2005 and 2008) is giving __DATE__ in this form (today): Oct 9 2008. Notice the "c" in Oct, which is correct for the english "october".
As I have a german WinXP with german locale settings QDate expects the german shortform of "Oktober", which is "Okt", of course.

As result the fromString() method fails to successfully parse the dateString because it expects "Okt" and not "Oct" and date remains 0. Today's date is shown as "1. Januar -4713" in my application. That was shortly before the invention of the wheel :D.

I played around with QLocale class but I don't know how to tell fromString(), that it has to expect the english form "Oct".
Does anybody has an idea what to do?

yuriry
9th October 2008, 19:51
What if you use QDate::currentDate().toString() instead of __DATE__?

EDIT: sorry I think I missed the point - you need the date at compile time :(

caduel
9th October 2008, 19:54
Look at Qt::DefaultLocaleShortDate.

If you set your app's locale to english (for the duration of parsing that date), it should work.

Or take a look at http://www.boost.org/doc/libs/1_36_0/doc/html/date_time.html

Or you do a simple (and ugly) switch statement and parse the date by hand.

HTH

Boron
9th October 2008, 20:37
I can read the Qt-Docs as long as I want, but what shall I do with Qt:: DefaultLocaleShortDate?

Does nobody have a French or a Russian (or anything else) Windows and could test what date is created, using my code?

@yuriry
currentDate() is the wrong date, because it gives the date at runtime (tomorrow different than today). The __DATE__ macro is the date at compile time. It is a constant string.

maverick_pol
9th October 2008, 23:28
Hi,

1. What is the default lang for your system? What is the land you are using in the app?
2. Which system are you using?

Kacper

Boron
10th October 2008, 15:15
1. + 2. I have a German WinXP.
The settings concerning localization are set to "Germany", of course.

2. (2nd part) I have not intentionally set a land in my app. So locale settings are taken from what is set in Windows.

When I change to " English" in the control panel (Regional and Language Settings) Windows shows October with 'c' an a correct date is shown in my application.
When changing back to "German" (the month now is "Oktober") my application shows "1. Januar -4713" after rebuild.