Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-54955

Mac: Wrong QDate string conversion with Qt::SystemLocaleLongDate on dates prior to 1893-04-02

    XMLWordPrintable

Details

    • macOS
    • 8
    • 9155a0766 (dev), 025ef096f (6.6), c8a28bdda (6.5), 207954d5f (dev), f1dd984d0 (6.5), f46856ae1 (6.6), 3a1a6b034 (tqtc/lts-6.2)
    • Foundation Sprint 83, Foundation Sprint 84

    Description

      QDate(1893, 04, 01).toString(Qt::SystemLocaleLongDate) will output "31. März 1893" on Mac OS X. Any other prior date will show the same problem.

      #include <QDebug>
      #include <QDate>
      
      #include <CoreFoundation/CoreFoundation.h>
      extern "C" { void NSLog(CFStringRef format, ...); }
      
      void dump(const QDate &d)
      {
        qDebug() << "Qt version" << QT_VERSION_STR
                 << "qDebug" << d
                 << ".toString()" << d.toString()
                 << ".toString(Qt::SystemLocaleLongDate)" << d.toString(Qt::SystemLocaleLongDate);
      }
      
      int main(int argc, char *argv[])
      {
        // good
        dump(QDate(1893, 04, 02));
        // bad
        dump(QDate(1893, 04, 01));
      
        // source of bug can be found here (macDateToString from qlocale_mac.mm)
        bool short_format = true;
        CFGregorianDate macGDate;
        macGDate.year = 1893;
        macGDate.month = 4;
        macGDate.day = 1;
        macGDate.hour = 0;
        macGDate.minute = 0;
        macGDate.second = 0.0;
        CFDateRef myDate = CFDateCreate(0, CFGregorianDateGetAbsoluteTime(macGDate, CFTimeZoneCopyDefault()/*null fixes the problem*/));
        CFLocaleRef mylocale = CFLocaleCopyCurrent();
        CFDateFormatterStyle style = short_format ? kCFDateFormatterShortStyle : kCFDateFormatterLongStyle;
        CFDateFormatterRef myFormatter = CFDateFormatterCreate(kCFAllocatorDefault,
                                                               mylocale, style,
                                                               kCFDateFormatterNoStyle);
      
        NSLog(CFSTR("CF conversion: %@"), CFDateFormatterCreateStringWithDate(0, myFormatter, myDate));
      
        return 0;
      }
      
      Qt version 5.8.0 qDebug QDate("1893-04-02") .toString() "So. Apr. 2 1893" .toString(Qt::SystemLocaleLongDate) "2. April 1893"
      Qt version 5.8.0 qDebug QDate("1893-04-01") .toString() "Sa. Apr. 1 1893" .toString(Qt::SystemLocaleLongDate) "31. März 1893"
      2016-07-27 14:42:05.917 datebug2[19841:11691404] CF conversion: 31.03.93
      

      Replacing CFTimeZoneCopyDefault() by null fixes this. Should a date care about timezones anyway?

      Additional note
      It happens with older date than 1800.
      Using QDateTime type as the first argument seems to work

         QDateTime dt;
        dt.setDate(QDate(782, 11, 12));
        qDebug() << dt << QLocale().toString(dt, QLocale::ShortFormat) << QLocale().toString(dt.date(), QLocale::ShortFormat);
      

      results in

      QDateTime(0782-11-12 00:00:00.000 EET Qt::LocalTime) "12.11.0782 00:00" "8.11.782"
      

      Sample code attached QTBUG-54955.zip

      Attachments

        For Gerrit Dashboard: QTBUG-54955
        # Subject Branch Project Status CR V

        Activity

          People

            Eddy Edward Welbourne
            njeisecke Nils Jeisecke
            Vladimir Minenko Vladimir Minenko
            Alex Blasche Alex Blasche
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: