From 2e29f55f76e49c1fbffd2af51ec19d59b87f0e72 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Sun, 3 Apr 2022 03:18:32 +0200 Subject: QDate: add conversions for std::chrono calendaring classes std::chrono::year_month_day and related classes offer very convenient to specify dates. This patch adds implicit constructors to QDate to support this convenience, e.g.: // YYYY-MM-DD, DD-MM-YYYY, MM-DD-YYYY formats: QDate d1 = 1985y / December / 8; QDate d2 = 8d / December / 1985; QDate d3 = December / 8d / 1985; // Indexed weekday: QDate d4 = 2000y / January / Monday[0]; QDate d5 = 2000y / January / Monday[last]; and so on. These are all implemented using the conversion from the std calendaring classes to sys_days. Conversions between sys_days and QDate are also added, since they're basically "for free". I don't expect "ordinary" users to stumble upon it, but it's worthy mentioning that std::chrono::year *does* have a year zero (hence, year_month_day in year 0 or below are offset by one with the corresponding QDate). I've left a note in the documentation. [ChangeLog][QtCore][QDate] QDate (and therefore QDateTime) is now constructible using the year/month/day/week classes available in the std::chrono library. Moreover, it now features conversions from and to std::chrono::sys_days. Change-Id: I2a4f56423ac7d1469541cbb6a278a65b48878b4a Reviewed-by: Thiago Macieira --- .../doc/snippets/code/src_corelib_time_qdatetime.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/corelib/doc/snippets/code') diff --git a/src/corelib/doc/snippets/code/src_corelib_time_qdatetime.cpp b/src/corelib/doc/snippets/code/src_corelib_time_qdatetime.cpp index a477e91548..265f46c067 100644 --- a/src/corelib/doc/snippets/code/src_corelib_time_qdatetime.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_time_qdatetime.cpp @@ -216,3 +216,23 @@ QString string = "Tuesday, 23 April 12 22:51:41"; QString format = "dddd, d MMMM yy hh:mm:ss"; QDateTime valid = QDateTime::fromString(string, format); //! [21] + +//! [22] +// 23 April 2012: +QDate date = std::chrono::year_month_day(std::chrono::year(2012), + std::chrono::month(4), + std::chrono::day(23)); + +// Same, under `using std::chrono` convenience: +QDate dateWithLiterals1 = 23 / April / 2012y; +QDate dateWithLiterals2 = 2012y / April / 23; + +// Last day of February 2000 +QDate lastDayFeb2020 = 2000y / February / last; + +// First Monday of January 2020: +QDate firstMonday = 2020y / January / Monday[0]; + +// Last Monday of January 2020: +QDate lastMonday = 2020y / January / Monday[last]; +//! [22] -- cgit v1.2.3