Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved QLocale doesn't match system locale

    General and Desktop
    qlocale locale qdatetime
    2
    4
    190
    Loading More Posts
    • 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.
    • T
      TheEnigmist last edited by

      I'm using Qt 6.2.1 on Windows 11 with a system locale different from en_US but whatever project I create (Window, Console) if I use a simple code like:

      qInfo().nospace() << QDateTime::currentDateTime().toString("dd MMMM yyyy hh:mm:ss.zzz");
      

      It prints the date in english locale

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        What is your locale ?
        What do you get ?
        What do you expect ?

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

        T 1 Reply Last reply Reply Quote 0
        • T
          TheEnigmist @SGaist last edited by

          @SGaist Hi,
          My system locale is it_IT
          a64f1181-a368-4d53-8306-146ba383e420-image.png
          As you can see currentDateTime().toString() gives me string in en_US
          I expect to get that string in my system locale

          1 Reply Last reply Reply Quote 0
          • T
            TheEnigmist last edited by TheEnigmist

            I found the error. From docs here: https://doc.qt.io/qt-6/qdatetime.html#toString it is reported

            Note: Day and month names as well as AM/PM indication are given in English (C locale). If localized month and day names and localized forms of AM/PM are used, use QLocale::system().toDateTime().
            

            Correct way to use system locale or a desired locale is

            qInfo().nospace() << QLocale::system().toString(QDateTime::currentDateTime(), "dd MMMM yyyy hh:mm:ss.zzz");
            
            1 Reply Last reply Reply Quote 2
            • First post
              Last post