Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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?

    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?



  • @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.



  • @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.



  • @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.



  • @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
    

Log in to reply