summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2023-06-06 17:11:23 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2023-06-07 13:23:30 +0000
commit025ef096f7f54d86af48c9c3b882435c4c874cc3 (patch)
treed9ac85d999543b33fe8ca905798d5184a6e99b65
parent010b9adb9552bc380bc93481fdbdffed61c175a8 (diff)
Fix what we can of macOS's divergence for pre-1900 dates
The system locale backend on macOS uses system APIs to format dates and times in localized ways. Those system APIs appear to know more about time zones than the time_t functions (which artificially cut off before 1900) are willing to tell us. As a result, QDateTime is left to guess the offsets in use before 1900 but the locale-formatting takes a correct offset into account, which can lead to QDateTime and the locale-aware APIs using different offsets. This is further compounded by the system APIs taking into account the calendar transition from Julian to Gregorian. We can't do much about the latter. Previously we were formatting dates by passing the start of the day to the system APIs (which take a date-time, albeit using a "Date" name for the type), along with a date-format that ignores the time of day. For dates before 1900, if the system APIs know the offset in use was less than that in use in the early 1900s, QDateTime (using the latter) gets the start of the day slightly earlier than where the system APIs know it is, so the day before is the date at that time. Use noon on the day in question to avoid this problem, at least for zones that didn't do whole day offset-shifts (crossing the date line) before 1900. Document the problem and the limited extent to which we can solve it. Task-number: QTBUG-54955 Change-Id: I5be1bfdb3013433ee248846533ef73af39f173f5 Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io> (cherry picked from commit 9155a07667a3e3e7ac59382a59ba615d7211a322) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/corelib/text/qlocale_mac.mm12
1 files changed, 11 insertions, 1 deletions
diff --git a/src/corelib/text/qlocale_mac.mm b/src/corelib/text/qlocale_mac.mm
index 9a25bee0de..a2f23f0b14 100644
--- a/src/corelib/text/qlocale_mac.mm
+++ b/src/corelib/text/qlocale_mac.mm
@@ -236,8 +236,18 @@ static QString fourDigitYear(int year, const QString &zero)
static QString macDateToStringImpl(QDate date, CFDateFormatterStyle style)
{
- QCFType<CFDateRef> myDate = date.startOfDay().toCFDate();
+ // Use noon on the given date, to avoid complications that can arise for
+ // dates before 1900 (see QTBUG-54955) using different UTC offset than
+ // QDateTime extrapolates backwards from time_t functions that only work
+ // back to 1900. (Alaska and Phillipines may still be borked, though.)
+ QCFType<CFDateRef> myDate = QDateTime(date, QTime(12, 0)).toCFDate();
QCFType<CFLocaleRef> mylocale = CFLocaleCopyCurrent();
+ // Note: Darwin take the calendar transition from Julian to Gregorian into
+ // account (see QTBUG-54955, again). This means that, just before that
+ // transition, dates are off by ten days, drifting by one day per century
+ // before that, except when the century is a multiple of 400 years. Unless
+ // we can find a way to suppress that, we're stuck with bogus results for
+ // historic dates more than a few centuries back.
QCFType<CFDateFormatterRef> myFormatter
= CFDateFormatterCreate(kCFAllocatorDefault, mylocale, style,
kCFDateFormatterNoStyle);