1

Why this code return an invalid date?

QLocale locale("es");
QDate date = locale.toDate("1-Jun-14", "d-MMM-yy");

If debug the locale variable, it is initialized correctly to locale es_ES, but doesn't return the date and date.isValid() returns false.

László Papp
  • 51,870
  • 39
  • 111
  • 135
mabg
  • 1,894
  • 21
  • 28

2 Answers2

0

I don't know why but you have to put a . behind the month (short version). I tried the following code:

QLocale locale("es");
qDebug() << locale.standaloneMonthName(6, QLocale::ShortFormat);
QDate date = locale.toDate("01-Jun.-14", "dd-MMM-yy");
qDebug() << date;

The output was surprisingly:

"Jun."
QDate("1914-06-01")

Locks like a bug to me and fails if i try to use it with a e.g. german locale.

Update: Ok, i got it. This is pretty confusing. You have to take the short name given by locale.monthName. This is for e.g. "jun." for ES and "Juni" for DE. This does not really make any sense. In germany the short name for "Juni" is "Jun". For spain i do not know it. This is a bug.

Update 2: I think i understand the system (at least for the german locale). If the full name is larger than 4 characters (e.g. "Januar") the short version is the real short form ("Jan") with a dot appended ("Jan."). If the full name is shorter than 4 characters the short form is exactly the same as the long form (e.g. "Mai"). But i do not get why a dot is appended, since for the EN locale no dot is appended.

OnWhenReady
  • 939
  • 6
  • 15
0

I've opened a case on QT and the response is that:

The Unicode CLDR data for Spanish has a dot after the month names.

It isn't a bug, it needs the dot.

More details: https://bugreports.qt-project.org/browse/QTBUG-39519

mabg
  • 1,894
  • 21
  • 28