@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