Qt4.8.1 QDate::fromString issue with negative years
-
Using Qt4.8.1, not tried in Qt5
Here is small example:
@
QDate test = QDate::fromString("-0005.01.01", "yyyy.MM.dd");
if(test.isValid()){
PfGlobal::debug_other_debug("PfGlobal::StringToDate", tr("Valid date from QString"));
}else{
PfGlobal::debug_other_debug("PfGlobal::StringToDate", tr("Invalid date from QString")); // always returned
}
test = QDate(-5, 1, 1); // same date created directly from int numbers
if(test.isValid()){
PfGlobal::debug_other_debug("PfGlobal::StringToDate", tr("Valid date from QDate")); // returned
}else{
PfGlobal::debug_other_debug("PfGlobal::StringToDate", tr("Invalid date from QDate"));
}
@I have tried with other dates like "-5.01.01", "-1000.01.01", but QDate::fromString always return invalid date.
In the function help it is written: "yyyy The year as four digit number. If the year is negative, a minus sign is prepended in addition.", so I understood it that way:
@
QDate test = QDate::fromString("-0005.01.01", "-yyyy.MM.dd"); // return QDate January 1st 5AD instead of BC
test.setYMD(-test.year(), test.month(), test.day()); // fix year to be negative
@Is there a way to use QDate::fromString which automatically interpret the date as BC/AD ? Or is it necessary to use a workaround as above when both positive and negative dates are possible?
Thanks,
Razamut -
Facing the exact same issue right now in 5.2.1. Either I'm overseeing something simple or this is a bug...
This is my (somewhat ugly) workaround to cope with both negative and positive dates
@
std::string str = "01.01.-1000";
int year_negative = str.find('-');
if(year_negative != std::string::npos){
str.erase(std::remove(str.begin(), str.end(), '-'), str.end());
}
QDate tmp_date = QDate::fromString(QString::fromStdString(str), "dd.MM.yyyy");
tmp_date.setDate((year_negative != std::string::npos) ? (-1)*tmp_date.year() : tmp_date.year(), tmp_date.month(), tmp_date.day());
@