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 Datetime
Forum Updated to NodeBB v4.3 + New Features

QDateTime::fromString() returns invalid Datetime

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 6 Posters 3.7k 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
    Christian M.
    wrote on last edited by Christian M.
    #14
    This post is deleted!
    JonBJ 1 Reply Last reply
    0
    • C Christian M.

      This post is deleted!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #15

      @Christian-M
      Hello and welcome.

      I do not understand. The whole point is that is that from 02:00 to 02:59 on that day there IS no "valid time". It does not exist. In local time, which is what you are asking about. At best it is ambiguous: does 02:30 represent 30 minutes after the clocks changed from 02:00 or 30 minutes before they changed to 03:00? So I don't know what you mean by "To get a valid QDateTime for example in UTC" when the local time is simply not valid. If you want to treat 02:30 as a UTC time that is fine, but is quite a different time from local time.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Christian M.
        wrote on last edited by Christian M.
        #16
        This post is deleted!
        JonBJ 1 Reply Last reply
        0
        • C Christian M.

          This post is deleted!

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #17

          @Christian-M
          OK, so the only correct way to do this is: convert from foreign TZ (where 02:00--03:00 is valid) to UTC, convert from UTC to your local TZ (no ambiguity, a given UTC only maps to one local TZ time). It never was correct to even try to interpret that foreign TZ time as a local time. You might as well interpret that foreign time as though it were UTC, since you don't know anyway, then at least it won't cause an error.

          QDateTime QDateTime::fromString(const QString &string, const QString &format, const QTimeZone &timeZone);

          Indeed! Can one not achieve this by either adding some TZ information to the string or is void QDateTime::setTimeZone(const QTimeZone &toZone) just what this is for? I might have a play later if I have some time....

          P.S.
          I see that @Christian-Ehrlicher said earlier

          But where do you pass this information? QDateTime::fromString() is using the current locale. Use QLocale::toDateTime() instead

          Maybe that will allow conversion from a foreign local time?

          C 1 Reply Last reply
          0
          • JonBJ JonB

            @Christian-M
            OK, so the only correct way to do this is: convert from foreign TZ (where 02:00--03:00 is valid) to UTC, convert from UTC to your local TZ (no ambiguity, a given UTC only maps to one local TZ time). It never was correct to even try to interpret that foreign TZ time as a local time. You might as well interpret that foreign time as though it were UTC, since you don't know anyway, then at least it won't cause an error.

            QDateTime QDateTime::fromString(const QString &string, const QString &format, const QTimeZone &timeZone);

            Indeed! Can one not achieve this by either adding some TZ information to the string or is void QDateTime::setTimeZone(const QTimeZone &toZone) just what this is for? I might have a play later if I have some time....

            P.S.
            I see that @Christian-Ehrlicher said earlier

            But where do you pass this information? QDateTime::fromString() is using the current locale. Use QLocale::toDateTime() instead

            Maybe that will allow conversion from a foreign local time?

            C Offline
            C Offline
            Christian M.
            wrote on last edited by Christian M.
            #18
            This post is deleted!
            JonBJ 1 Reply Last reply
            0
            • C Christian M.

              This post is deleted!

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #19

              @Christian-M
              Firstly, your post has literally crossed with my adding a P.S. to my reply above.

              foreingTz.setTimeZone(QTimeZone(QByteArray("UTC+3")));

              Are you sure this approach is correct (I don't think so)? Ignoring for the moment that you may know the foreign timezone name but not its UTC offset. Let's say this foreign TZ has its own daylight savings rules, and let's say that this foreign local time does (or does not, you don't know) lie in its DST. Then, correct me if I am wrong, I cannot see how from UTC+3 datetime code could possibly know whether DST is or is not in effect on the stated time. In effect, you need to know whether to pass, say, UTC+3 vs UTC+4 for that foreign local time at its different times of year, and you do not know that. In which case, it cannot convert correctly.....???

              [P.S. I assume your input string does not itself come with any UTC+... suffix on it. Obviously if it does then the whole thing is trivial, since one can get to UTC from that and then UTC to your local. I assume you mean the time string has no UTC information on it, just that you know it comes from e.g. Europe in local time and are wanting to convert to Melbourne local time.]

              C 1 Reply Last reply
              0
              • JonBJ JonB

                @Christian-M
                Firstly, your post has literally crossed with my adding a P.S. to my reply above.

                foreingTz.setTimeZone(QTimeZone(QByteArray("UTC+3")));

                Are you sure this approach is correct (I don't think so)? Ignoring for the moment that you may know the foreign timezone name but not its UTC offset. Let's say this foreign TZ has its own daylight savings rules, and let's say that this foreign local time does (or does not, you don't know) lie in its DST. Then, correct me if I am wrong, I cannot see how from UTC+3 datetime code could possibly know whether DST is or is not in effect on the stated time. In effect, you need to know whether to pass, say, UTC+3 vs UTC+4 for that foreign local time at its different times of year, and you do not know that. In which case, it cannot convert correctly.....???

                [P.S. I assume your input string does not itself come with any UTC+... suffix on it. Obviously if it does then the whole thing is trivial, since one can get to UTC from that and then UTC to your local. I assume you mean the time string has no UTC information on it, just that you know it comes from e.g. Europe in local time and are wanting to convert to Melbourne local time.]

                C Offline
                C Offline
                Christian M.
                wrote on last edited by Christian M.
                #20
                This post is deleted!
                JonBJ 1 Reply Last reply
                0
                • C Christian M.

                  This post is deleted!

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #21

                  @Christian-M
                  Firstly, I mistook you for the OP, I see now you were a responder. I was trying to answer/help the OP. I am not the one who is confused! I have been working with cross-timezone code for decades! What I am trying to discover is how to do something in Qt with this, which I have not done.

                  My understanding of what the OP was asking is:

                  • He is in Australia/Melbourne, that is his local tz.
                  • He gets a datetime string to parse. It does not have a tz specification, e.g. something like "2017-03-26T02:14:34.000".
                  • He does know it represents a local time, and he does know which timezone it is from, but it is not his local timezone. And he does not know whether the foreign time lies in foreign DST or Standard time, he just knows the time is "correct" for a foreign local time. This is important.
                  • The question is what Qt code can he write to correctly parse that datetime from its "foreign" tz to a datetime in his local tz (or to UTC).

                  I do not see how your

                  QCalendar cal(QCalendar::System::Julian);
                  QDateTime ts = QDateTime::fromString("02/10/2022 02:00:00", "dd/MM/yyyy hh:mm:ss", cal); //valid
                  

                  can possibly address this. The fact that you use Julian calendar does not alter the problem. Since nowhere have you told it that the string comes from a foreign local time, it has no idea what this "02:00" time actually was in UTC.

                  Could you please show a worked example you claim which addresses this correctly? Let's say you are in Australia and you are given "02/10/2022 02:00:00" which you know to be in, say, UK time. But your code is not running in UK. Please show how you convert that to, say, UTC time in Qt. Thank you.

                  C 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Christian-M
                    Firstly, I mistook you for the OP, I see now you were a responder. I was trying to answer/help the OP. I am not the one who is confused! I have been working with cross-timezone code for decades! What I am trying to discover is how to do something in Qt with this, which I have not done.

                    My understanding of what the OP was asking is:

                    • He is in Australia/Melbourne, that is his local tz.
                    • He gets a datetime string to parse. It does not have a tz specification, e.g. something like "2017-03-26T02:14:34.000".
                    • He does know it represents a local time, and he does know which timezone it is from, but it is not his local timezone. And he does not know whether the foreign time lies in foreign DST or Standard time, he just knows the time is "correct" for a foreign local time. This is important.
                    • The question is what Qt code can he write to correctly parse that datetime from its "foreign" tz to a datetime in his local tz (or to UTC).

                    I do not see how your

                    QCalendar cal(QCalendar::System::Julian);
                    QDateTime ts = QDateTime::fromString("02/10/2022 02:00:00", "dd/MM/yyyy hh:mm:ss", cal); //valid
                    

                    can possibly address this. The fact that you use Julian calendar does not alter the problem. Since nowhere have you told it that the string comes from a foreign local time, it has no idea what this "02:00" time actually was in UTC.

                    Could you please show a worked example you claim which addresses this correctly? Let's say you are in Australia and you are given "02/10/2022 02:00:00" which you know to be in, say, UK time. But your code is not running in UK. Please show how you convert that to, say, UTC time in Qt. Thank you.

                    C Offline
                    C Offline
                    Christian M.
                    wrote on last edited by Christian M.
                    #22
                    This post is deleted!
                    JonBJ 1 Reply Last reply
                    0
                    • C Christian M.

                      This post is deleted!

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #23

                      @Christian-M
                      Hi, it has taken me a day to get back to you.

                      Unfortunately I am on Qt 5.12 where there is no QCalendar, that was introduced at 5.14. So I cannot test your whole principle of a default Julian calendar and passing that to QDateTime::fromString(). So unless you know of an equivalent to your code for Qt <= 5.12 I cannot test anything.

                      What I can say is that at 5.12

                          QDateTime foreingTz = QDateTime::fromString(datetime_foreign_with_no_timezone_spec);
                          foreingTz.setTimeZone(QTimeZone(QByteArray(foreign_timezone)));
                      

                      does seem to work, where my objective is to treat datetime_foreign_with_no_timezone_spec as a local time in foreign_timezone. What it does is:

                      1. Initial fromString() treats datetime_foreign_with_no_timezone_spec as a local time in my local timezone.
                      2. Calling setTimeZone(foreign_timezone) on that does seem to alter the interpretation of the original datetime string as a local time in the foreign timezone (which is what I am trying to achieve).
                      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