Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. No straightforward way to handle dates between Qml and C++??
Forum Updated to NodeBB v4.3 + New Features

No straightforward way to handle dates between Qml and C++??

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 3 Posters 686 Views 3 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.
  • P Offline
    P Offline
    patrickkidd
    wrote on last edited by
    #1

    Are stuck with using the JS Date API and automatic conversion to C++ QDateTime via Qt properties with the QDateTime type?

    I have C++ models which store QDate and interface with Qml which uses the JS Date object. I have bene using QDate because all I want to do is record the day, month, and year that a user enters without dealing with time zones. The user enters a date with an unknown format, but is usually "mm/dd/yyyy." It is fine if the parsing function makes some intuitive mistakes, for example switching between mm and dd. It is NOT OK that the parsing function mangles the time zone and all the values in a counter-intuitive way to the extent that the API is unusable (in my opinion). Read on...

    I learned that I should be using QDateTime instead of QDate, so I am converting between them accurately on the C++ side. That's fine.

    But so far as I can tell, I need to be using JS Dates in Qml and then having them automatically converted to a C++ QDateTime property through the Qml engine. The JS Date API is probably unusable; the string parsing behavior is unreliable, but more importantly, the API automatically switches time zones on date objects when simply setting arbitrary values like the year. This makes it very difficult (or impossible!) to create a simple date object that has the right value to retain the same month, day, and year when automatically converted to a C++ QDateTime. This implicit behavior in the JS Date API is not documented anywhere so far as I can tell.

    So from where I am standing, there is no straightforward way to handle dates in Qml given the above problems. Are some of my assumptions incorrect? Is this feature simply incomplete?

    https://alaskafamilysystems.com/

    Gojir4G 1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #2

      @patrickkidd said in No straightforward way to handle dates between Qml and C++??:

      Are stuck with using the JS Date API and automatic conversion to C++ QDateTime via Qt properties with the QDateTime type?

      No, you could create an object in C++ and expose it to QML.

      I think if you want meaningful responses a small, working program that exhibits the issues you describe would be very helpful. I cannot tell from the post if you are using a Date object or using the js date api, or both. I have not worked a whole lot with dates myself, but I know we have applications at our company that uses interfaces to manipulate/show the date using QML. So I may be able to reference those. I just need something to work with first.

      C++ is a perfectly valid school of magic.

      P 2 Replies Last reply
      1
      • P patrickkidd

        Are stuck with using the JS Date API and automatic conversion to C++ QDateTime via Qt properties with the QDateTime type?

        I have C++ models which store QDate and interface with Qml which uses the JS Date object. I have bene using QDate because all I want to do is record the day, month, and year that a user enters without dealing with time zones. The user enters a date with an unknown format, but is usually "mm/dd/yyyy." It is fine if the parsing function makes some intuitive mistakes, for example switching between mm and dd. It is NOT OK that the parsing function mangles the time zone and all the values in a counter-intuitive way to the extent that the API is unusable (in my opinion). Read on...

        I learned that I should be using QDateTime instead of QDate, so I am converting between them accurately on the C++ side. That's fine.

        But so far as I can tell, I need to be using JS Dates in Qml and then having them automatically converted to a C++ QDateTime property through the Qml engine. The JS Date API is probably unusable; the string parsing behavior is unreliable, but more importantly, the API automatically switches time zones on date objects when simply setting arbitrary values like the year. This makes it very difficult (or impossible!) to create a simple date object that has the right value to retain the same month, day, and year when automatically converted to a C++ QDateTime. This implicit behavior in the JS Date API is not documented anywhere so far as I can tell.

        So from where I am standing, there is no straightforward way to handle dates in Qml given the above problems. Are some of my assumptions incorrect? Is this feature simply incomplete?

        Gojir4G Offline
        Gojir4G Offline
        Gojir4
        wrote on last edited by
        #3

        @patrickkidd Hi,
        There is a very light documentation about the Js Date object here, but you can find more info by googling "ECMAScript Date" as it is part of the ECMAScript specification.

        1 Reply Last reply
        0
        • fcarneyF fcarney

          @patrickkidd said in No straightforward way to handle dates between Qml and C++??:

          Are stuck with using the JS Date API and automatic conversion to C++ QDateTime via Qt properties with the QDateTime type?

          No, you could create an object in C++ and expose it to QML.

          I think if you want meaningful responses a small, working program that exhibits the issues you describe would be very helpful. I cannot tell from the post if you are using a Date object or using the js date api, or both. I have not worked a whole lot with dates myself, but I know we have applications at our company that uses interfaces to manipulate/show the date using QML. So I may be able to reference those. I just need something to work with first.

          P Offline
          P Offline
          patrickkidd
          wrote on last edited by
          #4

          @fcarney said in No straightforward way to handle dates between Qml and C++??:

          @patrickkidd said in No straightforward way to handle dates between Qml and C++??:

          Are stuck with using the JS Date API and automatic conversion to C++ QDateTime via Qt properties with the QDateTime type?
          

          No, you could create an object in C++ and expose it to QML.

          I think if you want meaningful responses a small, working program that exhibits the issues you describe would be very helpful. I cannot tell from the post if you are using a Date object or using the js date api, or both. I have not worked a whole lot with dates myself, but I know we have applications at our company that uses interfaces to manipulate/show the date using QML. So I may be able to reference those. I just need something to work with first.

          Hmm, I would put together an example but the problems I described with the JS Date object are well-documented in the HTML/JS scene. (Plus, I don't have the time!) For what it's worth, the JS Date and Qml Date are the same thing.

          @Gojir4 said in No straightforward way to handle dates between Qml and C++??:

          @patrickkidd Hi,
          There is a very light documentation about the Js Date object here, but you can find more info by googling "ECMAScript Date" as it is part of the ECMAScript specification.

          I familiar with the documentation from Qt and the various browser implementations but they are all terribly insufficient to effectively deal with the cases I am describing.

          https://alaskafamilysystems.com/

          1 Reply Last reply
          0
          • fcarneyF fcarney

            @patrickkidd said in No straightforward way to handle dates between Qml and C++??:

            Are stuck with using the JS Date API and automatic conversion to C++ QDateTime via Qt properties with the QDateTime type?

            No, you could create an object in C++ and expose it to QML.

            I think if you want meaningful responses a small, working program that exhibits the issues you describe would be very helpful. I cannot tell from the post if you are using a Date object or using the js date api, or both. I have not worked a whole lot with dates myself, but I know we have applications at our company that uses interfaces to manipulate/show the date using QML. So I may be able to reference those. I just need something to work with first.

            P Offline
            P Offline
            patrickkidd
            wrote on last edited by
            #5

            @fcarney said in No straightforward way to handle dates between Qml and C++??:

            to work with first.

            And by the way, creating a simple app-specific date object is a good idea in cases like mine when you just want a date. Of course, that's what a QDate is for, but automatic conversions between Qml and C++ also make problematic assumptions about timezones. A custom type would avoid that.

            Also, I solved my problem in the meantime by creating a parseDate(dateString) function in C++ (python/PyQt actually) which then falls back on python-dateutil for more predictable parsing of arbitrary date strings. The precise problem I was having issues described in the comment and doc string. What is not described is the additional problem of most date libs (including JS Date) not returning the correct offset for daylight savings time, which we are currently in (making AKST(GMT-8) -> AKDT(GMT-9)). The below code works around that. You can see how this problem its not a simple one to describe, hence my deferral to the issue as well-documented in the HTML/JS world.

                # If no time zone is entered, js Date parses a string as if it were UTC,                                                                                                                                    
                # then adjusts it to local time. So '1/1/2018' becomes 1/1/2018 0:00 GMT, then                                                                                                                              
                # the returned Date object is 12/31/2017 16:00 GMT-8 for Alaska. All we need is                                                                                                                             
                # year, month, and day to be accurate so it is automatically converted to                                                                                                                                   
                # a QDateTime by Qml (which uses local tz), then to QDate by PropertySheetModel.                                                                                                                            
                # So add the tz offset so the time is local but also represents the actual string parsed.                                                                                                                   
                @pyqtSlot(str, result=QDateTime)
                def parseDate(self, s):
                    """ Parse a date as if it were local TZ, instead of JS Date which assumes                                                                                                                               
                    UTC and then automatically adjusting to to local time resulting in incorrect                                                                                                                            
                    mm, dd, yyyy values.                                                                                                                                                                                    
                    """
                    import dateutil.parser
                    ret = None
                    try:
                        dt = dateutil.parser.parse(s)
                    except ValueError:
                        ret = QDateTime()
                    if ret is None:
                        ret = QDateTime(QDate(dt.year, dt.month, dt.day))
                    return ret
            

            https://alaskafamilysystems.com/

            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