Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLocale::toString() bug
Forum Updated to NodeBB v4.3 + New Features

QLocale::toString() bug

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 882 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AndyBriceA Offline
    AndyBriceA Offline
    AndyBrice
    wrote on last edited by
    #1

    I'm not sure if this is a bug in Qt or if I am doing something stupid.

    This code:

    QLocale locale1( QLocale::German, QLocale::Germany );
     for ( int m = 1; m <= 12; m++ )
     {
         QDate date1( 2025, m, 1 );
         qDebug() << "locale1=" << locale1 << " date1=" << date1 << "locale1.toString( date1, \"MMMM\" )=" << locale1.toString( date1, "MMMM" );
     }
    

    Gives the expected output:

    locale1= QLocale(German, Latin, Germany)  date1= QDate("2025-01-01") locale1.toString( date1, "MMMM" )= "Januar"
    locale1= QLocale(German, Latin, Germany)  date1= QDate("2025-02-01") locale1.toString( date1, "MMMM" )= "Februar"
    locale1= QLocale(German, Latin, Germany)  date1= QDate("2025-03-01") locale1.toString( date1, "MMMM" )= "März"
    locale1= QLocale(German, Latin, Germany)  date1= QDate("2025-04-01") locale1.toString( date1, "MMMM" )= "April"
    ...
    

    But this code:

    foreach ( const QLocale& locale2, QLocale::matchingLocales( QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry ) )
     {
         if ( locale2.country() == QLocale::Germany && locale2.language() == QLocale::German )
         {
            for ( int m = 1; m <= 12; m++ )
            {
                QDate date2( 2025, m, 1 );
                qDebug() << "locale2=" << locale2 << " date2=" << date2 << "locale2.toString( date2, \"MMMM\" )=" << locale2.toString( date2, "MMMM" );
            }
     }
    

    Gives different output:

    locale2= QLocale(German, Latin, Germany)  date2= QDate("2025-01-01") locale2.toString( date2, "MMMM" )= "January"
    locale2= QLocale(German, Latin, Germany)  date2= QDate("2025-02-01") locale2.toString( date2, "MMMM" )= "February"
    locale2= QLocale(German, Latin, Germany)  date2= QDate("2025-03-01") locale2.toString( date2, "MMMM" )= "March"
    locale2= QLocale(German, Latin, Germany)  date2= QDate("2025-04-01") locale2.toString( date2, "MMMM" )= "April"
    ...
    

    I am completely baffled why the month output isn't the same. I've looked at it for over hour! Any ideas?

    1 Reply Last reply
    0
    • AndyBriceA Offline
      AndyBriceA Offline
      AndyBrice
      wrote on last edited by
      #2

      I am using Qt 5.15.18 with MSVC2019 on Windows 11.

      1 Reply Last reply
      0
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Works fine for me with a recent Qt version:

        int main(int argc, char* argv[])
        {
            QApplication a(argc, argv);
        
            QLocale locale1(QLocale::German, QLocale::Germany);
            for (int m = 1; m <= 12; m++) {
                QDate date1(2025, m, 1);
                qDebug() << "locale1=" << locale1 << " date1=" << date1 << "locale1.toString( date1, \"MMMM\" )=" << locale1.toString(date1, "MMMM");
            }
            const auto locales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry);
            for (const QLocale& locale2 : locales) {
                if (locale2.country() == QLocale::Germany && locale2.language() == QLocale::German) {
                    for (int m = 1; m <= 12; m++) {
                        QDate date2(2025, m, 1);
                        qDebug() << "locale2=" << locale2 << " date2=" << date2 << "locale2.toString( date2, \"MMMM\" )=" << locale2.toString(date2, "MMMM");
                    }
                }
            }
            return 0;
        }
        

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • AndyBriceA Offline
          AndyBriceA Offline
          AndyBrice
          wrote on last edited by AndyBrice
          #4

          So it returns the German version of the month in all cases?

          1 Reply Last reply
          0
          • AndyBriceA Offline
            AndyBriceA Offline
            AndyBrice
            wrote on last edited by
            #5

            Stranger still, if I change "MMMM" to "dddd-MMMM" it returns the German day name, but not the German month name, inside the loop:

            locale1= QLocale(German, Latin, Germany)  date1= QDate("2025-01-01") locale1.toString( date1, "dddd-MMMM" )= "Mittwoch-Januar"
            locale1= QLocale(German, Latin, Germany)  date1= QDate("2025-02-01") locale1.toString( date1, "dddd-MMMM" )= "Samstag-Februar"
            locale1= QLocale(German, Latin, Germany)  date1= QDate("2025-03-01") locale1.toString( date1, "dddd-MMMM" )= "Samstag-März"
            locale1= QLocale(German, Latin, Germany)  date1= QDate("2025-04-01") locale1.toString( date1, "dddd-MMMM" )= "Dienstag-April"
            ...
            
            locale2= QLocale(German, Latin, Germany)  date2= QDate("2025-01-01") locale2.toString( date2, "dddd-MMMM" )= "Mittwoch-January"
            locale2= QLocale(German, Latin, Germany)  date2= QDate("2025-02-01") locale2.toString( date2, "dddd-MMMM" )= "Samstag-February"
            locale2= QLocale(German, Latin, Germany)  date2= QDate("2025-03-01") locale2.toString( date2, "dddd-MMMM" )= "Samstag-March"
            locale2= QLocale(German, Latin, Germany)  date2= QDate("2025-04-01") locale2.toString( date2, "dddd-MMMM" )= "Dienstag-April"
            ...
            
            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Update to a recent version.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • AndyBriceA Offline
                AndyBriceA Offline
                AndyBrice
                wrote on last edited by
                #7

                I don't understand how the same bit of code can be returning different answers inside and outside a loop. It doesn't make any sense.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

                  Just for the sake of research, what do you get if you compare the two QLocale objects for equality ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Please provide a minimal, compileable example which does not work with a recent Qt version. I provided one which does work fine.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    0
                    • AndyBriceA Offline
                      AndyBriceA Offline
                      AndyBrice
                      wrote on last edited by
                      #10

                      Code:

                      #include <QCoreApplication>
                      #include <QLocale>
                      #include <QDate>
                      #include <QtDebug>
                      
                      int main(int argc, char* argv[])
                      {
                          QCoreApplication a(argc, argv);
                      
                          QLocale locale1(QLocale::German, QLocale::Germany);
                          for (int m = 1; m <= 3; m++)
                          {
                              QDate date1(2025, m, 1);
                              qDebug() << "locale1=" << locale1 << " date1=" << date1 << "locale1.toString( date1, \"MMMM\" )=" << locale1.toString(date1, "MMMM");
                          }
                          const auto locales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry);
                          for (const QLocale& locale2 : locales)
                          {
                              if (locale2.country() == QLocale::Germany && locale2.language() == QLocale::German)
                              {
                                  qDebug() << "locale1 == locale2 =" << ( locale1 == locale2 );
                                  for (int m = 1; m <= 3; m++)
                                  {
                                      QDate date2(2025, m, 1);
                                      qDebug() << "locale2=" << locale2 << " date2=" << date2 << "locale2.toString( date2, \"MMMM\" )=" << locale2.toString(date2, "MMMM");
                                  }
                              }
                          }
                          return 0;
                      }
                      

                      Results:

                      Qt 5.15.18/MSVC2019/Windows:
                      Qt 5.15.18/XCode/macOS:

                      locale1= QLocale(German, Latin, Germany)  date1= QDate("2025-01-01") locale1.toString( date1, "MMMM" )= "Januar"
                      locale1= QLocale(German, Latin, Germany)  date1= QDate("2025-02-01") locale1.toString( date1, "MMMM" )= "Februar"
                      locale1= QLocale(German, Latin, Germany)  date1= QDate("2025-03-01") locale1.toString( date1, "MMMM" )= "März"
                      locale1 == locale2 = true
                      locale2= QLocale(German, Latin, Germany)  date2= QDate("2025-01-01") locale2.toString( date2, "MMMM" )= "January"
                      locale2= QLocale(German, Latin, Germany)  date2= QDate("2025-02-01") locale2.toString( date2, "MMMM" )= "February"
                      locale2= QLocale(German, Latin, Germany)  date2= QDate("2025-03-01") locale2.toString( date2, "MMMM" )= "March"
                      

                      ^^^Month is incorrect for locale2^^^

                      Qt 6.7.3/MSVC2019/Windows:
                      Qt 6.7.3/XCode/macOS:

                      locale1= QLocale(German, Latin, Germany)  date1= QDate("2025-01-01") locale1.toString( date1, "MMMM" )= "Januar"
                      locale1= QLocale(German, Latin, Germany)  date1= QDate("2025-02-01") locale1.toString( date1, "MMMM" )= "Februar"
                      locale1= QLocale(German, Latin, Germany)  date1= QDate("2025-03-01") locale1.toString( date1, "MMMM" )= "März"
                      locale1 == locale2 = true
                      locale2= QLocale(German, Latin, Germany)  date2= QDate("2025-01-01") locale2.toString( date2, "MMMM" )= "Januar"
                      locale2= QLocale(German, Latin, Germany)  date2= QDate("2025-02-01") locale2.toString( date2, "MMMM" )= "Februar"
                      locale2= QLocale(German, Latin, Germany)  date2= QDate("2025-03-01") locale2.toString( date2, "MMMM" )= "März"
                      

                      ^^^ Month is as expected^^^

                      So the localised month is wrong (but only inside the loop) for Qt 5.15.18.

                      Weirdly, day of week ("ddd" or "dddd") is correct in both cases.

                      I can work around it, but it is a very odd bug.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved