From d36f83d319823e9c9a4fd939b12e17223413474e Mon Sep 17 00:00:00 2001 From: Philip Van Hoof Date: Wed, 22 Aug 2012 13:12:12 +0200 Subject: Parse yearless date strings on leap years during LIST in QFtp Adapted _q_parseUnixDir to handle yearless date strings during a leap year while performing LIST. Task-number: QTBUG-26911 Change-Id: I3193400a274a896530d45de986ec8fa5b159abb4 Reviewed-by: Shane Kearns Reviewed-by: Philip Van Hoof --- src/network/access/qftp.cpp | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/network/access/qftp.cpp b/src/network/access/qftp.cpp index 029228b..f577b92 100644 --- a/src/network/access/qftp.cpp +++ b/src/network/access/qftp.cpp @@ -466,13 +466,22 @@ void QFtpDTP::abortConnection() socket->abort(); } -static void _q_fixupDateTime(QDateTime *dateTime) +static void _q_fixupDateTime(QDateTime *dateTime, bool leapYear = false) { // Adjust for future tolerance. const int futureTolerance = 86400; if (dateTime->secsTo(QDateTime::currentDateTime()) < -futureTolerance) { QDate d = dateTime->date(); - d.setYMD(d.year() - 1, d.month(), d.day()); + if (leapYear) { + int prevLeapYear = d.year() - 1; + + while (!QDate::isLeapYear(prevLeapYear)) + prevLeapYear--; + + d.setYMD(prevLeapYear, d.month(), d.day()); + } else { + d.setYMD(d.year() - 1, d.month(), d.day()); + } dateTime->setDate(d); } } @@ -542,6 +551,30 @@ static void _q_parseUnixDir(const QStringList &tokens, const QString &userName, } if (dateTime.isValid()) info->setLastModified(dateTime); + else if (dateString.startsWith("Feb 29")) { + + // When the current year on the FTP server is a leap year and a + // file's last modified date is Feb 29th, and the current day on + // the FTP server is also Feb 29th, then the date can be in + // formats n==2 or n==4. toDateTime in that case defaults to 1900 + // for the missing year. Feb 29 1900 is an invalid date and so + // wont be parsed. This adds an exception that handles it. + + int recentLeapYear; + QString timeString = dateString.mid(7); + + dateTime = QLocale::c().toDateTime(timeString, QLatin1String("hh:mm")); + + recentLeapYear = QDate::currentDate().year(); + + while (!QDate::isLeapYear(recentLeapYear)) + recentLeapYear--; + + dateTime.setDate(QDate(recentLeapYear, 2, 29)); + + _q_fixupDateTime(&dateTime, true); + info->setLastModified(dateTime); + } // Resolve permissions int permissions = 0; -- cgit v0.12