Details
-
Suggestion
-
Resolution: Duplicate
-
Not Evaluated
-
None
-
None
-
None
Description
In my application, I am showing and comparing the age of certain historical events. The users should be able to read: "The king was assassinated 108 years, 9 months and two days ago". And also some sentences in these lines: "The twins died 5 years apart"...
All the dates are manipulated and stored (in a MariaDB database) using QDate class.
It would be very nice to have the means to calculate the difference between two points in the calendar, resulting in a human-readable form. It quickly gets tricky when dealing with multiple calendar systems. I would very much like QDate objects to have an operator- implemented which either:
- Returns an instance of a new object (QTimePeriod? QDatePeriod?)
- Or, returns a tuple of three numbers (QCalendar::YearMonthDay)
The latter option is suboptimal for two reasons. First, because the concept of the time period is calendar-specific. That is two years in the Islamic Civil calendar are about 20 days shorter than two years in the Gregorian calendar, for example. Second is that the time periods in a calendar system need an origin to form a vector space. That is, for example, "two months"; if added to February 1st, 2019, is a different time period than if added to September 1st, 2019. Thus we need to define the starting date to be able to uniquely identify a time period. (Maybe to arithmetically compare them, or to convert them to a "durations"?)
So we could have:
QDate d1; QDate d2; QTimePeriod p = d2 - d1; qDebug() << p.years() << p.months() << p.days(); // Note the plurals please qDebug() << p.toDays(); // is equal to d1.daysTo(d2); qDebug() << p.toMonths(); qDebug() << p.toYears(); QCalendar c{QCalendar::System::Jalali}; qDebug() << p.years(c) << p.months(c) << p.days(c); // Note the calendar argument please
This could be extended to QDateTime or QTime as well.
Please note that a date difference is a different concept compared to a duration.
Attachments
Issue Links
- duplicates
-
QTBUG-77721 Add a function to QDate to calculate the difference to another date
- Open