Results 1 to 6 of 6

Thread: QDate an locale setting

  1. #1
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QDate an locale setting

    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.
    Qt Code:
    1. QString dateString = __DATE__;
    2. QDate date = QDate::fromString( dateString.simplified(), "MMM d yyyy" );
    To copy to clipboard, switch view to plain text mode 
    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 .

    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?

  2. #2
    Join Date
    Sep 2008
    Posts
    60
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDate an locale setting

    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
    Last edited by yuriry; 9th October 2008 at 19:58.

  3. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDate an locale setting

    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...date_time.html

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

    HTH

  4. #4
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDate an locale setting

    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.

  5. #5
    Join Date
    May 2007
    Location
    Lublin, Poland
    Posts
    345
    Thanks
    40
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDate an locale setting

    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
    Qt allows you to use everything you want
    wysota
    --------------------------------------------------------------------------------
    #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
    abort(); // trap; generates core dump
    #else
    exit(1); // goodbye cruel world
    #endif

  6. #6
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDate an locale setting

    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.

Similar Threads

  1. Setting locale for the whole app
    By maverick_pol in forum Qt Programming
    Replies: 2
    Last Post: 22nd June 2008, 22:53
  2. Problems with QDate
    By cyberboy in forum Qt Programming
    Replies: 4
    Last Post: 25th May 2008, 21:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.