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. QDateTime::fromString() returns invalid
Forum Updated to NodeBB v4.3 + New Features

QDateTime::fromString() returns invalid

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 5.3k 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.
  • C Offline
    C Offline
    chatumao
    wrote on last edited by
    #1

    Hello again,

    today a small problem with QDateTime, I'm pretty sure it's something minor and I'm just too stupid to see it again, but before I rack my head over this for several hours I might just ask somebody a little more knowledgable than myself. Here's what I have:

    @eventTime = QDateTime::fromString( qsl.at(0), "yyyy-MM-dd h:mm:ss AP" );@

    eventTime is a QDateTime object, qsl is a QStringList and at(0) might return something like
    2014-03-11 4:35:38 PM. The format is always the same, but the values change, of course.

    The values from the debugger are correct (qsl.at(0) is indeed of the aforementioned format), but the conversion fails.

    I knocked together a little app doing just that, letting me feed it with a date, a matching format in another line and a third line showing me the date in a predefined format. Using the same values as above, everything works fine.

    Help is much appreciated!

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

      Hi,

      Can you elaborate on what fails ?

      What input makes it fail ?

      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
      • C Offline
        C Offline
        chatumao
        wrote on last edited by
        #3

        I replaced the qsl.at(0) call with a concrete date to make sure it's not because of that. Here's the line now:

        @eventTime = QDateTime::fromString( "2014-03-11 4:35:38 PM", "yyyy-MM-dd h:mm:ss AP" );@

        In debugger I get "(invalid)" as value of eventTime. I use the gdb debugger, btw.

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

          What if you print the value of eventTime ?

          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
          • C Offline
            C Offline
            chatumao
            wrote on last edited by
            #5

            What good would that do? I already know the value. Given that the debugger is bug-free. Anything curious about the format I used? Seems ok to me...

            1 Reply Last reply
            0
            • JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              Your format looks fine visually, but I'm not able to test it in code at the moment.

              [quote author="chatumao" date="1395615052"]What good would that do?[/quote]It might reveal some clues to what went wrong.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              0
              • C Offline
                C Offline
                chatumao
                wrote on last edited by
                #7

                If the the value in the debugger is "(invalid)", what do you think will be printed?

                Ok, I did it anyways. To my surprise, the value printed seems to be in order?!
                Can someone explain? I used the debuger before to check on QDateTime objects and there has never been a problem with the values.

                1 Reply Last reply
                0
                • JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #8

                  I tried your code, and it works on both Qt 4.8.5 and Qt 5.2.1. Your format string is correct.

                  [quote author="chatumao" date="1395639145"]If the the value in the debugger is "(invalid)", what do you think will be printed?[/quote]There are multiple possibilities. That's why printing it is helpful.

                  [quote]Ok, I did it anyways. To my surprise, the value printed seems to be in order?!
                  Can someone explain? I used the debuger before to check on QDateTime objects and there has never been a problem with the values.[/quote]I'm not sure, I haven't seen something like this before myself. But I daresay Qt can understand its own data quite well; what does eventTime.isValid() return?

                  P.S. Debuggers are not guaranteed to be bug-free: http://www.gnu.org/software/gdb/bugs/

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    chatumao
                    wrote on last edited by
                    #9

                    Ok. eventTime.isValid() returns 1. I guess that's something.

                    I tried it with the Microsoft debugger cdb.exe.

                    @eventTime = QDateTime::fromString( "2014-03-11 3:00:38 PM", "yyyy-MM-dd h:mm:ss AP" );@

                    gives me unix time 0 in the debug information (01.01.1970, 00:00:00).

                    Printing out the date is working fine.

                    I am aware of bugs in debuggers, from what I gather there is no bug-free software as soon as there are enough lines of code involved. I just couldn't believe that I would actually encounter one in gdb myself, that's all.

                    I doubt that it is a bug in the debugger, though. cdb is also showing odd behavior, so I guess it's a Qt thing. Maybe I should report this somewhere?

                    Thanks for the time both of you.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mlong
                      wrote on last edited by
                      #10

                      Feel free to open a bug report at https://bugreports.qt-project.org

                      If you do, you might post the ID of the report here so anyone interested could follow it.

                      Software Engineer
                      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        chatumao
                        wrote on last edited by
                        #11

                        Alright, I will give this a try. Would be nice to know if you guys experience the same issues, though. Might save some peoples time.

                        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